使用手动拖入Facebook 登录SDK 方式接入
1. 开发者平台 - 登录/注册账号 - 创建应用 - 选择应用的用途(第三方游戏应用) - 输入应用显示名称、应用联系邮箱 - 点击创建应用编号…
2. 为应用添加产品 - Facebook登录 - 点击iOS
3. 选择接入方式 - SDK:FB SDK
4. 点击下载iOS版SDK
5. 添加项目Bundle ID
6. 启用单点登录
7. 配置info.plist
注:页面左上角
<key>FacebookAppID</key>
<string>应用编号</string>
<key>FacebookDisplayName</key>
<string>Facebook显示名称</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
8. 添加依赖库
注:部分依赖库是Google登录中使用
9. 配置URL Types
1)组成URL Types:fb + 应用编号
2)配置URL Types
相关代码
10. AppDelegate.swift
import FBSDKCoreKit
func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool {
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application( _ app:UIApplication, open url:URL, options: [UIApplication.OpenURLOptionsKey :Any] = [:] ) -> Bool {
return ApplicationDelegate.shared.application( app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation])
}
11. iOS13.0配置 SceneDelegate.swift
func scene(_ scene:UIScene, openURLContexts URLContexts:Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }
ApplicationDelegate.shared.application( UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation] )
}
12.1 发起登录 - Facebook 原生按钮
- 创建FBLoginButton 对象,添加到view中
let loginButton = FBLoginButton()
loginButton.center = view.center view.addSubview(loginButton)
loginButton.delegate = self
view.addSubview(loginButton)
- 遵循LoginButtonDelegate 并实现代理方法
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
if result!.isCancelled {
print("取消登录")
} else {
print("loginButton")
}
}
func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
print("loginButtonDidLogOut")
}
12.2 发起登录 - 自定义按钮
let login = LoginManager.init()
login.logIn(permissions: ["public_profile", "email"], from: self) { (result, error) in
}
13 进入调试