Sign In with Apple을 앱에 구현하고 싶다 [iOS13 Xcode11]
7529 단어 iOSiOS13Xcode11Objective-C
이 게시물은
WWDC2019도 끝나고, 예년대로 시기 iOS의 발표도 있었습니다.
여러가지 신경이 쓰이는 것은 있었습니다만, 「Sign In with Apple」로 회원계의 서비스를 촉진하고 싶다고 생각해 조사해 보았습니다.
구현 방법
※ 말하지 않아도, 아직 iOS13은 β판입니다. 앞으로의 업데이트로 뭔가 변경이 들어갈 가능성이 있고, 나의 이해가 잘못되어 있는 경우도 있으므로 참고까지 남겨주세요.
준비
※ 말하지 않아도, 아직 iOS13은 β판입니다. 앞으로의 업데이트로 뭔가 변경이 들어갈 가능성이 있고, 나의 이해가 잘못되어 있는 경우도 있으므로 참고까지 남겨주세요.
준비
이상으로 준비 완료입니다. 그리고는 실장할 뿐입니다!
코딩
버튼에 연결된 메소드로 요청을 실행합니다.
/**
* SignInWithApplenボタン押下
*/
- (IBAction)signIn:(id)sender {
ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
ASAuthorizationAppleIDRequest *request = [provider createRequest];
[request setRequestedScopes:@[ASAuthorizationScopeFullName,ASAuthorizationScopeEmail]];
ASAuthorizationController *controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
controller.delegate = self;
[controller performRequests];
}
잘하면 이런 느낌의 화면이 나옵니다.
ASAuthorize 성공, 실패 대리자 메서드 구현
/**
* SignIn成功時
*/
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(nonnull ASAuthorization *)authorization
{
ASAuthorizationAppleIDCredential *appleIDCredential = [authorization credential];
// appleIDCredentialから情報取得(初回のみ?)
NSString *user = [appleIDCredential user];
NSString *email = [appleIDCredential email];
NSString *fullName = [NSString stringWithFormat:@"%@ %@", [[appleIDCredential fullName] familyName], [[appleIDCredential fullName] givenName]];
// 二回目以降はjwt形式でエンコードされている値でのみ返すっぽい
NSData *tokenData = appleIDCredential.identityToken;
NSString *tokenDataString = [[NSString alloc] initWithData:tokenData encoding:NSUTF8StringEncoding];
NSLog(@"user:%@\nemail:%@\nfullName:%@",user, email, fullName);
// ここでサーバーに送るなどログイン処理
}
/**
* SignIn失敗時
*/
- (void) authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error
{
// エラー処理
}
이런 느낌입니까?
우선 성공시에 최소한의 처리를 썼습니다만, 에러 핸들링도 베스트 프랙티스를 알면 추기해 가겠네요.
아무래도 두 번째 이후에는 appleIDCredential에 email과 fullName이 들어 있지 않았습니다.
그래서 appleIDCredential.identityToken을 디코딩하고 꺼내야 할 것 같습니다.
감상
매우 간단. 어딘가의 귀찮은 OAuth를 기억하면 매우 간단했습니다.
굉장히 거기까지 문서를 심각하게 읽지 않았기 때문에 아직도 여러가지 해야 할 일은 있다고 생각합니다만, 필요 최저한의 데이터 취득은 이것으로 할 수 있었습니다.
이것으로 회원 증가하면 좋겠다.
Reference
이 문제에 관하여(Sign In with Apple을 앱에 구현하고 싶다 [iOS13 Xcode11]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/GoGoNaoya/items/dab5ed88ebaefd4247da
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(Sign In with Apple을 앱에 구현하고 싶다 [iOS13 Xcode11]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/GoGoNaoya/items/dab5ed88ebaefd4247da텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)