2015-02-07 34 views
1

我使用登錄facebook功能並從Facebook獲取電子郵件地址。這是我的代碼。未使用Facebook登錄時,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. 
     } 
    } 
    }]; 
} 

在這裏,在SLComposeViewController(其他部分),它顯示了一些錯誤是 「LaunchServices稱爲invalidationHandler」。在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); 
     } 
    }]; 


} 
相關問題