2012-04-08 12 views
5

私のアプリケーションがアプライアンスに対して認証する、つまりhttpsでユーザー名とパスワードを渡す必要があります。IOS 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は、上記の例では私がどのようにユーザー名とパスワードが含まれていません必要かかった例を使用してASそれを追加しますか?リクエストにはユーザー名とパスワードも含まれています。あるいは、その情報を渡す方が良いかもしれません。 現在、リクエストにはURL文字列のみが含まれています。

おかげ ER

答えて

8

での例を参照してください。

-(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 
     } 
    } 
} 
0

あなたはNSURLConnectionイベントを処理するためにNSURLConnectionDelegateデリゲートを使用する必要があります。認証のために

2つのデリゲートメソッドがあります:connection:canAuthenticateAgainstProtectionSpace:connection:didReceiveAuthenticationChallenge:

は、認証が必要な要求をするとき、あなたは次のように扱うdidReceiveAuthenticationChallengeにコールバックを受け取りますURL Loading System Programming Guide