我正在努力克服iOS 5中对NSURLConnection所做的更改。
基本上,方法发生了变化,因此,如果您需要连接到使用HTTPS的站点并发送凭据进行身份验证,则不能再这样做了,因为许多方法已被弃用。
有没有什么地方可以让我获得如何编写代码以使用iOS5兼容代码的工作示例?我已经检查了文档和头文件,但是没有一个单独的资源向我展示如何使用示例进行这种连接,该示例显示了代码在哪个文件中的位置(假设我只是从Xcode中的一个标准模板开始,比如单个视图模板或选项卡栏模板)。
我真的很感谢任何建议;因为我的应用程序将在iOS 5上运行,我希望使用新的过程,但缺乏文档和教程使我的任务比我想象的更难。
发布于 2011-12-17 08:42:00
您应该使用NSURLCredentialStorage单例。在使用NSURLConnection之前,您需要通过调用其中一个NSURLCredentialStorage方法将凭据添加到存储中,例如:
- (void)setCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)protectionSpace
发布于 2011-12-20 00:59:12
我使用的是:
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:
(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] <= maxRetryCount ) {
NSURLCredential *newCredential =
[NSURLCredential
credentialWithUser: userName
password:password
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender]
useCredential:newCredential
forAuthenticationChallenge:challenge];
}
else
{
NSLog(@"Failure count %d",[challenge previousFailureCount]);
}
}
这对我来说很好。但是,看起来自签名证书也被接受了。我可能需要发布一个关于这个的单独问题。
https://stackoverflow.com/questions/8541626
复制相似问题