2012-04-08 18 views
5

मैं चाहता हूं कि मेरा आवेदन उपकरण के खिलाफ प्रमाणित करने के लिए i.e 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 उदाहरण में ऊपर यूज़रनेम और पासवर्ड मैं कैसे कर शामिल नहीं है इसे जोड़ें ? इसलिए अनुरोध में उपयोगकर्ता नाम और पासवर्ड भी होगा या शायद उस जानकारी को पास करने के लिए बेहतर होगा। वर्तमान में अनुरोध में केवल यूआरएल स्ट्रिंग है।

धन्यवाद ईआर

उत्तर

8

में connection:canAuthenticateAgainstProtectionSpace: और connection:didReceiveAuthenticationChallenge:

उदाहरण देखें जब एक अनुरोध प्रमाणीकरण की आवश्यकता है, जिससे आप 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 
     } 
    } 
} 
0

आप NSURLConnection ईवेंट प्रबंधित करने के NSURLConnectionDelegate प्रतिनिधि उपयोग करना चाहिए।

प्रमाणीकरण के लिए वहाँ दो प्रतिनिधियों तरीके हैं: URL Loading System Programming Guide

संबंधित मुद्दे