iOS
...
Users
iOS 앱에 Apple 로그인 도입 - ParseSwift 사용법
12 분
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 프로젝트가 연결된 후, apple로 로그인 기능을 추가합니다 이를 위해 프로젝트 탐색기에서 프로젝트를 선택하고 타겟 섹션으로 이동합니다 주요 타겟을 선택하고 서명 및 기능 탭으로 이동한 후, + 기능 버튼을 클릭하고 apple로 로그인 기능을 추가합니다 ios 앱에서 sign in with apple 방법을 통합하기 위해 필요한 유일한 구성입니다 2 parseswift와 함께 authenticationservices 프레임워크 사용하기 apple로 로그인 흐름은 세 단계로 완료할 수 있습니다 하지만 먼저, sign in with 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 앱이 back4app 플랫폼의 기존 사용자와 apple id를 연결해야 하는 경우, 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 사용자를 연결하는 방법을 배웠습니다