「这是我参加2022初次更文应战的第5天,活动详情检查:2022初次更文应战」。
本文翻译自pub: sign_in_with_apple | Flutter Package (flutter-io.cn)
译时版别:sign_in_with_apple 3.3.0
运用Apple登录
运用 Apple 登录的 Flutter 桥接。
支撑经过 Apple ID 登录,包括从用户钥匙串中检索凭据。
支撑的渠道
- iOS
- macOS
- Android
示例用法
SignInWithAppleButton(
onPressed: () async {
final credential = await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
);
print(credential);
// Now send the credential (especially `credential.authorizationCode`) to your server to create a session
// after they have been validated with Apple (see `Integration` section for more information on how to do this)
// 现在发送凭据(特别是 `credential.authorizationCode`) 到你的服务器,在Apple( `Integration` 集成部分有更多怎么完结的信息)验证之会创立 session。
},
);
流程
集成
集成 Apple 登录不只是在 pubspec.yaml
中增加依靠,也会运用它露出出来的凭据接纳函数。
一旦收到凭据,它们需求经过 Apple 服务器验证(来保证他们有用,且的确和上面提到的用户有关),然后会从服务器得到一个 session 放在你的体系上。
你的服务器之后每天都会经过 Apple 验证 session (经过 改写Token 它能够获得初始化验证),假如授权已在 Apple 那边吊销,则也会在你的体系中吊销 session 。
条件
在你能够开始集成(或测验) Apple 登录之前,需求一个 Apple 开发者程序的付费成员 。 Apple 登录是受限的服务,关于免费的 Apple ID (源) 是不可用的。
设置
注册一个 App ID
假如你还没有 App ID, 在developer.apple.com/account/res…依照如下过程创立一个新的:
-
点击 “Register an App ID”
-
在导游中挑选 “App IDs” ,点击 “Continue”
-
设置
Description
和Bundle ID
,挑选Sign In with Apple
capability- 一般这儿 “Enable as a primary App ID” 的默认设定现已够用。假如你想要装载多个运用,全部都为你的用户同享相同的 Apple ID 凭据,请查阅 Apple 文档看一下最好的设置方法。
-
点击 “Continue”,然后点击 “Register” 完结创立 App ID。
假如你现已有了一个 App ID ,想运用它来进行 Apple 登录:
- 在列表中翻开 App ID
- 选中 “Sign in with Apple” capability
- 点击 “Save”
假如你现已改变了运用的 capability ,你需求获取更新后的条款装备文件(例如运用 Xcode )来运用新的 capability 。
创立一个 Service ID
Service ID 只用于 Web 或 Android 集成。假如你只是用于集成 iOS,能够越过此过程。
拜访你的 Apple 开发者页面,然后到”Identifiers”,然后做下面的过程:
接下来跳转到developer.apple.com/account/res…,然后做下面的过程:
-
点击 “Register an Services ID”
-
挑选 “Services IDs”, 点击 “Continue”
-
设置你的 “Description” 和 “Identifier”
- “Identifier” 之后会作为你的
clientID
被引用到。
- “Identifier” 之后会作为你的
-
点击 “Continue” 然后 “Register”
现在 Sevice 已被创立,咱们需求使其可用于 Apple 登录:
- 在 Service 列表中挑选 Service
- 选中 “Sign in with Apple” 相邻的 Box ,然后点击 “Configure”
- 在
Domains and Subdomains
里增加你想运用 Apple 登录的网站的域,例如:example.com
. 在这儿你至少需求输入一个域,即便你不想在任何网站上运用 Apple 登录。 - 在
Return URLs
box 里增加你想运用的完好的回来URL, 例如:example.com/callbacks/s… - 点击 “Next” 然后 “Done” 关闭设置对话框
- 点击 “Continue” 然后 “Save” 更新 Service
为了和 Apple 服务器通讯来验证从运用客户端接纳到的授权码,你需求在 developer.apple.com/account/res… 上创立一个 key :
-
点击 “Create a key”
-
设置 “Key Name” (例如: “Sign in with Apple key”)
-
选中 “Sign in with Apple” 相邻的 box ,然后点击同一行的 “Configure”
-
在 “Primary App ID” 下面,挑选你要运用的运用的 App ID (新创立的或者已有的)
-
点击 “Save” 离开详情视图
-
点击 “Continue” ,然后点击 “Register”
-
现在你会看一个只会显示一次的界面,你需求点击 “Download” 按钮下载 key
- 也要留意 “Key ID” 之后会用于装备服务器
现在一切都在 Apple 开发者进口设置好了,然后咱们能够开始设置服务器了。
服务器
服务器部分一般集成到已有的后台,这儿有一些现成的包用于大多数现有的编程语言和 Web 框架。
为了展现怎么创立一个完好的示例,咱们在 Glitch 上安装了一个示例工程,Glitch 供给了简略免费的支撑HTTPS的 Web API ,正是咱们这儿需求的。
要开始这个基于 Glitch 的示例,拜访工程的页面glitch.com/~flutter-si… 点击 “Remix this” 。现在你具有了属于你自己的示例服务器的副本。
首要在左侧的文件浏览器挑选 .env
文件,把它放到你的凭据里 (这些不会是公开的,只会和受邀的合作者同享) 。
之后点击左上方头像相邻的 “Share” 按钮,挑选 “Live App” 然后复制页面进口 URL (例如:https://some-random-identifier.glitch.me
)。
现在在developer.apple.com/account/res…上更新一下之前创立的 Service 来包括 Return URLs
下的URL :
https://[YOUR-PROJECT-NAME].glitch.me/callbacks/sign_in_with_apple
(替换 []
里的名称)。
这些完结之后,现在你能够进行集成 Apple 登录到你的 Flutter 运用的代码中了。
Android
从两个方法来展现增加 Apple 登录到 Flutter 运用中。 首要咱们看一下使示例运用和服务端设置合作运转,然后介绍从头开始设置运用所需的其它过程。
要在 Android 上运用此插件,你需求运用Android V2 Embedding 。
你能够检查 AndroidManifest.xml
并查找下面的元从来发现你是否现已在运用新的 embedding :
<meta-data
android:name="flutterEmbedding"
android:value="2"
/>
假如你不是运用 Android V2 Embedding,请首要运用下面的导游升级你的运用: github.com/flutter/flu…
Web
要支撑 Web ,你需求在 index.html
的 <head>
标签里增加以下脚本的导入:
<script type="text/javascript" src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
(咱们还未发现按需加载的方法,由于脚本看上去在页面加载时自己初始化了。)
然后在 Apple 开发者进口的 Service 装备中,增加托管 Domains and Subdomains
和 Returns URLs
中页面的域。
前者是需求的,你能够在Web页面上翻开流程,后者用来从弹出框向翻开的页面发送回最终凭据。(假如你省掉了这一步,流程只会静默地卡在最后一步。)
示例运用
-
在你的编辑器中翻开包下的
example
文件夹 -
运转
flutter packages get
-
翻开
lib/main.dart
,看一下SignInWithAppleButton.onPressed
回调- 设置
scopes
参数为你需求的规模,咱们能够坚持恳求名称和邮件用于测验。 - 更新传递给
WebAuthenticationOptions
构造方法的值来匹配 Apple 开发者进口的值 - 相同更新
signInWithAppleEndpoint
变量指向你的(运用?)
- 设置
-
更新了代码之后,在 Android 设备或模拟器上
flutter run
示例
你的运用
在运用的android/app/src/main/AndroidManifest.xml
里增加
<!-- Set up the Sign in with Apple activity, such that it's callable from the browser-redirect -->
<activity
android:name="com.aboutyou.dart_packages.sign_in_with_apple.SignInWithAppleCallback"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="signinwithapple" />
<data android:path="callback" />
</intent-filter>
</activity>
Apple 登录回调你的服务器(在 WebAuthenticationOptions.redirectUri
中指定)时,运用下面的 URL 来安全地重定向到你的 Android 运用:
intent://callback?${PARAMETERS FROM CALLBACK BODY}#Intent;package=YOUR.PACKAGE.IDENTIFIER;scheme=signinwithapple;end
PARAMETERS FROM CALLBACK BODY
会运用从 Apple 服务器端点接纳到的编码后(urlencoded)的内容来填充,然后 package
参数会被改为匹配你的运用的包识别ID (package identifier) (像在 Google Play 商铺发布的那样)。坚持 callback
途径 和signinwithapple
主题不动。
此外,当在客户端处理传入的凭据时,保证假如你的服务器现已验证过传入的 code
参数时,只覆写当时(客人)用户的session,以使你的运用不受到歹意传入的链接的影响(例如:当时用户登出)。
iOS
这时,你运用现已在你的运用的 capability 或为了运转示例的测验运用中增加了 Apple 登录 capability 。
假如在 XCode 中,你没有翻开 Automatically manage Signing
,你需求重新创立和下载更新后的条款装备文件用于你的运用,它们包括新的 Sign in with Apple
capability 。然后你能够下载新的凭据然后在 XCode 中挑选它们。
假如 XCode 办理你的签名,这一步会主动为你完结。只需求确认Sign in with Apple
capability 如下面示例中描述的现已激活。
此外,这假定你至少具有一个在你的开发者用户上已注册用于本地测验的 iOS 设备,这样你能够在设备上运转这个示例。
Example
-
在命令行终端翻开
example
文件夹,运转flutter packages get
-
在 XCode 中翻开
example/ios/Runner.xcworkspace
-
在
Runner
(文件浏览器侧边栏) ->Targets
->Runner
->Signing & Capabilities
设置 “Bundle Identifier” (“App ID”) 为之前在 Apple 开发者进口创立的值。- 保证 “Sign in with Apple” 在 capability 列表中 (假如没有的话,经过
+
增加)
- 保证 “Sign in with Apple” 在 capability 列表中 (假如没有的话,经过
-
现在在
example
文件夹翻开一个命令行终端,执行下面的命令:cd ios
-
bundle install
,安装用于 Cocoapods 的 Ruby 依靠 -
bundle exec pod install
,为 iOS 工程安装Cocoapods
-
在命令行终端回来到
example
文件夹的根目录,然后在测验设备上flutter run
你的运用
-
首要也是最重要的是保证你的运用具有 “Sign in with Apple” capability (
Runner
(文件浏览器侧边栏 ->Targets
->Runner
->Signing & Capabilities
),不然 Apple 登录会失利而没有任何可视化的指示(代码仍会接纳反常)。 -
集成上面展现的示例服务器或者创立你自己的后台
- 保证当验证用 Apple 服务器接纳到的
code
参数时运用的clientID
依靠于客户端: 运用在 Apple 渠道上运转的运用的 code 时运用 App ID (一些地方也称作 “Bundle ID”),运用从 Web授权流程接纳到的的 code 时运用 service ID 。
- 保证当验证用 Apple 服务器接纳到的
macOS
macOS 上的设置和 iOS 大致类似。一般用于 macOS 的 Flutter 开发,你需求在 dev
或 master
频道上。
示例
-
在命令行终端翻开
example
文件夹,运转flutter packages get
-
在 XCode 中翻开
example/macos/Runner.xcworkspace
-
在
Runner
下(文件浏览器侧边栏) ->Targets
->Runner
->Signing & Capabilities
设置 “Bundle Identifier” (“App ID”) 为之前在 Apple 开发者进口创立的值。- 保证 “Sign in with Apple” 在 capability 列表中 (假如没有的话,经过
+
增加) - 别的,这儿没有警告信息。 (例如:你的 Mac 必须在现已在本地设备注册。(假如没有,你会看到 “one click fix” 按钮用来做这个。))
- 保证 “Sign in with Apple” 在 capability 列表中 (假如没有的话,经过
-
在命令行终端回来到
example
文件夹的根目录,然后在测验设备上flutter run