2012-09-16 10 views
7
करने में त्रुटि (Accounts.framework)

मैं निम्नलिखित कोड का उपयोग कर रहा (पर WWDC 2012 वीडियो से पता चला है):फेसबुक एकता 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... 
            } 
           }]; 

मैं भी जोड़ लिया है NSDictionary मेरी .plist फाइल करने के लिए:

enter image description here

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

तो, मेरी समस्या यह है कि मैं निम्न अपवाद प्राप्त कर रहा है

मैंने इस ACAccountTypeIdentifierTwitter और ACAccountTypeIdentifierSinaWeibo के साथ प्रयास किया है। मैं किसी भी अपवाद नहीं ले पा रहा हूँ, हालांकि वे हमेशा लौट रहे हैं granted == NO

उत्तर

17

खैर, 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... 
            } 
           }]; 
+0

इसकी अच्छा कर रहा है। लेकिन अगर हम इस तरह उपयोग करते हैं, तो मेरा ऐप फेसबुक डिफ़ॉल्ट सेटिंग्स में जोड़ा जाता है। तो समस्या यह है कि यह बंद राज्य में है। क्या इसका कोई समाधान है? – sathiamoorthy

0

स्विफ्ट 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))") 
    } 
}