FacebookAuth 0.2.1

FacebookAuth 0.2.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017 年 8 月
SwiftSwift 版本3.1
SPM支持 SPM

Moritz Lang 维护。



  • 作者:
  • Moritz Lang

纯 Swift 的 iOS Facebook 认证,无需任何内部依赖🚀

安装

用法

FacebookAuth 的主要目标是使用简单。要开始使用,您需要遵循以下三个步骤

1. 创建新的 FacebookAuth 实例

let config = FacebookAuth.Config(appID: "facebookAppID")
let facebookAuth = FacebookAuth(config: config)

2. 配置实例以处理 Facebook 响应

在您的 AppDelegate 中添加以下方法

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
    return facebookAuth.handle(url)
}

3. 配置您的 URL 方案

以 "源代码" 的形式打开您的 Info.plist 并添加以下代码片段

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb<YourFacebookAppID></string>
        </array>
    </dict>
</array>

确保将 <YourFacebookAppID> 替换为您的 Facebook 应用 ID。

4. 调用 authenticate 方法

现在,剩下要做的就是调用 FacebookAuth 实例上的 authenticate 方法,库应该会完成剩下的操作。

// self is the current `UIViewController`.
facebookAuth.authenticate(onTopOf: self, permissions: [.publicProfile, .email]) { result in
    guard let token = result.token else {
        guard let error = result.error else { return }
        switch error {
        case .cancelled:
            print("The authentication was cancelled by the user.")
        case .failed:
            print("Something else went wrong.")
        }
        return
    }
    guard let permissions = result.granted else { return }
    print("Token \(token) has the following permissions: \(permissions)")
}

请求权限

如果您以后需要更多权限,您可以调用 askForPermissions 方法并传入您要请求的权限。

facebookAuth.ask(for: [.userBirthday], onTopOf: self) { result in }

您可以像上面一样处理 result,其中 result.granted 是用户授予的所有权限数组。