2012-09-16 5 views
7

ich den folgenden Code verwenden (dargestellt auf dem WWDC 2012 Videos):Facebook Integration Error (Accounts.framework) in iOS6

self.accountStore = [[ACAccountStore alloc] init]; 

    ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    [self.accountStore requestAccessToAccountsWithType:facebookAccountType 
           withCompletionHandler:^(BOOL granted, NSError *e) { 
            if (granted) 
            { 
             NSArray *accounts = [self.accountStore 
                   accountsWithAccountType:facebookAccountType]; 
             self.facebookAccount = [accounts lastObject]; 
            } else { 
             // Fail gracefully... 
            } 
           }]; 

Ich habe auch die NSDictionary meine .plist Datei:

enter image description here

Also, mein Problem ist, dass ich die folgende Ausnahme erhalte:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Access options are required for this account type.' 

Ich habe mit dieser ACAccountTypeIdentifierTwitter und ACAccountTypeIdentifierSinaWeibo versucht. Ich erhalte keine Ausnahme, obwohl sie immer granted == NO

Antwort

17

Nun kehren die WWDC 2012 zeigt eine Sache, aber die documentation zeigt eine andere ... Die Methode, die sie verwenden, sind jetzt veraltet:

– requestAccessToAccountsWithType:withCompletionHandler: Deprecated in iOS 6.0 

Was Sie tun sollten:

ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

    NSDictionary *options = @{ 
    @"ACFacebookAppIdKey" : @"123456789", 
    @"ACFacebookPermissionsKey" : @[@"publish_stream"], 
    @"ACFacebookAudienceKey" : ACFacebookAudienceEveryone}; // Needed only when write permissions are requested 

    [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options 
           completion:^(BOOL granted, NSError *error) { 
            if (granted) 
            { 
             NSArray *accounts = [self.accountStore 
                   accountsWithAccountType:facebookAccountType]; 
             self.facebookAccount = [accounts lastObject]; 
            } else { 
             NSLog(@"%@",error); 
             // Fail gracefully... 
            } 
           }]; 
+0

Seine funktioniert gut. Aber wenn wir das verwenden, fügt meine App Facebook-Standardeinstellungen hinzu. Also das Problem ist, es ist im OFF-Zustand. Gibt es dafür eine Lösung? – sathiamoorthy

0

Swift 3

let account = ACAccountStore() 
let accountType = account.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook) 
let options: [AnyHashable : Any] = [ACFacebookAppIdKey: "Your App ID on FB", ACFacebookPermissionsKey: ["publish_stream", "publish_actions"], ACFacebookAudienceKey: ACFacebookAudienceEveryone] 

account.requestAccessToAccounts(with: accountType, options: options) { (success, error) in 
    if success { 
    if let accounts = account.accounts(with: accountType) { 
     if accounts.isEmpty { 
     print("No facebook account found, please add your facebook account in phone settings") 
     } else { 
     let facebookAccount = accounts.first as! ACAccount 

     let message = ["status": "My first Facebook posting "] 
     let requestURL = URL(string: "https://graph.facebook.com/me/feed") 

     let postRequest = SLRequest(forServiceType: SLServiceTypeFacebook, 
            requestMethod: SLRequestMethod.POST, 
            url: requestURL, 
            parameters: message) 
     postRequest?.account = facebookAccount 

     postRequest?.perform(handler: {(_, urlResponse, 
      error) in 

      if let err = error { 
      print("Error : \(err.localizedDescription)") 
      } 
      print("Facebook HTTP response \(String(describing: urlResponse?.statusCode))") 
     }) 
     } 
    } 
    } else { 
    print("Facebook account error: \(String(describing: error))") 
    } 
} 
Verwandte Themen