开发者

How can I get around this NSURLConnection bug?

I am currently building an iOS app that开发者_JS百科 uses Kosmaczewski's Objective-C REST client/wrapper to connect to a public API.

This public API uses basic authentication over HTTPS.

The problem I've had is that when I try to provide a username/password using the wrapper's interface, it crashes. After doing some searching, I believe this is related to a bug discussed in these two threads:

  • https://devforums.apple.com/message/109867#109867
  • http://forums.macrumors.com/showthread.php?t=786398

Currently, for the sake of moving along development, I'm providing the username/password in the URL in the format: https://admin:pass@site.com

I really need to find a solution to this to do this properly though. I don't like the idea of hacking around it by generating a base64 encoded string and modifying the authentication header. Nor do I want to depart from this wrapper (which is very easy to implement) to using something like ASIHTTPRequest.

So my question would be: Is this really a bug? Is there another explanation for what's happening? What techniques would you suggest to try to get around it?

If you want to look at any code, please see the wrapper as I'm simply implementing it. https://github.com/akosma/iphonerestwrapper

Thanks much!


While you don't want to depart from the wrapper class, it sounds like this is a bug in the wrapper class. NSUrlConnection doesn't have this issue if used by itself, and it's also extremely easy to implement. Here's some sample code for working with REST services that use HTTP authentication, if you choose to go that route. Very simple


-(void)makeConnection{

    NSURL *statusURL = [[NSURL alloc] initWithString:@"URLHERE"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:statusURL];
    //change this to GET/PUT/POST/whatever you need
    [request setHTTPMethod:@"PUT"];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [statusURL release];
    [request release];

    [conn start];

    [conn release];

    responseData = [[NSMutableData alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge previousFailureCount] == 0) 
    {
        NSLog(@"Sending credentials");

        NSURLCredential *newCredential;
        newCredential = [NSURLCredential credentialWithUser:@"USERNAME" password:@"PASSWORD" persistence:NSURLCredentialPersistencePermanent];
        [[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(@"credentials are no good :(");
    }   
}

I'd avoid using a wrapper, since the URL Loading system is very powerful on it's own, introducing more complexity always introduces more bugs :)

For more info : http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜