Flutter 登录苹果失败
Flutter Sign in with apple failure
我在我的应用程序上实现了使用苹果登录,由于某种原因我的服务器 return 错误导致使用苹果登录的用户名长度不符合我的条件并且我没有缓存信息用户。之后我更新服务器以删除用户名长度要求,但不幸的是用户名和电子邮件为空?我知道的解决方案是停止在 Iphone 上的设置页面中使用 apple id,还有其他代码解决方案吗?这是我的代码:
AppleSignInButton(
style: ButtonStyle.whiteOutline,
cornerRadius: 8,
type: ButtonType.signIn,
onPressed: () async {
var isConnected = await checkConnection();
if (!isConnected) {
showErrorDialog(message: ErrorMessage.NO_NETWORK);
} else {
final AuthorizationResult result = await AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
if (result != null) {
switch (result.status) {
case AuthorizationStatus.authorized:
_loginWithApple(result.credential);
break;
case AuthorizationStatus.cancelled:
break;
case AuthorizationStatus.error:
showErrorDialog(message: result.error.localizedFailureReason);
break;
}
} else {
showErrorDialog(message: result.status.toString(), onTap: (){});
}
}
},)
显然 Apple 只为首次登录提供 fullName
和 email
字段。对于后续登录,这些字段将 null
。
但是,您可以选择通过 appleIDCredential.fullName
在 didCompleteWithAuthorization
委托方法中获取全名,并自行更新用户的个人资料。
来源:https://github.com/firebase/firebase-ios-sdk/issues/4393#issuecomment-559012512
将依赖项添加到您的 pubspec.yaml 文件。
您可以从https://pub.dev/packages/apple_sign_in
获取最新版本
依赖项:
apple_sign_in: ^0.1.0
您可以从命令行安装软件包:
$ flutter pub get
或者,您的编辑器可能支持 flutter pub get like Android Studio 和 Visual Studio 代码。
导入“apple_sign_in.dart”以使用它的功能。
import 'package:apple_sign_in/apple_sign_in.dart';
在脚手架的 initState( ) 中初始化功能。
if(Platform.isIOS){
//check for ios if developing for both android & ios
AppleSignIn.onCredentialRevoked.listen((_) {
print("Credentials revoked");
});
}
放置Apple Sign in button 专门为此操作提供。
AppleSignInButton(
style: ButtonStyle.black,
type: ButtonType.continueButton,
onPressed: appleLogIn,
);
为登录功能定义 appleLogIn( ) 方法
if(await AppleSignIn.isAvailable()) {
//Check if Apple SignIn isn available for the device or not
}else{
print('Apple SignIn is not available for your device');
}
如果可用,那么我们可以请求登录
if(await AppleSignIn.isAvailable()) {
final authorizationResult result = await
AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
}
根据您的要求处理结果
if(await AppleSignIn.isAvailable()) {
final AuthorizationResult result = await AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
switch (result.status) {
case AuthorizationStatus.authorized:
print(result.user);//All the required credentials
case AuthorizationStatus.error:
print("Sign in failed: ${result.error.localizedDescription}");
break;
case AuthorizationStatus.cancelled:
print('User cancelled');
break;
}
}
您可以根据需要将凭据发送到后端。
我在我的应用程序上实现了使用苹果登录,由于某种原因我的服务器 return 错误导致使用苹果登录的用户名长度不符合我的条件并且我没有缓存信息用户。之后我更新服务器以删除用户名长度要求,但不幸的是用户名和电子邮件为空?我知道的解决方案是停止在 Iphone 上的设置页面中使用 apple id,还有其他代码解决方案吗?这是我的代码:
AppleSignInButton(
style: ButtonStyle.whiteOutline,
cornerRadius: 8,
type: ButtonType.signIn,
onPressed: () async {
var isConnected = await checkConnection();
if (!isConnected) {
showErrorDialog(message: ErrorMessage.NO_NETWORK);
} else {
final AuthorizationResult result = await AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
if (result != null) {
switch (result.status) {
case AuthorizationStatus.authorized:
_loginWithApple(result.credential);
break;
case AuthorizationStatus.cancelled:
break;
case AuthorizationStatus.error:
showErrorDialog(message: result.error.localizedFailureReason);
break;
}
} else {
showErrorDialog(message: result.status.toString(), onTap: (){});
}
}
},)
显然 Apple 只为首次登录提供 fullName
和 email
字段。对于后续登录,这些字段将 null
。
但是,您可以选择通过 appleIDCredential.fullName
在 didCompleteWithAuthorization
委托方法中获取全名,并自行更新用户的个人资料。
来源:https://github.com/firebase/firebase-ios-sdk/issues/4393#issuecomment-559012512
将依赖项添加到您的 pubspec.yaml 文件。
您可以从https://pub.dev/packages/apple_sign_in
获取最新版本
依赖项:
apple_sign_in: ^0.1.0
您可以从命令行安装软件包:
$ flutter pub get
或者,您的编辑器可能支持 flutter pub get like Android Studio 和 Visual Studio 代码。
导入“apple_sign_in.dart”以使用它的功能。
import 'package:apple_sign_in/apple_sign_in.dart';
在脚手架的 initState( ) 中初始化功能。
if(Platform.isIOS){ //check for ios if developing for both android & ios AppleSignIn.onCredentialRevoked.listen((_) { print("Credentials revoked"); }); }
放置Apple Sign in button 专门为此操作提供。
AppleSignInButton( style: ButtonStyle.black, type: ButtonType.continueButton, onPressed: appleLogIn, );
为登录功能定义 appleLogIn( ) 方法
if(await AppleSignIn.isAvailable()) { //Check if Apple SignIn isn available for the device or not }else{ print('Apple SignIn is not available for your device'); }
如果可用,那么我们可以请求登录
if(await AppleSignIn.isAvailable()) {
final authorizationResult result = await
AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
}
根据您的要求处理结果
if(await AppleSignIn.isAvailable()) {
final AuthorizationResult result = await AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
switch (result.status) {
case AuthorizationStatus.authorized:
print(result.user);//All the required credentials
case AuthorizationStatus.error:
print("Sign in failed: ${result.error.localizedDescription}");
break;
case AuthorizationStatus.cancelled:
print('User cancelled');
break;
}
}
您可以根据需要将凭据发送到后端。