iphone - iOS https认证

标签 iphone objective-c https

我希望我的应用程序针对设备进行身份验证,即通过 https 传递用户名和密码。

我看了几个例子,基本上构建了以下内容:

-(void)authentication
{
    //setting the string of the url taking from appliance IP.
    NSString *urlString =[NSString 
                          stringWithFormat:
                          @"http://%@",appliance_IP.text];
    //define the url with the string
    NSURL *url = [NSURL URLWithString:urlString];

    //creating request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //setting credential taking from the username and password field
    NSURLCredential *credential = [NSURLCredential credentialWithUser:username.text password:password.text persistence:NSURLCredentialPersistenceForSession];


    NSURLProtectionSpace *protectionSpace=
    [[NSURLProtectionSpace alloc]initWithHost:urlString port:443 protocol:@"https" realm:nil authenticationMethod:nil ];


    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];

    [ NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];

}

因为我使用的示例我需要更多理解,上面示例中的 NSURLConnection 不包含用户名和密码,我该如何添加?因此请求还将包含用户名和密码,或者传递该信息可能更好。 目前请求只包含url字符串。

谢谢 错误

最佳答案

当发出需要身份验证的请求时,您将收到 didReceiveAuthenticationChallenge 的回调,您的处理如下:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
   if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
   {
       if ([challenge.protectionSpace.host isEqualToString:MY_PROTECTION_SPACE_HOST])
       {
           [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
       }

       [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
   }
   else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic])
   {
      if ([challenge previousFailureCount] == 0)
      {
          NSURLCredential *newCredential;

          newCredential = [NSURLCredential credentialWithUser:MY_USERNAME
                        password:MY_PASSWORD
                        persistence:NSURLCredentialPersistenceForSession];

          [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
      }
      else
      {
          [[challenge sender] cancelAuthenticationChallenge:challenge];

          // inform the user that the user name and password
          // in the preferences are incorrect

          NSLog (@"failed authentication");

          // ...error will be handled by connection didFailWithError
      }
  }
}

关于iphone - iOS https认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10063224/

相关文章:

javascript - http ://whateverorigin. org/的跨域访问问题

ios - 如何实现在 AVPlayer 上播放的视频的修剪?

ios - 如何在 MBCalendarkit 中显示选定月份的事件

wordpress - 在 Wordpress 中通过 HTTPS 加载 CSS 和 JS

iphone - 崩溃 iPad 照片选择器

C++ 在 XCode 4.2 中不编译(链接?)

javascript - leaflet 弹出窗口在 HTTPS 上无法正确执行,但在 HTTP 上工作得更好

iphone - 如何从 iPhone 应用程序将文件上传到服务器?

iphone - 将全局字典添加到 AppDelegate 与单例类

iphone - 多语言应用程序与许多应用程序