![](/img/trans.png)
[英]Will "Sign in With Apple" allow apps to be backward compatible with iOS 12 and lower?
[英]How to implement sign in with apple to iOS12 compatible project
我嘗試在支持 iOS12 的 iOS 項目中實現 Sign in With Apple。 由於 AuthenticationServices 相關聲明僅從 iOS13 有效,我需要使用#available(iOS 13.0, *)...
來分隔 iOS13 或更高版本和 iOS12 的代碼並吹。
我無法將 ASAuthorizationControllerDelegate 與 iOS12 或更低版本分開。 似乎#available(iOS 13.0, *)...
只能在方法中使用,但不能直接用於 swift 文件,所以我不能將我的 LoginViewController 與 ASAuthorizationControllerDelegate 僅用於 iOS13 或更高版本。
有什么幫助嗎?
extension LoginViewController {
@objc func didTapAppleButton() {
guard #available(iOS 13.0, *) else { return }
let provider: ASAuthorizationAppleIDProvider = .init()
let request = provider.createRequest()
request.requestedScopes = [.fullName, .email]
let authController: ASAuthorizationController = .init(authorizationRequests: [request])
authController.delegate = self
authController.presentationContextProvider = self
authController.performRequests()
}
}
// This does not work.
// -> Error: Declaration is only valid at file scope
if #available(iOS 13.0, *) {
extension LoginViewController: ASAuthorizationControllerDelegate {
public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
}
public func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
}
}
}
您需要的是@available
,而不是if #available
。 if #available
用於在特定 iOS 版本上執行特定代碼路徑。 要聲明僅在 iOS 13 上可用的特定類型或擴展,您需要@available
。
@available(iOS 13.0, *)
extension LoginViewController: ASAuthorizationControllerDelegate {
public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
}
public func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.