2014-02-28 24 views
1

我有一個連接問題,我不確定如何解決。我梳理了錯誤文檔和NSURLConnection。這是我在同一個應用的iOS版本中使用的代碼。它在那裏工作得很好,但是當我將它帶到應用程序的OS X版本時,它不起作用。這是代碼:NSURLConnection錯誤。在iOS上不在OSX上工作

NSURL* url = [NSURL URLWithString:@"url"]; 
NSString* data = [NSString stringWithFormat:@"command=retrieve&number=%d", number]; 
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]]; 

NSLog(@"Starting Command"); 
NSOperationQueue* queue = [[NSOperationQueue alloc] init]; 
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
    NSLog([NSString stringWithFormat:@"%@",connectionError]); 
    if ([data length] > 0 && connectionError == nil) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self parseResponseFromCommandRequest:data]; 
     }); 
    } 
}]; 
NSLog(@"Ending Command"); 

該錯誤發生在NSURLConnection。輸出是:

錯誤域= NSPOSIXErrorDomain代碼= 1 「該操作不能完成操作不允許」 的UserInfo = 0x610000279dc0 {NSErrorFailingURLStringKey = URL,NSErrorFailingURLKey = URL}

URL實際上是一個正常運行的網址,但我在此替換它。就像我說的,它在iOS上可以正常工作,但不能在OS X上工作。有沒有一個庫可能會導致這種情況?任何其他想法?

回答

3

這可能只是設置允許訪問權限的問題。它從錯誤中看出它因爲不被允許而失敗,這表明應用程序沙箱正在完成它的工作。對於NSURL,您通常會使用bookmarks.app-scopebookmarks.document-scope

請參見:Enabling Security-Scoped Bookmark and URL Access

根據您的應用程序是如何使用NSURL,網絡權利可能是值得探討:

com.apple.security.network.server = Allow Incoming Connections 
com.apple.security.network.client = Allow Outgoing Connections 
+0

謝謝,我會研究,並送還給你。 – steven

+1

謝謝。你在現場。比我想象的容易。剛進入Capabilities - > Sandbox - >並勾選(客戶端)。現在就像魅力一樣。 – steven

相關問題