2015-04-11 11 views
8

Accesso tramite Twitter e tentativo di ottenere il nome di schermata degli utenti. Il nome dello schermo produce un valore null ogni volta. Qualche idea?Nome schermata Twitter che restituisce un valore null in Parse

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

     } 
+0

ti risponderò qui di seguito, ma se si sta utilizzando logInWithBlock, non dovrebbe usare la prima linea PFUser * currentUser = [PFUser currentUser] perché nessuno è connesso ma così non c'è utente corrente ! – AlexKoren

+0

@ spogebob92 sei riuscito a risolverlo? Ho lo stesso problema e non riesco a capirlo. – Cyprian

risposta

3

Ecco come lo faccio:

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

Con questo codice ottengo questo errore: Incompatibile tipo di puntatore a blocchi che invia 'void (^) (BOOL, NSError * __ strong)' al parametro di tipo 'PFUserResultBlock' (noto anche come void (^) (PFUser * __ strong, NSError * __strong) ') – spogebob92

+0

oh oops, dovrebbe essere [PFTwitterUtils logInWithBlock:^(PFUser * utente, errore NSError *) { }]; Il mio male su quello. – AlexKoren

+0

È davvero strano, a volte riesce a prendere i dati, altre volte no. Qualche idea? – spogebob92

0

provare a configurare questo nel portale Twitter. Ti fornirà le opzioni per le autorizzazioni della tua app (il che significa che potresti dover abilitare la restituzione di un nome utente manualmente dalla dashboard di Twitter).

0
- (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]; 
     } 
    }]; 
} 
Problemi correlati