วิธีการลงชื่อเข้าใช้ด้วย Apple บน iOS สำหรับนักพัฒนา
11 นาที
ลงชื่อเข้าใช้ด้วย apple บทนำ เมื่อรวมวิธีการลงชื่อเข้าใช้ของบุคคลที่สามในแอป ios จะต้องเพิ่มตัวเลือกลงชื่อเข้าใช้ด้วย apple เป็นทางเลือกเพิ่มเติม สำหรับวัตถุประสงค์นี้ apple ได้แนะนำ authenticationservices เฟรมเวิร์ก เฟรมเวิร์กนี้ช่วยให้นักพัฒนาสามารถรวม sign in with apple ปุ่มเข้ากับโปรเจ็กต์ xcode ใด ๆ ได้อย่างราบรื่น ใน ที่เก็บนี้ 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 ใหม่ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอป parse ที่ back4app หมายเหตุ ติดตาม บทแนะนำการติดตั้ง parse sdk (swift) https //www back4app com/docs/ios/parse swift sdk เพื่อสร้างโปรเจกต์ xcode ที่เชื่อมต่อกับ back4app เป้าหมาย เพื่อรวมฟีเจอร์การลงชื่อเข้าใช้ผู้ใช้โดยใช้ authenticationservices เฟรมเวิร์กและ parseswift sdk 1 การตั้งค่าการลงชื่อเข้าใช้ด้วย apple เมื่อเรามีโปรเจกต์ xcode เชื่อมโยง https //www back4app com/docs/ios/parse swift sdk/install sdk กับแอป back4app เราจะดำเนินการเพิ่มความสามารถในการลงชื่อเข้าใช้ด้วย apple เพื่อทำเช่นนี้ ให้เลือกโปรเจกต์ของคุณจากตัวนำทางโปรเจกต์และไปที่ส่วนเป้าหมาย เลือกเป้าหมายหลักและไปที่แท็บ signing & capabilities จากนั้นคลิกที่ปุ่ม + capability และเพิ่มความสามารถ sign in with apple นี่คือการตั้งค่าที่จำเป็นเพียงอย่างเดียวในการเริ่มต้นการรวม sign in with apple วิธีการในแอป ios 2 การใช้กรอบงาน authenticationservices กับ parseswift การลงชื่อเข้าใช้ด้วย apple สามารถทำได้ในสามขั้นตอน แต่ก่อนอื่นให้เราทำการเพิ่มและตั้งค่าปุ่มที่จะใช้ในการนำเสนอ sign in with apple ขั้นตอน ใน \<font color="#2166ae">logincontroller\</font> คลาสเราจะเพิ่มปุ่มนี้ 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 } โปรดทราบว่า \<font color="#2166ae">logincontroller\</font> ปฏิบัติตามโปรโตคอลใหม่สองรายการ \<font color="#2166ae">asauthorizationcontrollerdelegate\</font> และ \<font color="#2166ae">asauthorizationcontrollerpresentationcontextproviding\</font> โปรโตคอลแรกช่วยให้เราสามารถมอบหมายผลลัพธ์การลงชื่อเข้าใช้ให้กับ \<font color="#2166ae">logincontroller\</font> คลาส โปรโตคอลที่สองใช้เพื่อกำหนด \<font color="#2166ae">uiwindow\</font> ที่แผ่นลงชื่อเข้าใช้จะแสดง เราจะดำเนินการไหลใน \<font color="#2166ae">handlesigninwithapple()\</font> เมธอด ในขั้นตอนแรก เราจะเตรียมคำขอและแบบฟอร์ม คำขอจะถูกสร้างขึ้นโดย \<font color="#2166ae">asauthorizationappleidrequest\</font> คลาส เราจะได้รับอินสแตนซ์ของคลาสนี้จาก \<font color="#2166ae">asauthorizationappleidprovider\</font> ผู้ให้บริการและแบบฟอร์มโดย \<font color="#2166ae">asauthorizationcontrolle\</font> r คลาส เมื่อเรามีอินสแตนซ์ของคำขอแล้ว เราต้องระบุขอบเขตที่เราสนใจ ดังนั้น apple จึงให้การเข้าถึงชื่อเต็มและอีเมลของผู้ใช้เท่านั้น วิธีมาตรฐานในการสร้างคำขอคือ 1 @objc fileprivate func handlesigninwithapple() { 2 let provider = asauthorizationappleidprovider() 3 let request asauthorizationappleidrequest = provider createrequest() 4 request requestedscopes = \[ fullname, email] 5 6 } ด้วยคำขอนี้ เราสร้าง \<font color="#2166ae">asauthorizationcontroller\</font> คอนโทรลเลอร์นี้มีหน้าที่ในการแสดงแผ่นงานที่ผู้ใช้ทำการยืนยันตัวตนและให้สิทธิ์ที่เกี่ยวข้องเพื่อทำให้กระบวนการลงชื่อเข้าใช้เสร็จสมบูรณ์ 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 } ในขั้นตอนสุดท้าย เราจัดการผลลัพธ์การลงชื่อเข้าใช้ นี่จะถูกส่งกลับผ่านวิธี delegate \<font color="#2166ae">authorizationcontroller(controller\ didcompletewithauthorization )\</font> อาร์กิวเมนต์สุดท้ายในวิธีนี้คือ \<font color="#2166ae">asauthorization\</font> คลาสที่มีข้อมูลที่จำเป็นทั้งหมดเกี่ยวกับ apple id และข้อมูลรับรอง ขั้นตอนนี้คือที่ที่เราสมาคมกับ \<font color="#2166ae">user\</font> อ็อบเจ็กต์และดำเนินการเข้าสู่ระบบไปยัง \<font color="#2166ae">back4app\</font> แอปพลิเคชัน อ็อบเจ็กต์ \<font color="#2166ae">user\</font> นี้มีโครงสร้างดังต่อไปนี้ (ดูที่ คู่มือการเข้าสู่ระบบ 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 } ตอนนี้เราสร้าง \<font color="#2166ae">user\</font> อ็อบเจ็กต์จากข้อมูลที่มีอยู่ใน \<font color="#2166ae">asauthorization\</font> ผลลัพธ์ เราทำสิ่งนี้โดยการสร้าง \<font color="#2166ae">parseapple\</font> อ็อบเจ็กต์ (จาก \<font color="#2166ae">user apple\</font> ) และเรียกใช้ \<font color="#2166ae">login(user\ identitytoken )\</font> เมธอด 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 แดชบอร์ดแอปพลิเคชันของคุณและดู \<font color="#2166ae">ผู้ใช้\</font> ใหม่ที่มี \<font color="#2166ae">authdata\</font> ของ facebook คุณยังสามารถตรวจสอบได้ว่ามีเซสชันที่ถูกต้องถูกสร้างขึ้นในแดชบอร์ด ซึ่งมีการชี้ไปยัง \<font color="#2166ae">ผู้ใช้\</font> ที่เกี่ยวข้อง 4 การเชื่อมโยงผู้ใช้ที่มีอยู่กับ apple id ในกรณีที่แอป ios ของคุณต้องการเชื่อมโยง apple id กับผู้ใช้ที่มีอยู่ใน back4app แพลตฟอร์ม \<font color="#2166ae">parseapple\<user\>\</font> วัตถุจะดำเนินการตามวิธีการ \<font color="#2166ae">link(user\ identitytoken\ completion )\</font> ซึ่งคุณจะต้องส่งค่าของ \<font color="#2166ae">ผู้ใช้\</font> และ \<font color="#2166ae">identitytoken\</font> จาก \<font color="#2166ae">asauthorizationappleidcredential\</font> ครีเดนเชียล 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 เรียกใช้แอป คุณสามารถไปที่ ที่เก็บนี้ https //github com/templates back4app/ios sign in with apple และดาวน์โหลดโปรเจกต์ตัวอย่าง ก่อนที่จะเรียกใช้โปรเจกต์ ให้แน่ใจว่าคุณได้ตั้งค่าโปรไฟล์การจัดเตรียมด้วยโปรไฟล์ที่เกี่ยวข้องกับบัญชีนักพัฒนาของคุณ บทสรุป ในตอนท้ายของคู่มือนี้ คุณได้เรียนรู้วิธีการลงชื่อเข้าใช้หรือเชื่อมโยงผู้ใช้ที่มีอยู่ใน back4app บน ios โดยใช้การลงชื่อเข้าใช้ด้วย apple