上代码
import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';
//这个是iphone登录插件
//这里需要装两个插件
//ionic cordova plugin add cordova-plugin-sign-in-with-apple
//npm i @ionic-native/sign-in-with-apple
import {
SignInWithApple,
AppleSignInResponse,
AppleSignInErrorResponse,
ASAuthorizationAppleIDRequest,
SignInWithAppleOriginal
} from '@ionic-native/sign-in-with-apple';
import { from } from 'rxjs';
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
constructor(
private platform: Platform,
//开始的时候按照这种方式一直报错
// private signInWithApple: SignInWithApple
) { }
public str?: string = '8899';
//大胆一点根据vscode提示,我直接把这个值赋给了自定义的变量
private signInWithApple: any = SignInWithApple;
//这个就是存储从apple账号里获取到的一些信息,也就是下面写出的那个返回值示例
private appleSignInfo: any;
checkPlatFormIsiOS() {
//先检查当前平台是不是iphone
return this.platform.is('iphone') && !this.platform.is('mobileweb');
}
clickEvent() {
if (this.checkPlatFormIsiOS()) {
//如果是就执行
alert('i am runing');
this.str = 'i am runing';
//开始获取apple账号的信息
this.signInWithApple.signin({
requestedScopes: [
ASAuthorizationAppleIDRequest.ASAuthorizationScopeFullName,
ASAuthorizationAppleIDRequest.ASAuthorizationScopeEmail
]
//请求scope //全名和email
})
.then((res: AppleSignInResponse) => {
//请求成功
alert('Send token to apple for verification: ' + res.identityToken);
alert(JSON.stringify(res));
this.appleSignInfo = res;
// 这个是res返回值的格式
let appleLoginInfo = {
"email": "",
"authorizationCode": "",
"state": "",
"identityToken": "",
"fullName": {
"nickname": "",
"phoneticRepresentation": {},
"middleName": "",
"familyName": "",
"namePrefix": "",
"givenName": "",
"nameSuffix": "",
},
"user": ""
}
console.log(res);
})
.catch((error: AppleSignInErrorResponse) => {
//请求失败
alert(error.code + ' ' + error.localizedDescription);
console.error(error);
});
} else {
//如果不是iphone
//把页面上的提示字符改一下
this.str = 'this is not iphone';
}
}
}