[英]Authentication from web view in a native iOS app?
I have a web backend server that handles web site (html) authentication from email, facebook, google....我有一个 web 后端服务器,它处理来自 email、Z26CAE7718C32180A7A9 的 web 站点 (html) 身份验证...
Is it a possible option to load the authentication web form into a native web view, intercept the authentication response by native app, and store login state into native app, so the native app will be able then to send REST authorized queries to the backend server? Is it a possible option to load the authentication web form into a native web view, intercept the authentication response by native app, and store login state into native app, so the native app will be able then to send REST authorized queries to the backend server ?
Is this scenario possible?这种情况可能吗?
Yes it is possible using WKWebView.是的,可以使用 WKWebView。 It has navigation delegate API
它有导航委托 API
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
Using the above API, you can check for access token which is coming via cookie.使用上面的 API,您可以检查来自 cookie 的访问令牌。
Objective-c Objective-c
WKWebsiteDataStore *dataStore = webView.configuration.websiteDataStore;
WKHTTPCookieStore *cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray<NSHTTPCookie*> *cookieArray) {
NSString *cookieValue = nil;
for (NSHTTPCookie *cookie in cookieArray) {
if ([cookie.name isEqualToString:@"access_token"]) {
NSString *accessToken = cookie.value;
}
}
}];
Swift Swift
let dataStore = webView.configuration.websiteDataStore
let cookieStore = dataStore.httpCookieStore
cookieStore.getAllCookies({ cookieArray in
let cookieValue: String? = nil
for cookie in cookieArray ?? [] {
if (cookie.name == "access_token") {
let accessToken = cookie.value
}
}
}) })
This is what I'm doing in our project.这就是我在我们的项目中所做的。 Hope this helps you.
希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.