【问题标题】:Twitter Screen Name returning a null in Parse在 Parse 中返回 null 的 Twitter 屏幕名称
【发布时间】:2015-06-17 07:56:03
【问题描述】:

通过 Twitter 登录并尝试获取用户的屏幕名称。屏幕名称每次都会产生一个空值。有什么想法吗?

PFUser *currentUser = [PFUser currentUser];
    [PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
        if (!user) {
            NSLog(@"Uh oh. The user cancelled the Twitter login.");
            return;
        } else if (user.isNew) {
            twitterScreenName = [PFTwitterUtils twitter].screenName;
            NSLog(@"%@",[PFTwitterUtils twitter].screenName);
            NSString * requestString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/show.json?screen_name=%@", twitterScreenName ];

                                        NSURL *verify = [NSURL URLWithString:requestString];
                                        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:verify];
                                        [[PFTwitterUtils twitter] signRequest:request];

                                        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                NSError *error;
                NSDictionary* result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
                if (!error) {

                   user.username =twitterScreenName;
                   user[@"name"]= result[@"name"];
                    user[@"profileDescription"] = result[@"description"];
                  user[@"imageURL"] = [result[@"profile_image_url_https"] stringByReplacingOccurrencesOfString:@"_normal" withString:@"_bigger"];
                    [user saveEventually];
                }
            }];
          [self performSegueWithIdentifier: @"username" sender: self];

        }

【问题讨论】:

  • 我会在下面回答,但是如果你使用 logInWithBlock,你不应该使用第一行 PFUser *currentUser = [PFUser currentUser] 因为还没有人登录所以没有当前用户!
  • @spogebob92 你能解决它吗?我遇到了同样的问题,无法解决。

标签: ios objective-c twitter parse-platform


【解决方案1】:

这是我的做法:

[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
    }
}];

【讨论】:

  • 使用该代码我收到此错误:不兼容的块指针类型将“void (^)(BOOL, NSError *__strong)”发送到“PFUserResultBlock”类型的参数(又名“void (^)(PFUser *__strong, NSError *__strong)')
  • 哦,糟糕,应该是 [PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) { }];我对那个不好。
  • 它真的很奇怪,有时它设法抓取数据,有时它没有。有什么想法吗?
  • 什么时候做,什么时候不做?随机?
  • 它需要我登录两次才能获得屏幕名称。
【解决方案2】:

尝试在 Twitter 门户中进行配置。它将为您提供应用程序拥有哪些权限的选项(这意味着您可能必须启用从 Twitter 仪表板中手动返回用户名)。

【讨论】:

    【解决方案3】:
    - (IBAction)TWloginPress:(id)sender {
        [activityview setHidden:NO];
        [activityview startAnimating];
        [[Twitter sharedInstance] logInWithCompletion:^
         (TWTRSession *session, NSError *error) {
             if (session) {
                  [self signupUser:session.userName email:nil loggedinvia:@"TWITTER"];
             } else {
                 NSLog(@"error: %@", [error localizedDescription]);
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"There was some problem with signing you with twitter. Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
             }
         }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-15
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2018-08-19
      • 1970-01-01
      • 2012-04-01
      相关资源
      最近更新 更多