Я пытаюсь войти в систему с помощью социальных сетей и учетных записей.
Вход в Facebook через социальную структуру в ios
Следуйте этому вопросу и преобразовать код в быстрый, но получить ошибку
Операция не может быть завершена. (ошибка 6 com.apple.accounts.)
Найдите много вопросов о переполнении стека в задаче 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
}
}
}
}
EDIT 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 присутствует или нет, если он присутствует, перейдите к дальнейшему, но он не даст разрешения.