views:

185

answers:

3

I have an application which has been running happily, but since I updated to OS3.1 and updated the SDK the application is failing to log onto a remote server, passing a connection string to the stringWithContentsOfUrl function.

Before the update this was working fine, and if I copy the text string which is displayed on the NSLog statement and paste that into a browser, then I get the correct response, however, this is replying with "LOGIN_ERROR" indicating failure.

Any idea why this is now failing and how to fix it?

NSString* userName = [[NSUserDefaults standardUserDefaults] stringForKey:@"username_pref"];
    NSString* password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password_pref"];
    NSString* loginUrl = [NSString stringWithFormat:@"https://website.com/API/login?email=%@&password=%@", userName, password];

    NSLog (@"Logging in as %@ using %@ at [%@]", userName, password, loginUrl);

    NSURL* url = [NSURL URLWithString:loginUrl];
    NSString* loginDetails = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];

    if ([loginDetails compare:@"\"LOGIN_ERROR\""] == NSOrderedSame) 
    {
     DLog (@"Login Failed : LOGIN_ERROR");
     self.isLoggedIn = NO;
    }
    else
    {
     DLog (@"Login Success");
     if (userDetails) {
      [userDetails release];
     }

     NSDictionary* jsonData   = [loginDetails JSONValue];

     userDetails      = [[[DMUserDetails alloc] init] retain];
     userDetails.id     = [[jsonData objectForKey:@"id"] intValue];
     userDetails.api_token   = [jsonData objectForKey:@"api_token"];
     userDetails.full_name   = [jsonData objectForKey:@"full_name"];
     userDetails.mobile_number  = [jsonData objectForKey:@"mobile_number"];
     userDetails.mobile_host   = [jsonData objectForKey:@"mobile_host"];
     userDetails.email    = [jsonData objectForKey:@"email"];
     userDetails.twitter    = [jsonData objectForKey:@"twitter"];
     userDetails.jabber    = [jsonData objectForKey:@"jabber"];
     userDetails.msn     = [jsonData objectForKey:@"msn"];
     userDetails.start_page   = [jsonData objectForKey:@"start_page"];
     userDetails.date_format   = [[jsonData objectForKey:@"date_format"] intValue];
     userDetails.time_format   = [[jsonData objectForKey:@"time_format"] intValue];
     userDetails.sort_order   = [[jsonData objectForKey:@"sort_order"] intValue];
     userDetails.timezone   = [jsonData objectForKey:@"timezone"];
     userDetails.tz_offset   = [jsonData objectForKey:@"tz_offset"];
     userDetails.premium_until  = [jsonData objectForKey:@"premium_until"];
     userDetails.default_reminder = [jsonData objectForKey:@"default_reminder"];

     self.isLoggedIn = YES;
    }

    [self performSelectorOnMainThread:@selector(didFinishLogon) withObject:nil waitUntilDone:NO];
A: 

The most likely problem is that loginDetails is nil, indicating an error retrieving the URL, rather than you actually receiving a "LOGIN ERROR" response.

Pass in an error object and log the error.

Chris Suter
The server returns the string "LOGIN_ERROR" in case of failures, but the URL I am accessing is correct. If I copy and paste the string from the log to a browser it works fine.
Xetius
A: 

Try:

NSError *error = nil;
NSString *loginDetails = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];

if (error != nil) {
    NSLog(@"%@", error);
}
Pierre Bernard
+1  A: 

If your user name is an e-mail address and has an at sign (@) in it, have you tried to escape the at sign in the URL by using %40 instead of @?

BP