ios - 使用 native Facebook 帐户(在设置中)登录 Facebook,无需网页 View

标签 ios swift3 facebook-login

我使用 Swift 3 在 iOS 上创建了一个 Facebook 登录按钮,它可以工作,但我想知道如何让我的按钮不打开新的 Web View 页面来请求权限。我想向用户展示这个:

What I want

有人知道怎么做吗?

我到处搜索,但我不知道该怎么做。

最佳答案

下面的代码可帮助您提供已登录设备的用户的访问 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/

相关文章:

react-native - React Native Webview 社交认证

ios - quickType 栏上的自动填充密码键盘键锁图标未显示 - iOS? - 已设置 textContentType = .password?

cocoa - 验证 Facebook token 时遇到问题

ios - 尝试发布 iOS 应用程序时出现黑屏问题

ios - 播放大量音频文件后,AVAudioPlayer停止播放:kAudio_TooManyFilesOpenError

ios - xcodebuild:错误:无法构建工作区。原因:仅构 build 备不能用于运行此目标

swift - 如何使用 coredata 从上到下对 tableview 项目进行排序

ios - 如何在 ios 中实现 Facebook 重新授权?

ios - OpenGL ES 2.0 中 100 x 100 正方形的 2D 网格

ios - 通过 UINavigationViewController 在兄弟 View 之间切换