gpt4 book ai didi

json - NSURLConnection 错误。适用于 iOS 而非 OSX

转载 作者:行者123 更新时间:2023-12-02 08:35:25 25 4
gpt4 key购买 nike

我有一个连接问题,我不确定如何解决。我已经梳理了有关错误和 NSURLConnection 的文档。这是我在同一应用程序的 iOS 版本中使用的代码。它在那里工作得很好,但是当我把它带到应用程序的 OS X 版本时,它就不起作用了。这是代码:

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 中。输出是:

Error Domain=NSPOSIXErrorDomain Code=1 "The operation couldn’t be completed. Operation not permitted" UserInfo=0x610000279dc0 {NSErrorFailingURLStringKey=url, NSErrorFailingURLKey=url}

url 实际上是一个正常运行的 url,但是我在这里替换了它。就像我说的,它在 iOS 上运行良好,但在 OS X 上运行良好。是否有一个我可能会丢失的库导致这种情况?还有其他想法吗?

最佳答案

这可能只是设置授权以允许访问的问题。从错误中可以看出它失败是因为不允许这样做,这表明应用程序沙箱正在完成它的工作。对于 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

关于json - NSURLConnection 错误。适用于 iOS 而非 OSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com