我有一个运行愉快的应用程序,但自从我更新到OS3.1并更新了SDK后,应用程序无法登录到远程服务器,将连接字符串传递给stringWithContentsOfUrl函数。
在更新之前,这工作正常,如果我复制NSLog语句中显示的文本字符串并将其粘贴到浏览器中,那么我得到正确的响应,但是,这是回复“LOGIN_ERROR”表示失败
知道为什么现在失败以及如何解决它?
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];
答案 0 :(得分:1)
如果您的用户名是电子邮件地址并且其中包含at符号(@),您是否尝试使用%40而不是@来转义URL中的at符号?
答案 1 :(得分:0)
最可能的问题是loginDetails为nil,表示检索URL时出错,而不是实际收到“LOGIN ERROR”响应。
传入错误对象并记录错误。
答案 2 :(得分:0)
尝试:
NSError *error = nil;
NSString *loginDetails = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
if (error != nil) {
NSLog(@"%@", error);
}