2015-02-07 2 views
1

페이스 북 기능으로 로그인을 사용하고 있으며 페이스 북에서 이메일 주소를 얻습니다. 여기 내 코드가있다. "라고 invalidationHandler LaunchServices"여기 SLComposeViewController (다른 부분)에서 페이스 북에서 로그인하지 않을 때 SLComposeViewController가 존재하지 않습니다.

if (! _accountStore) { 
     _accountStore = [[ACAccountStore alloc] init]; 
    } 

    if (! _facebookAccountType) { 
     _facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 
    } 

    NSDictionary *options = @{ ACFacebookAppIdKey:@"1501842240102594" }; 

    [_accountStore requestAccessToAccountsWithType: _facebookAccountType 
             options: options 
            completion: ^(BOOL granted, NSError *error) { 
    if (granted) { 
     NSArray *accounts = [_accountStore accountsWithAccountType:_facebookAccountType]; 
     _facebookAccount = [accounts lastObject]; 
     NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 

     SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
          requestMethod:SLRequestMethodGET 
          URL:url parameters:nil]; 
     request.account = _facebookAccount; 
     [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
     NSDictionary *dictionaryForFacebookData = [NSJSONSerialization JSONObjectWithData:responseData 
              options:NSJSONReadingMutableContainers 
              error:nil]; 

    //Here dictionaryForFacebookData is dictionary of facebook data. 

    } 
    else 
    { 
     SLComposeViewController *tweetSheet = [SLComposeViewController 
               composeViewControllerForServiceType:SLServiceTypeFacebook]; 

     [tweetSheet setInitialText:@"Great fun to learn iOS programming at appcoda.com!"]; 
     [self presentViewController:tweetSheet animated:YES completion:nil]; 

     if ([tweetSheet respondsToSelector:@selector(popoverPresentationController)]) 
     { 
      // iOS 8+ 
      UIPopoverPresentationController *presentationController = [tweetSheet popoverPresentationController]; 

      presentationController.sourceView = sender; // if button or change to self.view. 
     } 
    } 
    }]; 
} 

, 그것은 이 일부 오류를 보여줍니다. iOS 8 이전에는 SLComposeViewController를 사용하여 설정 부분에 로그인 Facebook에 사용자를 표시했습니다.

도와주세요.

답변

0
With this you can get user details from facebook 
in .m file 

- (void) getuserdetails 
{ 

    self.accountStore = [[ACAccountStore alloc]init]; 
    ACAccountType *FBaccountType= nil; 
    //[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    if (! FBaccountType) { 
     FBaccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 
    } 
    NSString *key =kFBAppId; 
    NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil]; 


    [self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion: 
    ^(BOOL granted, NSError *e) 
    { 
     if (granted) 
     { 
      NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType]; 
      self.facebookAccount = [accounts lastObject]; 
      NSLog(@"facebook account =%@",self.facebookAccount); 
      [self get]; 
     } 
     else 
     { 
      NSLog(@"fb error %@",e.description); 

      dispatch_async(dispatch_get_main_queue(),^
          { 
           [self performSelectorOnMainThread:@selector(hideLoader) withObject:nil waitUntilDone:YES]; 
           NSLog(@"%@",e.description); 
           if([e code]== ACErrorAccountNotFound) 
           { 
            UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Account not found" 
                        message:msgSetUpFBAccount delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
            [alt show]; 
           } 
           else 
           { 
            UIAlertView* alt = [[UIAlertView alloc] initWithTitle:msgFBAccessDenied 
                        message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
            [alt show]; 
           } 

          }); 
      NSLog(@"error getting permission %@",e); 

     } 
    }]; 
} 

-(void)get 
{ 

    NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook 
              requestMethod:SLRequestMethodGET 
                 URL:requestURL 
               parameters:nil]; 
    request.account = self.facebookAccount; 
    [request performRequestWithHandler:^(NSData *data, 
             NSHTTPURLResponse *response, 
             NSError *error) 
    { 

     if(!error) 
     { 
      list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

      NSLog(@"Dictionary contains: %@", list); 


      if([list objectForKey:@"error"]!=nil) 
      { 
       [self attemptRenewCredentials]; 
      } 
      dispatch_async(dispatch_get_main_queue(),^{ 
      }); 

     } 
     else 
     { 
      [self performSelectorOnMainThread:@selector(hideLoader) withObject:nil waitUntilDone:YES]; 
      NSLog(@"error from get%@",error); 
     } 

    }]; 


} 



-(void)attemptRenewCredentials{ 
    [self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){ 
     if(!error) 
     { 
      switch (renewResult) { 
       case ACAccountCredentialRenewResultRenewed: 
        NSLog(@"Good to go"); 

        [self get]; 
        break; 
       case ACAccountCredentialRenewResultRejected: 
       { 
        NSLog(@"User declined permission"); 
        UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Access Denied" 
                    message:@"You declined permission" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
        [alt show]; 
        break; 
       } 
       case ACAccountCredentialRenewResultFailed: 
       { 
        NSLog(@"non-user-initiated cancel, you may attempt to retry"); 
        UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Access Denied" 
                    message:@"non-user-initiated cancel, you may attempt to retry" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; 
        [alt show]; 
        break; 
       } 
       default: 
        break; 
      } 

     } 
     else{ 
      //handle error gracefully 
      NSLog(@"error from renew credentials%@",error); 
     } 
    }]; 


} 
관련 문제