Platform
使用 Apple 注册
14 分
使用 apple 登录教程 介绍 使用 apple 登录使用户能够使用他们的 apple id 登录应用程序。 此功能在 ios 13 及更高版本和 parse 3 5 及更高版本中可用。 先决条件 要完成本教程,您需要: 在 back4app 创建的应用程序 查看 https //www back4app com/docs/get started/new parse app 以了解如何在 back4app 创建应用程序。 为您的 back4app 应用设置子域名 查看 https //www back4app com/docs/platform/activating web hosting 以了解如何在 back4app 中创建子域名。 一个 https //developer apple com/ 。 1 创建一个新的 back4app 应用 首先,必须确保您在 back4app 上有一个现有的应用程序。但是,如果您是新用户,可以查看 https //www back4app com/docs/get started/new parse app 以了解如何创建一个。 2 将“使用 apple 登录”功能添加到您的 xcode 项目中 在您的 xcode 项目中,点击目标 (1) 并转到签名与功能选项卡 (2)。 点击 + 功能 + 功能 按钮 (3) 并添加 使用 apple 登录 使用 apple 登录 功能 (4)。 在此选择您的 包标识符 包标识符 (5) 并记住该信息,因为我们稍后会需要它。 3 创建一个新的服务 id 登录到您的 https //developer apple com/ 并转到 标识符 标识符 部分。检查您创建的 包标识符 包标识符 是否在此 点击 包标识符 包标识符 并向下滚动。检查 使用 apple 登录 使用 apple 登录 是否被选中 点击 编辑 编辑 并确保 启用为主要应用 id 启用为主要应用 id 被选中 如果一切正常,请保存并退出。 4 为 apple 设置 parse auth 访问 back4app 网站,登录,然后找到您的应用。之后,点击 服务器设置 服务器设置 ,搜索 apple 登录 apple 登录 块,并选择 设置 设置 apple 登录 apple 登录 部分看起来是这样的 现在,您只需将您的 bundle id bundle id 粘贴到下面的字段中,然后点击按钮保存。 如果您在集成 apple 登录时遇到任何问题,请通过聊天与我们的团队联系! 5 选项 1 下载我们的模板 在实现 apple 登录时涉及一些编码,因此我们创建了 https //github com/templates back4app/parsesigninwithapple ,您可以下载并更改 bundle identifier bundle identifier , app id app id , 和 client key client key 代码已完全文档化,因此这是一个很好的起点。 如果您更喜欢阅读此文档,请继续下一步。 6 选项 2 手动编写代码 在您的视图中,添加 authenticationservices 框架并创建将处理 pfuserauthenticationdelegate 的 authdelegate: 1 import authenticationservices 2 3 class authdelegate\ nsobject, pfuserauthenticationdelegate { 4 func restoreauthentication(withauthdata authdata \[string string]?) > bool { 5 return true 6 } 7 8 func restoreauthenticationwithauthdata(authdata \[string string]?) > bool { 9 return true 10 } 11 } 7 为 viewcontroller 实现您的代理 为 viewcontroller 实现 asauthorizationcontrollerdelegate 和 asauthorizationcontrollerpresentationcontextproviding: 1 class viewcontroller uiviewcontroller, asauthorizationcontrollerdelegate, asauthorizationcontrollerpresentationcontextproviding 8 添加使用 apple 登录的按钮 viewdidappear 是一个很好的位置。如果选择其他地方,请记得只调用一次 1 override func viewdidappear( animated bool) { 2 super viewdidappear(animated) 3 4 // sign in with apple button 5 let signinwithapplebutton = asauthorizationappleidbutton() 6 7 // set this so the button will use auto layout constraint 8 signinwithapplebutton translatesautoresizingmaskintoconstraints = false 9 10 // add the button to the view controller root view 11 self view\ addsubview(signinwithapplebutton) 12 13 // set constraint 14 nslayoutconstraint activate(\[ 15 signinwithapplebutton leadinganchor constraint(equalto view\ safearealayoutguide leadinganchor, constant 50 0), 16 signinwithapplebutton trailinganchor constraint(equalto view\ safearealayoutguide trailinganchor, constant 50 0), 17 signinwithapplebutton bottomanchor constraint(equalto view\ safearealayoutguide bottomanchor, constant 70 0), 18 signinwithapplebutton heightanchor constraint(equaltoconstant 50 0) 19 ]) 20 21 // the function that will be executed when user tap the button 22 signinwithapplebutton addtarget(self, action #selector(applesignintapped), for touchupinside) 23 } 最后一行中的 applesignintapped 也必须在 viewcontroller 类中定义 1 // this is the function that will be executed when user taps the button 2 @objc func applesignintapped() { 3 let provider = asauthorizationappleidprovider() 4 let request = provider createrequest() 5 // request full name and email from the user's apple id 6 request requestedscopes = \[ fullname, email] 7 8 // pass the request to the initializer of the controller 9 let authcontroller = asauthorizationcontroller(authorizationrequests \[request]) 10 11 // similar to delegate, this will ask the view controller 12 // which window to present the asauthorizationcontroller 13 authcontroller presentationcontextprovider = self 14 15 // delegate functions will be called when user data is 16 // successfully retrieved or error occured 17 authcontroller delegate = self 18 19 // show the sign in with apple dialog 20 authcontroller performrequests() 21 } 9 presentationcontextprovider presentationcontextprovider (asauthorizationcontrollerpresentationcontextproviding) 将询问哪个窗口应该显示授权对话框。由于我们将要在同一窗口中显示它,因此我们必须返回 self view\ window 1 func presentationanchor(for controller asauthorizationcontroller) > aspresentationanchor { 2 // return the current view window 3 return self view\ window! 4 } 10 处理委托 asauthorizationcontrollerdelegate 当委托被调用时,我们必须处理一些选项,因此让我们添加一些代码以明确处理这些选项 1 func authorizationcontroller(controller asauthorizationcontroller, didcompletewitherror error error) { 2 print("authorization error") 3 guard let error = error as? asauthorizationerror else { 4 return 5 } 6 7 switch error code { 8 case canceled 9 // user press "cancel" during the login prompt 10 print("canceled") 11 case unknown 12 // user didn't login their apple id on the device 13 print("unknown") 14 case invalidresponse 15 // invalid response received from the login 16 print("invalid respone") 17 case nothandled 18 // authorization request not handled, maybe internet failure during login 19 print("not handled") 20 case failed 21 // authorization failed 22 print("failed") 23 @unknown default 24 print("default") 25 } 26 } 11 处理 didcompletewithauthorization 的委托 当我们成功认证时,我们可以检索授权信息 1 func authorizationcontroller(controller asauthorizationcontroller, didcompletewithauthorization authorization asauthorization) { 2 3 if let appleidcredential = authorization credential as? asauthorizationappleidcredential { 4 // unique id for each user, this uniqueid will always be returned 5 let userid = appleidcredential user 6 print("userid " + userid) 7 8 // if needed, save it to user defaults by uncommenting the line below 9 //userdefaults standard set(appleidcredential user, forkey "userid") 10 11 // optional, might be nil 12 let email = appleidcredential email 13 print("email " + (email ?? "no email") ) 14 15 // optional, might be nil 16 let givenname = appleidcredential fullname? givenname 17 print("given name " + (givenname ?? "no given name") ) 18 19 // optional, might be nil 20 let familyname = appleidcredential fullname? familyname 21 print("family name " + (familyname ?? "no family name") ) 22 23 // optional, might be nil 24 let nickname = appleidcredential fullname? nickname 25 print("nick name " + (nickname ?? "no nick name") ) 26 / 27 useful for server side, the app can send identitytoken and authorizationcode 28 to the server for verification purpose 29 / 30 var identitytoken string? 31 if let token = appleidcredential identitytoken { 32 identitytoken = string(bytes token, encoding utf8) 33 print("identity token " + (identitytoken ?? "no identity token")) 34 } 35 36 var authorizationcode string? 37 if let code = appleidcredential authorizationcode { 38 authorizationcode = string(bytes code, encoding utf8) 39 print("authorization code " + (authorizationcode ?? "no auth code") ) 40 } 41 42 // do what you want with the data here 43 44 } 45 } 这是我们可以添加 parse 登录代码的地方。因此在 在这里处理数据 在这里处理数据 注释后,让我们添加 1 pfuser loginwithauthtype(inbackground "apple", authdata \["token" string(identitytoken!), "id" userid]) continuewith { task > any? in 2 if ((task error) != nil){ 3 //dispatchqueue main async { 4 print("could not login \nplease try again ") 5 print("error with parse login after siwa \\(task error! localizeddescription)") 6 //} 7 return task 8 } 9 print("successfuly signed in with apple") 10 return nil 11 } 当然,添加 parse 框架: 1 import parse