I'm working on an app that must support Apple Sign In with iOS 10.0+. For the latest iOS I can use AuthenticationServices native library. That's fine, however, for iOS 10-12 we would like to use WKWebView to handle the authentication embedded in app to get the token and email (when possible) after the auth is complete.
I'm following their official guide from apple: https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/incorporating_sign_in_with_apple_into_other_platforms
The problem now is that when I configure the redirect_uri, how do I get token back from the WKWebView after auth completes successfully? I have "tried" to intercept the response and get the token via the WKWebKit's WKNavigationDelegate navigationResponse response body but to no avail. I'm missing a key information.
public func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
private func signInUsingWebAuthenticationSession() {
let queryItems = [
URLQueryItem(name: "client_id", value: "com.devapp.app"),
URLQueryItem(name: "redirect_uri", value: "https://dev.devapp.com/redirect"),
URLQueryItem(name: "response_type", value: "code id_token"), // Or code
URLQueryItem(name: "scope", value: "name email"), // Retrieve name and email
URLQueryItem(name: "response_mode", value: "form_post")
]
var urlComps = URLComponents(string: "https://appleid.apple.com/auth/authorize")!
urlComps.queryItems = queryItems
guard let authURL = urlComps.url else {
return
}
/// ... Load this url in WKWebView
}
I hope there are some smart developers out there who has encountered/solved this issue before and are happy to share their knowledge. Cheers!
P.S If there are other solution that would work, feel free to comment