Flutter苹果账号登录插件sign_in_apple的使用
Flutter苹果账号登录插件sign_in_apple的使用
使用
sign_in_apple 插件用于在 Flutter 应用中实现苹果账号登录。首先,请确保在项目的 Info.plist 文件中设置 io.flutter.embedded_views_preview = true。
回调函数
SignInApple.handleAppleSignInCallBack(onCompleteWithSignIn: (String name,
String mail,
String userIdentifier,
String authorizationCode,
String identifyToken) async {
print("flutter receiveCode: \n");
print(authorizationCode);
print("flutter receiveToken \n");
print(identifyToken);
setState(() {
_name = name;
_mail = mail;
_userIdentify = userIdentifier;
_authorizationCode = authorizationCode;
});
}, onCompleteWithError: (AppleSignInErrorCode code) async {
var errorMsg = "unknown";
switch (code) {
case AppleSignInErrorCode.canceled:
errorMsg = "用户取消了请求";
break;
case AppleSignInErrorCode.failed:
errorMsg = "请求失败";
break;
case AppleSignInErrorCode.invalidResponse:
errorMsg = "请求无效响应";
break;
case AppleSignInErrorCode.notHandled:
errorMsg = "请求未处理";
break;
case AppleSignInErrorCode.unknown:
errorMsg = "未知错误";
break;
}
print(errorMsg);
});
自定义UI
手势检测器
GestureDetector(
onTap: () {
SignInApple.clickAppleSignIn();
},
child: Container(
width: 56,
height: 56,
child: Image.asset(
"images/apple_logo.png",
width: 56,
height: 56,
),
),
),
系统按钮
AppleSignInSystemButton(
width: 250,
height: 50,
buttonStyle: AppleSignInSystemButtonStyle.black,
),
开始使用
本项目是一个 Flutter 插件包,包含 Android 和/或 iOS 平台特定的实现代码。
对于 Flutter 的入门帮助,可以查看官方文档,里面有教程、示例、移动开发指南以及完整的 API 参考。
完整示例代码
以下是 main.dart 文件的完整示例代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:sign_in_apple/sign_in_apple.dart';
import 'package:sign_in_apple/apple_id_user.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _name = 'Unknown';
String _mail = 'Unknown';
String _userIdentify = 'Unknown';
String _authorizationCode = 'Unknown';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 平台消息是异步的,因此我们在异步方法中初始化。
Future<void> initPlatformState() async {
SignInApple.handleAppleSignInCallBack(onCompleteWithSignIn: (AppleIdUser user) async {
print("flutter receiveCode: \n");
print(user.authorizationCode);
print("flutter receiveToken \n");
print(user.identifyToken);
setState(() {
_name = user.name ?? ""; // 可能为 null 或 "" 如果使用隐私设置
_mail = user.mail ?? ""; // 可能为 null 或 "" 如果使用隐私设置
_userIdentify = user.userIdentifier;
_authorizationCode = user.authorizationCode;
});
}, onCompleteWithError: (AppleSignInErrorCode code) async {
var errorMsg = "unknown";
switch (code) {
case AppleSignInErrorCode.canceled:
errorMsg = "用户取消了请求";
break;
case AppleSignInErrorCode.failed:
errorMsg = "请求失败";
break;
case AppleSignInErrorCode.invalidResponse:
errorMsg = "请求无效响应";
break;
case AppleSignInErrorCode.notHandled:
errorMsg = "请求未处理";
break;
case AppleSignInErrorCode.unknown:
errorMsg = "未知错误";
break;
}
print(errorMsg);
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('用户标识符是: $_userIdentify'),
SizedBox(height: 10),
Text('姓名是: $_name'),
SizedBox(height: 10),
Text('邮箱是: $_mail'),
SizedBox(height: 10),
Text('授权码是: $_authorizationCode'),
SizedBox(height: 10),
Text('原生系统按钮:'),
SizedBox(height: 20),
AppleSignInSystemButton(
width: 250,
height: 50,
buttonStyle: AppleSignInSystemButtonStyle.black,
),
SizedBox(height: 20),
Text('自定义 Flutter 按钮:'),
SizedBox(height: 20),
GestureDetector(
onTap: () {
SignInApple.clickAppleSignIn();
},
child: Container(
width: 56,
height: 56,
child: Image.asset(
"images/apple_logo.png",
width: 56,
height: 56,
),
),
),
],
),
),
),
);
}
}
更多关于Flutter苹果账号登录插件sign_in_apple的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter苹果账号登录插件sign_in_apple的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter应用中使用sign_in_apple插件实现苹果账号登录的示例代码。这个插件允许你使用Apple的Sign In功能,让用户能够通过他们的Apple ID登录你的应用。
首先,你需要在你的pubspec.yaml文件中添加sign_in_apple依赖:
dependencies:
flutter:
sdk: flutter
sign_in_apple: ^3.3.0 # 请检查最新版本号
然后,运行flutter pub get来安装依赖。
接下来,你需要配置你的iOS项目以支持Sign In with Apple。在你的ios/Runner/Info.plist文件中,添加以下配置:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>YOUR_APP_ID</string> <!-- 替换为你的应用的App ID -->
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>apple-id-auth</string>
<string>authservices</string>
</array>
确保你已经设置了正确的App ID,并且在Apple Developer网站上启用了Sign In with Apple功能。
现在,你可以在你的Flutter代码中使用sign_in_apple插件了。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:sign_in_apple/sign_in_apple.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SignInWithApplePage(),
);
}
}
class SignInWithApplePage extends StatefulWidget {
@override
_SignInWithApplePageState createState() => _SignInWithApplePageState();
}
class _SignInWithApplePageState extends State<SignInWithApplePage> {
final SignInApple _signInApple = SignInApple(
requestedScopes: [
SignInAppleRequestedScope.fullName,
SignInAppleRequestedScope.email,
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sign In with Apple'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
try {
final result = await _signInApple.call();
if (result?.credential != null) {
final userCredential = result.credential;
// 处理用户凭证,比如发送到服务器进行验证
print('User ID: ${userCredential.user}');
print('Authorization Code: ${userCredential.authorizationCode}');
print('Real User Email: ${userCredential.email}');
print('Full Name: ${userCredential.fullName?.givenName} ${userCredential.fullName?.familyName}');
}
} catch (e) {
print(e);
}
},
child: Text('Sign In with Apple'),
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮,用户可以点击该按钮来使用Apple ID登录。SignInApple对象被配置为请求用户的全名和电子邮件地址。当用户点击按钮时,会触发登录流程,并在成功后打印用户的ID、授权码、电子邮件和全名。
请注意,这只是一个简单的示例。在实际应用中,你可能需要将授权码发送到你的服务器进行验证,并处理用户会话。此外,根据Apple的要求,如果你的应用提供其他第三方登录选项,你需要向用户展示这些选项,并且不能将Sign In with Apple作为唯一的登录方式。

