Platform
ลงทะเบียนด้วย Apple
14 นาที
คู่มือการลงชื่อเข้าใช้ด้วย apple บทนำ การลงชื่อเข้าใช้ด้วย apple ช่วยให้ผู้ใช้สามารถลงชื่อเข้าใช้แอปด้วย apple id ของตนได้。 ฟีเจอร์นี้มีให้ใช้งานบน ios 13 ขึ้นไป และ parse 3 5 ขึ้นไป ข้อกำหนดเบื้องต้น ในการทำตามคู่มือนี้ คุณจะต้องมี แอปที่สร้างขึ้นที่ back4app ดูที่ คู่มือการสร้างแอปใหม่ เพื่อเรียนรู้วิธีการสร้างแอปที่ back4app ตั้งค่า subdomain สำหรับแอป back4app ของคุณ ดูที่ การเปิดใช้งานเว็บโฮสติ้งและ live query เพื่อเรียนรู้วิธีการสร้าง subdomain ใน back4app บัญชี นักพัฒนา apple 1 สร้างแอปใหม่ที่ back4app ก่อนอื่น คุณต้องมั่นใจว่าคุณมีแอปที่สร้างขึ้นที่ back4app อยู่แล้ว อย่างไรก็ตาม หากคุณเป็นผู้ใช้ใหม่ คุณสามารถตรวจสอบ คู่มือนี้ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอป 2 เพิ่มความสามารถในการเข้าสู่ระบบด้วย apple ในโปรเจกต์ xcode ของคุณ ในโปรเจกต์ xcode ของคุณ ให้คลิกที่ target (1) และไปที่แท็บ signing & capabilities (2) คลิกที่ + ความสามารถ + ความสามารถ ปุ่ม (3) และเพิ่ม เข้าสู่ระบบด้วย apple เข้าสู่ระบบด้วย apple ความสามารถ (4) ในขณะนั้น ให้เลือก bundle identifier bundle identifier (5) และจดข้อมูลนั้นไว้เพราะเราจะต้องใช้มันในภายหลัง 3 สร้าง service id ใหม่ เข้าสู่ระบบบัญชี นักพัฒนาของ apple https //developer apple com/ และไปที่ identifiers identifiers ส่วน ตรวจสอบว่าคุณได้สร้าง bundle identifier bundle identifier นั้นอยู่ที่นั่น คลิกที่ bundle identifier bundle identifier และเลื่อนลง ตรวจสอบว่ามีการเลือก เข้าสู่ระบบด้วย apple เข้าสู่ระบบด้วย apple หรือไม่ คลิก แก้ไข แก้ไข และตรวจสอบให้แน่ใจว่า เปิดใช้งานเป็น app id หลัก เปิดใช้งานเป็น app id หลัก ถูกเลือก ถ้าทุกอย่างถูกต้อง ให้บันทึกและออก 4 ตั้งค่า parse auth สำหรับ apple ไปที่เว็บไซต์ back4app, เข้าสู่ระบบ, และจากนั้นค้นหาแอปของคุณ หลังจากนั้น คลิกที่ การตั้งค่าเซิร์ฟเวอร์ การตั้งค่าเซิร์ฟเวอร์ ค้นหา การเข้าสู่ระบบด้วย apple การเข้าสู่ระบบด้วย apple บล็อก และเลือก การตั้งค่า การตั้งค่า ส่วน การเข้าสู่ระบบด้วย apple การเข้าสู่ระบบด้วย apple ดูเหมือนแบบนี้ ตอนนี้คุณเพียงแค่ต้องวาง bundle id bundle id ของคุณในช่องด้านล่างและคลิกที่ปุ่มเพื่อบันทึก หากคุณพบปัญหาใด ๆ ในการรวม apple login โปรดติดต่อทีมงานของเราผ่านการแชท! 5 ตัวเลือก 1 ดาวน์โหลดเทมเพลตของเรา มีการเขียนโค้ดบางส่วนที่เกี่ยวข้องในการทำให้ sign in with apple ทำงาน ดังนั้นเราจึงสร้าง เทมเพลตนี้ https //github com/templates back4app/parsesigninwithapple ที่คุณสามารถดาวน์โหลดและเปลี่ยน bundle identifier bundle identifier , app id app id , และ client key client key โค้ดมีเอกสารประกอบอย่างครบถ้วน ดังนั้นจึงเป็นจุดเริ่มต้นที่ดี หากคุณต้องการอ่านเอกสารนี้ กรุณาไปที่ขั้นตอนถัดไป 6 ตัวเลือก 2 เขียนโค้ดด้วยตนเอง ภายในมุมมองของคุณ ให้เพิ่มเฟรมเวิร์ก authenticationservices และสร้าง authdelegate ที่จะจัดการ pfuserauthenticationdelegate 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 นำไปใช้ delegates สำหรับ viewcontroller นำ asauthorizationcontrollerdelegate และ asauthorizationcontrollerpresentationcontextproviding ไปใช้สำหรับ viewcontroller 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 ได้ ดังนั้นหลังจากคำว่า do what you want with the data here do what you want with the data here ให้เพิ่ม 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