Basic HTTP Authentication on iPhone

后端 未结 2 539
失恋的感觉
失恋的感觉 2020-12-02 07:50

I\'m trying to get a small twitter client running and I ran into a problem when testing API calls that require authentication.

My password has special characters in

2条回答
  •  甜味超标
    2020-12-02 08:35

    You can directly aslo write download the username & password in main URL i.e https://username:password@yoururl.com/

    First of all you need to call NSURLConnection Delegate file:-

    • (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

      {

      return YES; }

    and then call - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

    {
    if ([challenge previousFailureCount] == 0)
            {
                NSURLCredential *newCredential;
    
                newCredential = [NSURLCredential credentialWithUser:@"username"
                                                           password:@"password"
                                                        persistence:NSURLCredentialPersistenceForSession];
                NSLog(@"NEwCred:- %@ %@", newCredential, newCredential);
                [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
            }
            else
            {
                [[challenge sender] cancelAuthenticationChallenge:challenge];
                NSLog (@"failed authentication");
            }
    }
    

提交回复
热议问题