Basic HTTP Authentication on iPhone

后端 未结 2 540
失恋的感觉
失恋的感觉 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:46

    Two things. Firstly you have to use the async methods rather than the synchronous/class method.

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:req]
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:30.0];
    
    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    

    The authentication is managed by implementing this method in your delegate:

    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
    

    And you'll probably also need to implement these methods too:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection;
    

    Using the async method tends to give a better user experience anyway so despite the extra complexity is worth doing even without the ability to do authentication.

提交回复
热议问题