我正在建立一个Twitter iOS登录。尝试获取新用户的屏幕名称时,第一次登录时没有获取屏幕名称,而第二次登录时却获取了屏幕名称。我该如何做才能在第一次登录时获得屏幕名称?
这是我正在使用的代码:
[PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
if (!user) {
NSLog(@"Uh oh. The user cancelled the Twitter login.");
return;
} else if (user.isNew) {
NSLog(@"User signed up and logged in with Twitter!");
NSLog(@"%@", [PFTwitterUtils twitter].screenName);
[self performSegueWithIdentifier: @"hasUsername" sender: self];
} else {
NSLog(@"User logged in with Twitter!");
NSLog(@"%@", [PFTwitterUtils twitter].screenName);
[self performSegueWithIdentifier: @"hasUsername" sender: self];
}
}];
NSLog函数仅在首次登录时返回空值。
最佳答案
这是使用Twitter登录和解析时使用的内容:
[PFTwitterUtils logInWithBlock:^(BOOL succeeded, NSError *error) {
if ([PFTwitterUtils isLinkedWithUser:[PFUser currentUser]]) {
NSURL *info = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/settings.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:info];
[[PFTwitterUtils twitter] signRequest:request];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!!data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString *userName = dict[@"screen_name"];
userName = [userName stringByReplacingOccurrencesOfString:@"Twitter:" withString:@""];
PFUser *user = [PFUser currentUser];
user[@"Twitter"] = userName;
[user saveEventually];
} else {
//uh oh, no twitter response
}
}];
} else {
//uh oh, failed login
}
}];
从我的其他答复中复制:Twitter Screen Name returning a null in Parse
关于ios - Twitter登录解析iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29590292/