2019年10月3日
为您的 Corona 应用添加应用登录
感谢插件大师 Scott Harrison 为我们撰写这篇关于在您的应用中使用 Google 和 Apple 登录的客座文章。
许多应用和游戏都有自己的登录系统。无论您是需要登录来存储数据、共享还是进行多人游戏,设置登录系统都是应用的重要组成部分。虽然大多数应用通常使用电子邮件和密码系统,但您应该考虑使用第三方登录系统。第三方系统允许用户更快地登录,并避免用户创建另一个密码。本教程将向您展示如何集成 Google 和 Apple 登录。
Google 登录
首先,请访问 Corona Labs 市场并激活Google 登录插件。接下来,您需要获取 Android 和 iOS 的客户端 ID。请转到Google 控制台并启用“Identity Toolkit API”。然后在“凭据”部分中创建客户端 ID。为 iOS 和 Web(用于 Android)设置客户端 ID。
另请确保将您的 iOS URL 方案添加到您的 build.settings
中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
settings = { android = { useGoogleServicesJson = true, usesPermissions = { "android.permission.INTERNET" }, }, iphone = { plist = { CFBundleURLTypes = { { CFBundleURLSchemes = { "com.googleusercontent.apps.REPLACE_WITH_YOUR_URL_SCHEME", } } }, }, }, plugins = { ["plugin.firestore"] = { publisherId = "tech.scotth", }, }, } |
注意:如果您正在使用 Firebase,只需从 GoogleService-Info.plist
文件中的 CLIENT_ID
键下获取 iOS 客户端 ID,对于 Android,请转到 google-services.json
中的 client >oauth_client>client_id
键下。
接下来,使用下面的代码片段根据您正在使用的平台插入客户端 ID。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
local googleSignIn = require( "plugin.googleSignIn" ) local json = require( "json" ) googleSignIn.init() local androidClientID = "您的 Android 客户端 ID" local clientID = "您的 iOS 客户端 ID" -- iOS 默认值 if ( system.getInfo( "platform" ) == "android" ) then clientID = androidClientID end googleSignIn.signIn( clientID, nil, nil, function ( event ) print( json.encode( event ) ) end ) |
Apple 登录
Apple 在 iOS 13+ 中引入了一种新的登录方式。如果应用开发者使用其他第三方登录选项(如 Google 或 Facebook),则必须添加 Apple 登录。开发者可以轻松地为 iOS 设备实施 Apple 登录。要进行设置,您只需在 Corona 市场中激活免费的Apple 登录插件。确保您的应用 ID 已设置Apple 登录权利。这需要在添加权利后生成并下载新的配置文件。
将 Apple 登录权利添加到 build.settings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
iphone = { plist = { -- 将权利放在 plist 外部 }, entitlements = { ["com.apple.developer.applesignin"] = {"Default"}, } }, plugins = { ["plugin.appleSignIn"] = { publisherId = "tech.scotth", }, }, |
然后只需在您的应用程序中使用 Apple Sign In 插件,即可在您的 iOS 13+ 设备上登录。
1 2 3 4 5 6 7 |
local appleSignIn = require( "plugin.appleSignIn" ) local json = require( "json" ) appleSignIn.show( "name", function( event ) print( json.encode( event ) ) end ) |
注意:appleSignIn.show()
中的第一个值可以是 “name
“,它将获取用户的姓名; “email
“,它将获取用户的电子邮件;或者 “nameAndEmail
“,它将获取电子邮件和姓名。
请在文档中阅读有关 Apple Sign In 插件的更多信息。
抱歉,评论表单目前已关闭。