Facebook login via social framework in ios

后端 未结 3 931
别那么骄傲
别那么骄傲 2021-01-31 05:48

I\'m new with iOS development. I have to integrate Facebook login in my iOS app, as user logs in it fetches all the info and navigates it to home screen. I have done this via Fa

3条回答
  •  爱一瞬间的悲伤
    2021-01-31 06:36

    Swift 3 Version of solution @reena

    @IBAction func loginButtonPressed(_ sender: UIButton) {
    
        var facebookAccount: ACAccount?
        let options:[String : Any] = [ACFacebookAppIdKey: "YOUR_FACEBOOK_API_KEY", ACFacebookPermissionsKey: ["email"],ACFacebookAudienceKey:ACFacebookAudienceFriends]
    
        let accountStore = ACAccountStore()
    
        let facebookAccountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook)
    
        accountStore.requestAccessToAccounts(with: facebookAccountType, options: options) { (granted:Bool, error:Error?) in
            if granted{
                let accounts = accountStore.accounts(with: facebookAccountType)
                facebookAccount = accounts?.last as? ACAccount
    
                let dict:NSDictionary = facebookAccount!.dictionaryWithValues(forKeys: ["properties"]) as NSDictionary
                let properties:NSDictionary = dict["properties"] as! NSDictionary
    
                print("facebook Response is-->:%@",properties)
            }
            else {
                if let err = error as? NSError, err.code == Int(ACErrorAccountNotFound.rawValue) {
                    DispatchQueue.main.async {
                        self.displayAlert("There is no Facebook accounts configured. you can add or created a Facebook account in your settings.")
                    }
                } else {
                    DispatchQueue.main.async {
                        self.displayAlert("Permission not granted For Your Application")
                    }
                }
            }
        }
    
    }
    

提交回复
热议问题