2014-02-28 25 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

相关问题