-2

ユーザーが私のアプリで facebook を介してログインするとき、私は彼らの写真と名前を照会し、その情報を使用して Core Data と私の Parse.com バックエンドを設定します。しかし、それは機能していません。すべてを行うコードは次のとおりです。

#pragma mark - ()

- (IBAction)authButtonAction:(id)sender {
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    // If the person is authenticated, log out when the button is clicked.
    // If the person is not authenticated, log in when the button is clicked.
    if (FBSession.activeSession.isOpen) {
        [appDelegate closeSession];
    } else {
        // The person has initiated a login, so call the openSession method
        // and show the login UX if necessary.
        [appDelegate openSessionWithAllowLoginUI:YES];
        self.authButton.hidden = YES;
    }
}

- (IBAction)toQuadAction:(id)sender {

    // Query to fetch the users name and picture
    NSString *query = @"SELECT name, username FROM user WHERE uid=me() ";

    // Set up the query parameter
    NSDictionary *queryParam = [NSDictionary dictionaryWithObjectsAndKeys:query, @"q", nil];
    // Make the API request that uses FQL
    [FBRequestConnection startWithGraphPath:@"/fql" parameters:queryParam HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error) {
                          if (error) {
                              NSLog(@"Error: %@", [error localizedDescription]);
                          } else {
                              NSLog(@"My name: %@", result[@"data"][0][@"name"]);
                              NSLog(@"My username: %@", result[@"data"][0][@"username"]);

                              //SAVE TO CORE DATA
                              AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                              NSEntityDescription *user = [NSEntityDescription entityForName:@"User" inManagedObjectContext:appDelegate.managedObjectContext];
                              NSFetchRequest *getName = [[NSFetchRequest alloc] init];
                              [getName setReturnsObjectsAsFaults:NO];
                              [getName setEntity:user];
                              NSError *error;
                              NSMutableArray *currentUser = [[appDelegate.managedObjectContext executeFetchRequest:getName error:&error] mutableCopy];

                              //SAVE THE NAME TO CORE DATA
                              [currentUser[0] setName:result[@"data"][0][@"name"]];

                              //SAVE NAME AND PICTURE TO PARSE
                              PFQuery *query = [PFQuery queryWithClassName:@"Student"];
                              [query whereKey:@"email" equalTo:[currentUser[0] valueForKey:@"email"]];
                              [query getFirstObjectInBackgroundWithBlock:^(PFObject *users, NSError *error) {
                                  if (!users){
                                      NSLog(@"The first object request failed");
                                  } else{
                                      NSLog(@"grabbed the object");
                                      //SET THE NAME IN PARSE
                                      [users setObject:result[@"data"][0][@"name"] forKey:@"name"];
                                      //SET THE IMAGE IN PARSE
                                      NSString *URLString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=235&height=385", result[@"data"][0][@"username"]];
                                      NSURL *picURL = [NSURL URLWithString:URLString];
                                      NSData *picData = [NSData dataWithContentsOfURL:picURL];
                                      PFFile *picture = [PFFile fileWithData:picData ];
                                      [users setObject:picture forKey:@"picture"];
                                      [users saveInBackground];
                                  }
                              }];
                          }
                          [self performSegueWithIdentifier:@"facebookToQuad" sender:sender];
                      }];
    }

だから、認証ボタンがあります。ユーザーはそれをクリックして、Facebook でログインします。次に、別のボタンが表示され、アプリに入ることができます。ユーザーがそれをクリックすると、Facebook のプロフィールに写真と名前を照会し、コア データを追加して解析します。ただし、現在、これらのストレージ システムはいずれも実装されていません。

4

1 に答える 1

2

より多くの空白を使用し、より多くのインスタンス変数を宣言してください。コードを開いて、問題を簡単に見つけられるようにします。

たとえば、 の値を取り出して、 result[@"data"][0][@"name"] 独自のローカル変数に割り当てます。

エンティティを CoreData でサブクラス化して、セッターとゲッターで属性の割り当てを管理しやすくします。

次に、ブレークポイントを設定し、コードの各ステップを調べます。

于 2013-07-26T18:45:38.490 に答える