The early stage of the work

1. Go to the Apple developer website and open the sign in with Apple permission under the corresponding APP ID. This is similar to push. The following figure

Note: The editing option on the right is the grouping function. If groups are set, you can use it.

2. Add response permission function Settings in the project project (similar to push)

  1. Select the Singing & Capability option, but only certificate Settings are displayed.
  2. One line will tell you to add capablities by clicking the “+” button above. That is, add the + sign can add the required permissions.
  3. Select the corresponding function option. By this point, all the preliminary functions have been completed. To delete it, click the x on the right

Code integration

  1. Import header file
#import <AuthenticationServices/AuthenticationServices.h>
Copy the code
  1. Follow the agent
<ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding>
Copy the code
  • ASAuthorizationControllerDelegate processing data correction
  • ASAuthorizationControllerPresentationContextProviding set context, the management view pop-up in where
  1. Initialization button
-(void)configUI{
    if(@ the available (iOS 13.0. *)) {self. AuthorizationButton = [[ASAuthorizationAppleIDButton alloc] init]; [self.authorizationButton addTarget:self action:@selector(click)forControlEvents:(UIControlEventTouchUpInside)];
        self.authorizationButton.center = self.view.center;
        [self.view addSubview:self.authorizationButton];
    } else{/ / Fallback on earlier versions}} - (void) click API_AVAILABLE (ios (13.0)) {ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc]init]; ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest]; request.requestedScopes = @[ASAuthorizationScopeFullName,ASAuthorizationScopeEmail]; ASAuthorizationController *auth = [[ASAuthorizationController alloc]initWithAuthorizationRequests:@[request]]; auth.delegate = self; auth.presentationContextProvider = self; [auth performRequests]; }Copy the code
  1. Process proxy callback data
Where / / / agent is mainly used for show - (ASPresentationAnchor) presentationAnchorForAuthorizationController: (ASAuthorizationController *) controller API_AVAILABLE (ios (13.0)) {returnself.view.window; } -(void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *) authorization API_AVAILABLE (ios (13.0)) {if([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]){ ASAuthorizationAppleIDCredential *apple = authorization.credential; NSString *userIdentifier = apple.user; NSPersonNameComponents *fullName = apple.fullName; NSString *email = apple.email; NSData *identityToken = apple.identityToken; NSLog(@"% @ % @ % @ % @",userIdentifier,fullName,email,identityToken);
        }else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]){
            
            //// Sign inusing an existing iCloud Keychain credential. ASPasswordCredential *pass = authorization.credential; NSString *username = pass.user; NSString *passw = pass.password; }} / / / callback failed - (void) authorizationController: (ASAuthorizationController *) controller didCompleteWithError error: (NSError *) API_AVAILABLE (ios (13.0)) {NSLog (@"% @",error);
}

Copy the code
  1. That’s the general process