Facebook Integration Error ( Accounts.framework) in iOS6
Asked Answered
P

2

7

I am using the following code (showed on WWDC 2012 videos):

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

I have also added the NSDictionary to my .plist file:

enter image description here

So, my problem is that I am receiving the following exception:

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

I have tried with this ACAccountTypeIdentifierTwitter and ACAccountTypeIdentifierSinaWeibo. I am not receiving any exception although they are always returning granted == NO

Pember answered 16/9, 2012 at 20:37 Comment(0)
P
17

Well, the WWDC 2012 shows one thing, but the documentation shows another... The method they are using is now deprecated:

– requestAccessToAccountsWithType:withCompletionHandler: Deprecated in iOS 6.0

What you should do:

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...
                                     }
                                 }];
Pember answered 16/9, 2012 at 21:20 Comment(1)
Its working fine. But if we use like this, my app added into Facebook default settings. So the problem is, it is in OFF state. Is there any solution for this?Matriarchy
Q
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))")
  }
}
Quita answered 4/8, 2017 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.