我想使用社交和賬戶框架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用戶目前與否,如果存在,則走得更遠,但它不會授予許可。
刪除您APIKey信息,有人可能會使用它在沒有很好的辦法....授予 –