【问题标题】:Facebook Integration Error ( Accounts.framework) in iOS6iOS6 中的 Facebook 集成错误(Accounts.framework)
【发布时间】:2012-09-09 03:41:05
【问题描述】:

我正在使用以下代码(显示在 WWDC 2012 视频中):

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

我还将NSDictionary 添加到我的.plist 文件中:

所以,我的问题是我收到以下异常:

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

我已经尝试过这个ACAccountTypeIdentifierTwitterACAccountTypeIdentifierSinaWeibo。我没有收到任何异常,尽管他们总是返回 granted == NO

【问题讨论】:

    标签: ios ios6 acaccount


    【解决方案1】:

    嗯,WWDC 2012 展示了一件事,但 documentation 展示了另一件事......他们使用的方法现在已弃用:

    – requestAccessToAccountsWithType:withCompletionHandler: Deprecated in iOS 6.0
    

    你应该做什么:

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

    【讨论】:

    • 它工作正常。但如果我们这样使用,我的应用程序会添加到 Facebook 默认设置中。所以问题是,它处于关闭状态。有什么解决办法吗?
    【解决方案2】:

    斯威夫特 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))")
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 2014-06-02
      • 2012-09-23
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多