我使用 Swift 3 在 iOS 上创建了一个 Facebook 登录按钮,它可以工作,但我想知道如何让我的按钮不打开新的 Web View 页面来请求权限。我想向用户展示这个:
有人知道怎么做吗?
我到处搜索,但我不知道该怎么做。
最佳答案
下面的代码可帮助您提供已登录设备的用户的访问 token 。用户个人资料的此访问 token 可帮助您直接登录 Facebook。
在 AppDelegate 中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
return FBSDKApplicationDelegate .sharedInstance() .application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
{
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
在 View Controller 中:
import Social
import Accounts
@IBAction func fbLoginButton(_ sender: Any)
{
var facebookAccount: ACAccount?
let accountStore = ACAccountStore()
let accountType = accountStore.accountType(
withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook)
let options = [ACFacebookAppIdKey: "YOUR_FB_APP_ID_KEY", ACFacebookPermissionsKey: ["public_profile","email"],ACFacebookAudienceKey:ACFacebookAudienceFriends] as [String : Any]
let facebookAccountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook)
accountStore.requestAccessToAccounts(with: facebookAccountType, options: options) { (success, error) -> Void in
if success
{
let accounts = accountStore.accounts(with: accountType)
facebookAccount = accounts?.last as? ACAccount
let accountsArray=accountStore.accounts(with: accountType) as NSArray
facebookAccount=accountsArray.lastObject as? ACAccount
print(facebookAccount!)
var dict:NSDictionary
dict=NSDictionary()
dict=facebookAccount!.dictionaryWithValues(forKeys: ["properties"]) as NSDictionary
let properties:NSDictionary=dict["properties"] as! NSDictionary
print("facebook Response is ::::",properties)
print((facebookAccount?.credential.oauthToken)!)
}
else
{
print("Access denied")
print((error?.localizedDescription)!)
}
}
}
关于ios - 使用 native Facebook 帐户(在设置中)登录 Facebook,无需网页 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43026604/