Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 网间网操作系统。如何在Grand Central Dispatch的上下文中处理URL身份验证挑战_Ios_Nsurlconnection_Grand Central Dispatch_Nsurlconnectiondelegate - Fatal编程技术网

Ios 网间网操作系统。如何在Grand Central Dispatch的上下文中处理URL身份验证挑战

Ios 网间网操作系统。如何在Grand Central Dispatch的上下文中处理URL身份验证挑战,ios,nsurlconnection,grand-central-dispatch,nsurlconnectiondelegate,Ios,Nsurlconnection,Grand Central Dispatch,Nsurlconnectiondelegate,我正在大量使用中央大调度。我现在需要对需要身份验证的服务器发出URL请求。iOS方法NSURLConnection是异步的,这使我在异步任务中使用GCD变得复杂。据我所知,NSURLConnection确实支持同步模式,但这需要在请求URL中嵌入user/pass,这毫无意义,因为我不知道这一点。我的选择是什么 谢谢, Doug不幸的是,您唯一的选择是使用异步请求。无法使用sendSynchronousRequest:returningResponse:error:method来处理身份验证挑战

我正在大量使用中央大调度。我现在需要对需要身份验证的服务器发出URL请求。iOS方法NSURLConnection是异步的,这使我在异步任务中使用GCD变得复杂。据我所知,NSURLConnection确实支持同步模式,但这需要在请求URL中嵌入user/pass,这毫无意义,因为我不知道这一点。我的选择是什么

谢谢,
Doug

不幸的是,您唯一的选择是使用异步请求。无法使用sendSynchronousRequest:returningResponse:error:method来处理身份验证挑战。

实际上,同步请求将在失败之前检查密钥链中是否存在凭据。它将使用默认凭据(如果存在)。 例如,在您关闭网络连接之前,在代码中的某个地方:

NSURLProtectionSpace    *protectionSpace    = nil;
NSURLCredential         *credential         = nil;

// Note that you can't wildcard realm, if your server will present a realm you need that here as well.
protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:@"foo.com" port:80 protocol:@"http" realm:nil authenticationMethod:NSURLAuthenticationMethodHTTPBasic];

// This creates the credential
credential = [NSURLCredential credentialWithUser:user password:pass persistence:NSURLCredentialPersistencePermanent];

// This stores it, in the application keychain.
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
现在它被设置为该保护空间的默认凭据,URL加载系统将在访问该保护空间时查找并使用该凭据。容易的这不会做的一件事是基于SSL的身份验证—NSURAuthenticationMethodClientCertificate和NSURAuthenticationMethodServerTrust。出于各种原因,在这种情况下,NSURLConnection要求您实现一个委托来评估服务器和客户端的信任。不过,对于基本的身份验证问题,使用上面的代码可以设置

但要回答关于GCD和NSURLConnection的更大问题,您可以选择。当然,您可以按照所有博客帖子所说的做,并在异步块中使用同步请求,这可能对您有用,也可能会爆炸。您还可以使用较新的方法sendAsynchronousRequest:queue:completionHandler:。这将为您解决许多问题,因为委托回调将在提供的队列上运行。它确实让事情变得简单多了

这也可能值得一读:
爱斯基摩人是值得聆听的

这是用户发起的操作吗?如果是这样,不管同步/异步如何,为什么不直接解析响应,如果是身份验证问题,则向用户提供用户名/密码表单?我认为你需要提供更多的背景。