iOS
...
Users
iOS项目中集成Facebook登录功能的技术指南
17 分
使用 facebook 登录 介绍 在 https //www back4app com/docs/ios/parse swift sdk/users/sign in with google , 我们学习了用户如何使用 google 账户登录 back4app 应用程序。作为后续,我们现在可以添加一个使用 facebook 账户的登录替代方案。为此,我们首先访问 facebook 的开发者页面,并设置在 xcode 项目中集成此功能的要求。facebook 为开发者提供了一个 sdk,以便在不同的应用程序中集成一个 使用 facebook 登录 的选项。 在 https //github com/templates back4app/ios sign in with facebook , 我们提供了一个简单的 xcode 模板,您可以在其中测试我们正在实现的不同登录方法。这个示例已经在 https //www back4app com/docs/ios/parse swift sdk/users/user log in , 您可以重新访问以获取有关该项目的更多详细信息。 先决条件 要完成此快速入门,您需要: 一个最新版本的 xcode。 在 back4app 创建的应用程序。 遵循 https //www back4app com/docs/get started/new parse app 以了解如何在 back4app 上创建一个 parse 应用。 注意: 遵循 https //www back4app com/docs/ios/parse swift sdk 以创建一个连接到 back4app 的 xcode 项目。 目标 使用facebook的sdk和 parseswift 集成用户登录功能。 1 设置facebook的sdk 一旦我们有了xcode项目 https //www back4app com/docs/ios/parse swift sdk/install sdk 到 back4app 应用程序,我们继续添加facebook的sdk,这将允许我们实现登录流程。对于这个例子,我们使用 swift package manager (spm) 来添加facebook登录依赖项。在xcode项目中,转到 文件>添加包… 在搜索栏中,查找 https //github com/facebook/facebook ios sdk 一旦 facebook ios sdk 出现在结果中,点击 添加包 按钮。 接下来,前往你的 facebook 开发者账户的 https //developers facebook com/apps/ 部分并创建一个新应用。输入你的应用所属的类型,然后转到下一页。输入其余信息并点击 创建应用 在你的 https //developers facebook com/apps/ 页面上,找到你新创建的应用,复制其 应用 id 并通过点击其名称进入仪表板。在右上角,你可以找到与您的 facebook 开发者账户关联的 客户端 id 。这两个 id 是后续配置所必需的。 facebook 需要设置登录功能的下一个配置是将几个键值数据输入到你的 xcode info plist 文件中。更准确地说,前往你的 xcode 项目导航器,找到 info plist 并以 源代码 的形式打开它。添加以下值: 1 cfbundleurltypes 2 3 4 cfbundletyperole 5 editor 6 cfbundleurlschemes 7 	 8 fbapp id 9 10 11 12 facebookappid 13 app id 14 facebookclienttoken 15 client token 16 lsapplicationqueriesschemes 17 18 fbapi 19 fb messenger share api 20 用 app id 字符串替换与新创建的 facebook 应用相关的 app id 。 client token 字符串必须替换为位于 dashboard>settings>advanced>security>client token 的客户端令牌。 现在我们必须将 keychain sharing 功能添加到 xcode 项目中。为此,从项目导航器中选择您的项目,然后转到目标部分。选择一个目标并转到 signing & capabilities 选项卡,然后单击 + capability 按钮并添加 keychain sharing 功能 在 appdelegate , 我们在 application( didfinishlaunchingwithoptions ) 委托方法中添加以下行(确保您首先导入 facebookcore 框架) 1 import facebookcore 2 3 @main 4 class appdelegate uiresponder, uiapplicationdelegate { 5 func application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplication launchoptionskey any]?) > bool { 6 7 8 applicationdelegate shared application(application, didfinishlaunchingwithoptions launchoptions) 9 return true 10 } 11 12 13 } 最后,在 scenedelegate , 我们添加以下代码以处理传入的 url 上下文 1 import facebookcore 2 3 class scenedelegate uiresponder, uiwindowscenedelegate { 4 5 6 func scene( scene uiscene, openurlcontexts urlcontexts set\<uiopenurlcontext>) { 7 guard let url = urlcontexts first? url else { 8 return 9 } 10 11 applicationdelegate shared application( 12 uiapplication shared, 13 open url, 14 sourceapplication nil, 15 annotation \[uiapplication openurloptionskey annotation] 16 ) 17 } 18 } 2 使用 facebook 登录与 parseswift 在成功将 facebooklogin 集成到您的 xcode 项目后,我们继续实现 facebook 登录功能。在 https //github com/templates back4app/ios sign in with facebook , logincontroller 负责处理和显示不同的登录选项。然后我们设置 signinwithfacebookbutton 动作: 1 // logincontroller swift file 2 import facebooklogin 3 4 5 class logincontroller uiviewcontroller { 6 7 8 private let signinwithfacebookbutton uibutton = { 9 let button = uibutton(type system) 10 button setimage(uiimage(named "facebookicon"), for normal) 11 button imageview? contentmode = scaleaspectfit 12 return button 13 }() 14 15 override func viewdidload() { 16 super viewdidload() 17 // 18 // layout configuration 19 // 20 21 signinwithfacebookbutton addtarget(self, action #selector(handlesigninwithfacebook), for touchupinside) 22 } 23 } 24 25 // mark sign in with facebook section 26 extension logincontroller { 27 @objc fileprivate func handlesigninwithfacebook() { 28 // todo here we will implement the sign in procedure 29 } 30 } 为了显示 facebook 的登录表单,facebooklogin sdk 允许我们通过 signin(with\ presenting\ callback) 方法设置并呈现它,该方法来自 loginmanager 类。我们必须传递一个包含 string 值的数组,这些值与我们希望从 facebook 收集的数据相关。常见的值是 public profile 和 email 另一方面,第二个参数是一个回调闭包,facebook 在其中返回用户凭证(嵌入在一个 loginmanagerloginresult 对象中)或在身份验证失败时返回错误。 1 // mark sign in with facebook section 2 extension logincontroller { 3 @objc fileprivate func handlesigninwithfacebook() { 4 let loginmanager = loginmanager() 5 6 // method provided by the facebook sdk see https //developers facebook com/docs/facebook login/ios/ for more details 7 loginmanager login(permissions \["public profile", "email"], from self) { \[weak self] result, error in 8 if let error = error { 9 self? showmessage(title "error", message error localizeddescription) 10 return 11 } else if let result = result, result iscancelled { 12 self? showmessage(title "alert", message "sign in cancelled") 13 return 14 } 15 16 // once facebook successfully signs in the user, we retrieve the information related to the sign in process via the result token object, an accesstoken class type 17 guard let accesstoken = result? token else { fatalerror("this dhould never hapen ") } 18 19 // todo sign in the user on your back4app application with the accesstoken 20 } 21 } 22 } 然后我们使用这个凭证让用户登录到 back4app 平台。表示用户的对象是以下结构(请参见 https //www back4app com/docs/ios/parse swift sdk/users/user log in 以获取更多详细信息): 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } 1 import parseswift 2 3 struct user parseuser { 4 5 6 var username string? 7 var email string? 8 var emailverified bool? 9 var password string? 10 11 var age int? 12 } 因此,facebook 返回的凭证包含一个 accesstoken 和用户的 id,这将用于完成登录过程。更准确地说,我们实例化一个 parsefacebook\<user> 对象并调用 login(userid\ authenticationtoken\ completion ) 方法: 1 // mark sign in with facebook section 2 extension logincontroller { 3 @objc fileprivate func handlesigninwithfacebook() { 4 let loginmanager = loginmanager() 5 6 // method provided by the facebook sdk see https //developers facebook com/docs/facebook login/ios/ for more details 7 loginmanager login(permissions \["public profile", "email"], from self) { \[weak self] result, error in 8 if let error = error { 9 self? showmessage(title "error", message error localizeddescription) 10 return 11 } else if let result = result, result iscancelled { 12 self? showmessage(title "alert", message "sign in cancelled") 13 return 14 } 15 16 // once facebook successfully signed in the user, we retrieve the information related to the sign in process via the result token object, an accesstoken class type 17 guard let accesstoken = result? token else { fatalerror("this dhould never hapen ") } 18 19 // with the accesstoken returned by facebook, you need to sign in the user on your back4app application 20 user facebook login(userid accesstoken userid, accesstoken accesstoken tokenstring) { \[weak self] result in 21 // returns the user object asociated to the facebook user returned by facebook 22 switch result { 23 case success(let user) 24 // after the login succeeded, we send the user to the home screen 25 // additionally, you can complete the user information with the data provided by facebook 26 let homecontroller = homecontroller() 27 homecontroller user = user 28 29 self? navigationcontroller? pushviewcontroller(homecontroller, animated true) 30 case failure(let error) 31 // handle the error if the login process failed 32 self? showmessage(title "failed to sign in", message error message) 33 } 34 } 35 } 36 } 37 } 3 验证用户登录和会话创建 为了确保 google 登录成功,您可以查看您的 back4app 应用程序仪表板,查看包含 facebook authdata 参数的新用户。 您还可以在仪表板中验证是否创建了有效的会话,其中包含指向相应用户对象的指针。 4 将现有用户链接到facebook账户 如果您的ios应用需要将facebook账户与您在 back4app 平台上的现有用户关联, parsefacebook\<user> 对象实现了方法 link(id\ accesstoken\ completion ) ,您需要传递facebook账户的 userid 和与会话关联的 accesstoken 。 1 let facebookuserid string // the userid of the facebook account to link to 2 let accesstoken string = accesstoken current! tokenstring // the access token of the currently signed in facebook user 3 4 user facebook link(userid facebookuserid, accesstoken accesstoken) { result in 5 switch result { 6 case success(let user) 7 // linking succeeded, the user is now linked to the corresponding facebook account 8 case failure(let error) 9 // linking failed, handle the error 10 } 11 } 5 注销 注销过程与调用user signout()方法的标准方式没有区别(在之前的指南中详细说明)。但是,当用户使用facebook账户登录时,为了保持一致性,您还必须注销当前的facebook用户。我们可以通过调用以下方法来实现这一点,和user signout()一起使用。 1 loginmanager () logout () 为了验证当前用户是否有链接的facebook账户,您可以通过查看 user current? authdata 字典来检查。 6 运行应用 您可以访问这个 https //github com/templates back4app/ios sign in with facebook 并下载示例项目。在运行项目之前,请确保您已将配置文件设置为与您的开发者账户关联的配置文件。 结论 在本指南的最后,您学习了如何在ios上使用facebook登录或链接现有的 back4app 用户。在下一个指南中,我们将继续介绍另一种登录方法。