iOS
...
Users
在iOS应用中使用ParseSwift SDK与Apple ID集成登录
11 分
使用 apple 登录 介绍 在 ios 应用中集成第三方登录方式时,必须将使用 apple 登录选项作为额外的替代方案。为此,apple 引入了 authenticationservices 框架。该框架允许开发者在任何 xcode 项目中无缝集成一个 使用 apple 登录 按钮。 在 这个仓库 https //github com/templates back4app/ios sign in with apple 中,我们提供了一个简单的 xcode 模板,您可以在其中测试我们正在实现的不同登录方式。这个示例已经在 登录指南 https //www back4app com/docs/ios/parse swift sdk/users/user log in 中介绍过。您可以重新访问它以获取有关该项目的更多详细信息。 先决条件 要完成此快速入门,您需要: 一个最新版本的 xcode。 一个非个人开发团队的 apple 开发者账户。 在 back4app 创建的应用。 请遵循 新 parse 应用教程 了解如何在 back4app 上创建 parse 应用。 注意: 请遵循 安装 parse sdk (swift) 教程 创建一个连接到 back4app 的 xcode 项目。 目标 使用 authenticationservices 框架和 parseswift sdk 集成用户登录功能。 1 设置使用 apple 登录 一旦我们将 xcode 项目 链接 到 back4app 应用,我们继续添加使用 apple 登录的功能。为此,从项目导航器中选择您的项目,然后转到目标部分。选择主目标并转到 签名与功能 标签,然后单击 + 功能 按钮并添加 使用 apple 登录 功能: 这是开始在 ios 应用中集成 使用 apple 登录 方法所需的唯一配置。 2 使用 authenticationservices 框架与 parseswift 使用 apple 登录的流程可以分为三个阶段。但在此之前,让我们添加并设置用于呈现 使用 apple 登录 流程的按钮。在 logincontroller logincontroller 类中,我们添加这个按钮: 1 // logincontroller swift file 2 import authenticationservices 3 4 5 class logincontroller uiviewcontroller { 6 7 8 private let signinwithapplebutton uibutton = { 9 let button = uibutton(type system) 10 button setimage(uiimage(named "appleicon"), 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 signinwithapplebutton addtarget(self, action #selector(handlesigninwithapple), for touchupinside) 22 } 23 } 24 25 // mark sign in with apple section 26 extension logincontroller asauthorizationcontrollerdelegate, asauthorizationcontrollerpresentationcontextproviding { 27 @objc fileprivate func handlesigninwithapple() { 28 // todo here we will implement the sign in procedure 29 } 30 31 // asauthorizationcontrollerdelegate 32 33 func authorizationcontroller(controller asauthorizationcontroller, didcompletewithauthorization authorization asauthorization) { 34 // todo handle the sign in result 35 } 36 37 func authorizationcontroller(controller asauthorizationcontroller, didcompletewitherror error error) { 38 showmessage(title "error", message error localizeddescription) 39 } 40 41 // asauthorizationcontrollerpresentationcontextproviding 42 43 func presentationanchor(for controller asauthorizationcontroller) > aspresentationanchor { 44 guard let window = view\ window else { fatalerror("no uiwindow found!") } 45 return window 46 } 47 } 请注意, logincontroller logincontroller 符合两个新协议: asauthorizationcontrollerdelegate asauthorizationcontrollerdelegate 和 asauthorizationcontrollerpresentationcontextproviding asauthorizationcontrollerpresentationcontextproviding 。第一个协议允许我们将登录结果委托给 logincontroller logincontroller 类。第二个协议用于确定显示登录表单的 uiwindow uiwindow 。 我们现在在 handlesigninwithapple() handlesigninwithapple() 方法中实现这个流程。 在第一阶段,我们准备请求和表单。请求由 asauthorizationappleidrequest asauthorizationappleidrequest 类构造。我们从 asauthorizationappleidprovider asauthorizationappleidprovider 提供者获取这个类的实例,并通过 asauthorizationcontrolle asauthorizationcontrolle 类获取表单。一旦我们有了请求的实例,就必须提供我们感兴趣的范围。到目前为止,apple 仅提供对用户全名和电子邮件的访问。因此,创建请求的标准方法是: 1 @objc fileprivate func handlesigninwithapple() { 2 let provider = asauthorizationappleidprovider() 3 let request asauthorizationappleidrequest = provider createrequest() 4 request requestedscopes = \[ fullname, email] 5 6 } 通过这个请求,我们构建一个 asauthorizationcontroller asauthorizationcontroller 控制器。这个控制器负责显示一个用户进行身份验证并给予相应权限以完成登录过程的表单: 1 @objc fileprivate func handlesigninwithapple() { 2 // as requested by apple, we set up the necessary objects to implement the sign in with apple flow 3 // see https //help apple com/developer account/#/devde676e696 for more details 4 let provider = asauthorizationappleidprovider() 5 let request asauthorizationappleidrequest = provider createrequest() 6 request requestedscopes = \[ fullname, email] 7 8 let authorizationcontroller = asauthorizationcontroller(authorizationrequests \[request]) 9 authorizationcontroller delegate = self // here self is a reference to logincontroller 10 authorizationcontroller presentationcontextprovider = self // here self is a reference to logincontroller 11 12 // presents the sign in with apple sheet and the result will be handled by the asauthorizationcontrollerdelegate delegate 13 authorizationcontroller performrequests() 14 } 在最后阶段,我们处理登录结果。这个结果通过委托方法返回 authorizationcontroller(controller\ didcompletewithauthorization ) authorizationcontroller(controller\ didcompletewithauthorization ) 这个方法的最后一个参数是一个 asauthorization asauthorization 类,包含有关 apple id 和凭据的所有必要信息。这个阶段是我们关联一个 user user 对象并执行登录到 back4app back4app 应用程序的地方。这个 user user 对象具有以下结构(请参见 登录指南 以获取更多详细信息): 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 } 现在,我们从包含在 user user 结果中的数据创建一个 asauthorization asauthorization 对象。我们通过实例化一个 parseapple parseapple 对象(来自 user apple user apple ) 并调用 login(user\ identitytoken ) login(user\ identitytoken ) 方法 1 // mark sign in with apple section 2 extension logincontroller asauthorizationcontrollerdelegate, asauthorizationcontrollerpresentationcontextproviding { 3 4 // asauthorizationcontrollerdelegate 5 6 func authorizationcontroller(controller asauthorizationcontroller, didcompletewithauthorization authorization asauthorization) { 7 // we cast the (asauthorization) authorization object to an asauthorizationappleidcredential object 8 guard let credential = authorization credential as? asauthorizationappleidcredential else { 9 return showmessage(title "sign in with apple", message "invalid credential") 10 } 11 12 guard let identitytoken = credential identitytoken else { 13 return showmessage(title "sign in with apple", message "token not found") 14 } 15 16 // we log in the user with the token generated by apple 17 user apple login(user credential user, identitytoken identitytoken) { \[weak self] result in 18 switch result { 19 case success(let user) 20 // after the login succeeded, we send the user to the home screen 21 // additionally, you can complete the user information with the data provided by apple 22 let homecontroller = homecontroller() 23 homecontroller user = user 24 25 self? navigationcontroller? pushviewcontroller(homecontroller, animated true) 26 case failure(let error) 27 self? showmessage(title "error", message error message) 28 } 29 } 30 } 31 } 3 验证用户登录和会话创建 为了确保 google 登录成功,您可以查看您的 back4app 应用程序仪表板,并查看新的 用户 用户 包含 facebook authdata authdata 参数。 您还可以在仪表板中验证是否创建了有效的会话,其中包含指向相应的 用户 用户 对象。 4 将现有用户链接到 apple id 如果您的 ios 应用需要将 apple id 关联到您在 back4app 平台上的现有用户, parseapple\<user> parseapple\<user> 对象实现了方法 link(user\ identitytoken\ completion ) link(user\ identitytoken\ completion ) ,您需要传递 user user 值和 identitytoken identitytoken ,这些来自 asauthorizationappleidcredential asauthorizationappleidcredential 凭证。 1 let credential asauthorizationappleidcredential 2 3 guard let identitytoken = credentials identitytoken else { 4 return showmessage(title "sign in with apple", message "token not found") 5 } 6 7 user apple link(user credential user, identitytoken identitytoken){ result in 8 switch result { 9 case success(let user) 10 // linking succeeded, user object now is linked to the corresponding apple id 11 case failure(let error) 12 // linking failed, handle the error 13 } 14 } 5 运行应用 您可以访问这个 代码库 并下载示例项目。在运行项目之前,请确保您已将配置文件设置为与您的开发者帐户关联的配置文件。 结论 在本指南的最后,您学习了如何使用 apple 登录在 ios 上登录或链接现有的 back4app 用户。