苹果版赤潮可以用微信登陆吗
Let’s recreate “Sign in with Apple” Button for our app in SwiftUI. Before that, let us get some terms out of the way
让我们在SwiftUI中为我们的应用程序重新创建“使用Apple登录”按钮。 在此之前,让我们摆脱一些限制
“SwiftUI is an innovative, exceptionally simple way to build user interfaces across all Apple platforms with the power of Swift. Build user interfaces for any Apple device using just one set of tools and APIs.” — Apple’s official documentation
“ SwiftUI是一种创新的,非常简单的方法,可以借助Swift跨所有Apple平台构建用户界面。 仅使用一组工具和API为任何Apple设备构建用户界面。” — 苹果官方文档
The best part is you can integrate your views with components from the UIKit, AppKit, and WatchKit frameworks. In short, SwiftUI has made building user interfaces straightforward.
最好的部分是您可以将视图与UIKit,AppKit和WatchKit框架中的组件集成在一起。 简而言之,SwiftUI使构建用户界面变得简单明了。
“Sign In with Apple is the fast, easy, and more private way to sign into apps and websites using the Apple ID that you already have.” — Apple’s official documentation
“使用Apple登录是一种使用您已经拥有的Apple ID登录应用和网站的快速,简便,私密的方式。” — 苹果官方文档
为什么使用Apple登录很重要? (Why Sign In with Apple is Important?)
- Apple would soon make every developer use Sign in with Apple mandatory. In sum, you don’t have another option. Apple很快将使每个开发人员都必须使用Apple登录。 总之,您没有其他选择。
- Sign in with Apple is built from the ground up to respect user’s privacy and keep them in control of their personal information. 使用Apple登录是从头开始的,旨在尊重用户的隐私并控制他们的个人信息。
- Instead of using a social media account, or filling out forms and choosing another new password, just tap the Sign in with Apple button, review your information, and sign in quickly and securely with Face ID, Touch ID, or the device passcode. 无需使用社交媒体帐户或填写表格并选择其他新密码,只需点击“使用Apple登录”按钮,查看您的信息,然后使用Face ID,Touch ID或设备密码快速安全地登录即可。
Sign in with Apple does not track or profile users as they favorite apps and websites, and Apple retains only the information that’s needed to make sure they can sign in and manage your account.
使用Apple登录不会跟踪用户喜欢的应用程序和网站,也不会对他们进行配置,Apple仅保留确保他们可以登录和管理您的帐户所需的信息。
Without further wait, let us get on how you could implement Sign In with Apple in SwiftUI. Before you begin, you need to add “Sign In with Apple” Capability in Xcode. To do that,
不用再等待了,让我们开始了解如何在SwiftUI中使用Apple实现登录。 在开始之前,您需要在Xcode中添加“使用Apple登录”功能。 要做到这一点,
- Click on your project 点击你的项目
- Go to Signing and Capabilities 转到签名和功能
- Click “+” sign and search for “Sign In with Apple” and select it. 单击“ +”号,然后搜索“使用Apple登录”并选择它。
That’s it, we are ready to implement Sign In With Apple. Now lets’ look at the code.
就是这样,我们已经准备好实现与Apple一起登录。 现在让我们看一下代码。
SwiftUI does not provide an AppleIdButton so let’s just start there. We would need to wrap one up ourselves. Here, we are simply wrapping ASAuthorizationAppleIdButton, so we could use it in SwiftUI
SwiftUI没有提供AppleIdButton,所以让我们从这里开始。 我们需要自己包一个。 在这里,我们只是包装了ASAuthorizationAppleIdButton,所以我们可以在SwiftUI中使用它
struct AppleIdButton: UIViewRepresentable {
func makeUIView(context: Context) -> ASAuthorizationAppleIDButton {
ASAuthorizationAppleIDButton()
}
func updateUIView(_ uiView: ASAuthorizationAppleIDButton, context: Context) {
}
}
At this point, in your ContentView, you should be able to render the official Apple Button like this,
此时,在ContentView中,您应该能够像这样渲染官方Apple Button,
Button (action: {}) {
AppleIdButton()
}//May be style it a little AppleIdButton().background(Color.primary).clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous)).padding(7).frame(width: UIScreen.main.bounds.width - 100, height: 65)
Let’s go back to AuthenticationService. Make sure you have AuthenticationServices imported. It provides a framework for handling Authorization requests. Next, we need to work on the Coordinator. This coordinator provides a mechanism to request Apple to register a new user, or sign in an existing user.
让我们回到AuthenticationService。 确保已导入AuthenticationServices。 它提供了一个处理授权请求的框架。 接下来,我们需要协调员。 该协调器提供了一种机制,可以请求Apple注册新用户或登录现有用户。
Coordinator to help the create request to Apple 协调员,以帮助向Apple创建请求Lets look at this code closely:
让我们仔细看一下这段代码:
- First, we create a request with AppleIDProvider and we request the user’s full name and email. One thing to note here is that this is optional and you can choose if you want to store this data or not. 首先,我们使用AppleIDProvider创建一个请求,并请求用户的全名和电子邮件。 这里要注意的一件事是这是可选的,您可以选择是否要存储此数据。
- Next, we initialize the ASAuthorizationController with the request we build in the first step and execute our request for user registration or sign in. 接下来,我们使用第一步中构建的请求初始化ASAuthorizationController,并执行我们的用户注册或登录请求。
- We need access to ASAuthorizationControllerDelegate protocol to be able to have access to delegate function to fetch verification (success or failure here) 我们需要访问ASAuthorizationControllerDelegate协议才能访问委托函数以获取验证(此处成功或失败)
didCompleteWithAuthorization: Once the user is successfully signed in, it checks is the credential is ASAuthorizationAppleIdCredential. If yes, this function returns the name and email of the user. While you should be storing this is in the Keychain (more secure), we are just storing it in UserDefaults.
didCompleteWithAuthorization:成功登录用户后,它将检查凭据是否为ASAuthorizationAppleIdCredential。 如果是,则此函数返回用户的名称和电子邮件。 当您将其存储在钥匙串中(更安全)时,我们只是将其存储在UserDefaults中。
didCompleteWithError: It is important to handle this case, where the sign-in fails and we return a localized description of the error.
didCompleteWithError:处理这种情况很重要,因为这种情况登录失败,并且我们返回错误的本地化描述。
If you noticed that we have saved the user in our UserDefault. Therefore, before getting here, we should check if the user is already signed in. This will keep user logged in until they choose to sign out manually. To achieve this, we will write a wrapper on top of getAppleRequest() to check if the user is already logged in.
如果您注意到我们已将用户保存在UserDefault中。 因此,在到达此处之前,我们应该检查用户是否已经登录。这将使用户保持登录状态,直到他们选择手动注销为止。 为此,我们将在getAppleRequest()顶部编写一个包装器,以检查用户是否已经登录。
func getUserInfo() {if let userData = UserDefaults.standard.object(forKey: "user") as? Data,let userDecoded = try? JSONDecoder().decode(User.self, from: userData) {
print("UserData: \(userDecoded)")
user = userDecoded
}
}
Let’s clean up our code a little by creating a ViewModel that our ContentView can communicate with:
让我们通过创建一个我们的View可以与之通信的ViewModel来稍微整理一下代码:
final class AppleViewModel: ObservableObject {
var signInWithApple = SignInWithAppleCoordinator()
@Published var user: User?
func getRequest() {
signInWithApple.getApppleRequest()
}
func getUserInfo() {
if let userData = UserDefaults.standard.object(forKey: "user") as? Data,
let userDecoded = try? JSONDecoder().decode(User.self, from: userData) {
print("UserData: \(userDecoded)")
user = userDecoded
}
}
}
Finally, from our View, we create an AppleIdButton. With this, you should be able to route the user to your app if the user is already logged in.
最后,从视图中创建一个AppleIdButton。 这样,如果用户已经登录,则应该能够将用户路由到您的应用。
struct Login: View {
@ObservedObject var appleView = AppleViewModel()
var body: some View {
VStack {
if appleView.user != nil {
//User Logged In
} else {
Button(action: {
self.appleView.getRequest()
}){
AppleIdButton()
.background(Color.primary).clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous)).padding(7)
.frame(width: UIScreen.main.bounds.width - 100, height: 65)
}
}.onAppear( perform: {
self.appleView.getUserInfo()
})
}
}
That’s it! Now you have “Sign In with Apple” built into your app.
而已! 现在,您的应用程序已内置“使用Apple登录”。
Thanks for reading!
谢谢阅读!
翻译自: https://medium.com/swlh/sign-in-with-apple-in-swiftui-1b1d3e62850b
苹果版赤潮可以用微信登陆吗