2015-10-20 141 views
3

我想使用社交和賬戶框架fb登錄。在iOS中使用社交和賬戶框架Facebook登錄swift

Facebook login via social framework in ios
關注這個問題,並將其轉換成代碼迅速,但得到的錯誤

操作無法完成。 (com.apple.accounts錯誤6.)

在目標c中發現堆棧溢出的許多問題,但在swift中找不到。

我的代碼:

import Social 
import Accounts 

class ViewController: UIViewController { 

    var facebookAccount : ACAccount!  
    let appIdKey : NSString = "*************" 
    var facebookAccount : ACAccount! 
    var isFacebookAvailable = Int() 
    var globalMailId = NSString() 
    var fbName = NSString() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.accountStore = ACAccountStore() 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    @IBAction func fbLogin(sender: UIButton) { 

     let accountType = self.accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook) 

     var dictFB = [String : AnyObject]() 
     dictFB[ACFacebookAppIdKey] = "*************" 
     dictFB[ACFacebookPermissionsKey] = ["publish_profile","email","user_friends"] 
     dictFB[ACFacebookAudienceKey] = ACFacebookAudienceFriends 

     self.accountStore.requestAccessToAccountsWithType(accountType, options: dictFB) { (granted : Bool, error : NSError?) -> Void in 
      if granted { 
       let accounts : NSArray = self.accountStore.accountsWithAccountType(accountType) 
       self.facebookAccount = accounts.lastObject as! ACAccount 

       let facebookCredentials = self.facebookAccount.credentials 

      } else { 
       print("Error getting permission : \(error!.localizedDescription)") 
       self.isFacebookAvailable = 0 
      } 
     } 
    } 
} 

編輯1: 我已經更新了我的代碼,這樣

@IBAction func fbLogin(sender: UIButton) { 

    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) { 

     let accountType = self.accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook) 

     var dictFB = [String : AnyObject]() 
     dictFB[ACFacebookAppIdKey] = "463272290501295" 
     dictFB[ACFacebookPermissionsKey] = ["publish_profile","email","user_friends"] 
     dictFB[ACFacebookAudienceKey] = ACFacebookAudienceFriends 

     self.accountStore.requestAccessToAccountsWithType(accountType, options: dictFB) { (granted : Bool, error : NSError?) -> Void in 
      if error != nil { 
       print("Error getting permission : \(error)") 
       } else { 
        print("granted : \(granted)") //print false here 
        if granted { 
        let accounts : NSArray = self.accountStore.accountsWithAccountType(accountType) 
        self.facebookAccount = accounts.lastObject as! ACAccount 

        let facebookCredentials = self.facebookAccount.credentials 
        } else { 
         dispatch_async(dispatch_get_main_queue(), {() -> Void in 
          self.showAlert(nil, message: "Permission not granted For Your Application") 
         }) 
        } 
       } 
      } 
     } else { 
      self.askForSettingsChange("Please login to a Facebook", message: "Go to settings to login Facebook") 
     } 
    } 
    func showAlert(title : String? , message : String?) { 
     let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
     let action = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
     alert.addAction(action) 

     self.presentViewController(alert, animated: true, completion: nil) 
    } 
    func askForSettingsChange(title : String? , message : String?) { 
     let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
     let settingAction = UIAlertAction(title: "Settings", style: .Default) { (action : UIAlertAction) -> Void in 
      dispatch_async(dispatch_get_main_queue(), {() -> Void in 
       UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!) // go to settings 
      }) 
     } 
     let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil) 

     alertController.addAction(settingAction) 
     alertController.addAction(cancelAction) 
     self.presentViewController(alertController, animated: true, completion: nil) 
    } 

支票存入手機設置如果Facebook用戶目前與否,如果存在,則走得更遠,但它不會授予許可。

+0

刪除您APIKey信息,有人可能會使用它在沒有很好的辦法....授予 –

回答

1

這裏是一個輔助類,我在使用帳戶框架(SWIFT)我的應用程序

import Accounts 
import Social 

class LoginManager: NSObject { 

    var facebookAccount: ACAccount? 

    func facebookLogin(completion: (credential: ACAccountCredential?) -> Void) { 
     let store = ACAccountStore() 
     let facebook = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook) 
     let apiKey = "YOUR_FACEBOOK_API_KEY" 
     let loginParameters: [NSObject: AnyObject] = [ACFacebookAppIdKey: apiKey, ACFacebookPermissionsKey: []] 
     store.requestAccessToAccountsWithType(facebook, options: loginParameters) { granted, error in 
      if granted { 
       let accounts = store.accountsWithAccountType(facebook) 
       self.facebookAccount = accounts.last as? ACAccount 

       let credentials = self.facebookAccount?.credential 
       completion(credential: credentials) 
      } else { 
       completion(credential: nil) 
      } 
     } 
    } 
} 
+0

權限在我的情況 –

+0

返回false您需要設置登錄博客。而且您還需要配置Facebook應用程序設置 – mustafa

+0

我已經使用設置和Facebook應用程序設置進行登錄,基本上我添加了捆綁標識符和預先設置,啓用本地應用程序....某些內容是剩餘的? –

1

的Facebook登錄

var facebookAccount: ACAccount? 

     let options: [NSObject : AnyObject] = [ACFacebookAppIdKey: "YOUR_FACEBOOK_API_KEY", ACFacebookPermissionsKey: ["publish_stream","publish_actions"],ACFacebookAudienceKey:ACFacebookAudienceFriends] 
     let accountStore = ACAccountStore() 
     let facebookAccountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook) 

      accountStore.requestAccessToAccountsWithType(facebookAccountType, options: options) { (granted, error) -> Void in 
       if granted 
       { 
        let accounts = accountStore.accountsWithAccountType(facebookAccountType) 
        self.facebookAccount = accounts.last as? ACAccount 
        let accountsArray=accountStore.accountsWithAccountType(facebookAccountType) as NSArray 

        self.facebookAccount=accountsArray.lastObject as? ACAccount 
        var dict:NSDictionary 
        dict=NSDictionary() 

        dict=self.facebookAccount!.dictionaryWithValuesForKeys(["properties"]) 
        let properties:NSDictionary=dict["properties"] as! NSDictionary 
        print("facebook Response is-->:%@",properties) 
       } 
       else 
       { 
        if error.code == Int(ACErrorAccountNotFound.rawValue) { 
         dispatch_async(dispatch_get_main_queue(), {() -> Void in 
          self.displayAlert("There is no Facebook accounts configured. you can add or created a Facebook account in your settings.") 
         }) 
        } else { 
         dispatch_async(dispatch_get_main_queue(), {() -> Void in 
          self.displayAlert("Permission not granted For Your Application") 
         }) 
        } 

       } 
      } 
0

使用我想我可能知道你的問題是什麼..老實說,AcAccountStore不是使用Facebook的最佳方式...

在這裏,你在「ACFacebookPermissionsKey」中給出的選項是錯誤的。它應該是「public_profile」,而不是「發佈」。

祝你好運。

+0

只花了大約8個小時解決我的Swift客戶端,並意識到你不能通過AcAccountStore請求「user_actions.fitness」......它只顯示失敗......不是錯誤代碼沒有什麼...... –

+0

可能不僅僅是「user_actions.fitness」,而且更多的權限也不可能通過AcAccountStore請求。沒有精力去排除故障......只要注意......然後我嘗試過的工作是「public_profile」,「email」,「user_friends」和「user_about_me」。或者,也許這是因爲我在Facebook上沒有「user_actions.fitness」......誰知道它沒有提供任何錯誤信息......不要使用它將是最好的解決方案。 –

0

這裏是我的示例視圖控制器來測試Facebook登錄。確保你的Facebook應用設置是正確的。

import UIKit 
import Accounts 
import Social 

class ViewController: UIViewController { 

// MARK: - Properties 
var facebookAccount: ACAccount? 

// MARK: - Lifecycle 
override func viewDidLoad() { 
    super.viewDidLoad() 
} 

// MARK: - Actions 
@IBAction func buttonFBLoginAction() { 
    facebookLogin { [unowned self] credential in 
     guard let _ = credential else { return } 
     // TODO: - Send token to service for login process. 
     self.performSegueWithIdentifier("ShowSecondVC", sender: nil) 
    } 
} 

// MARK: - Custom Methods 
private func facebookLogin(completion: (credential: ACAccountCredential?) -> Void) { 
    let store = ACAccountStore() 
    let facebook = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook) 
    let apiKey = "**************" 
    let readPermissions = ["public_profile", "email"] 
    let loginParameters: [NSObject: AnyObject] = [ACFacebookAppIdKey: apiKey, ACFacebookPermissionsKey: readPermissions] 
    store.requestAccessToAccountsWithType(facebook, options: loginParameters) { granted, error in 
     if granted { 
      let accounts = store.accountsWithAccountType(facebook) 
      self.facebookAccount = accounts.last as? ACAccount 

      let credentials = self.facebookAccount?.credential 
      completion(credential: credentials) 
     } else { 
      if let error = error { 
       print(error.localizedDescription) 
      } 
      completion(credential: nil) 
     } 
    } 
} 
}