简体   繁体   English

在本机 iOS 应用程序中从 web 视图进行身份验证?

[英]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.

相关问题 从本机iOS应用程序实施Web登录 - Implement Web Login from Native iOS App 将数据从Web视图传递到本机应用程序的最佳方法(Windows,iOS和Android) - Best approach to pass data from web view to native app (Windows, iOS & Android) iOS网络应用作为原生应用 - iOS web app as native app 如何从 Web 应用程序打开本机 iOS 应用程序 - How to open a native iOS app from a web app 从打包为原生iOS应用的Web应用打开外部链接 - Open external link from web app packaged as native iOS app ios原生应用程序可以从Web保存数据? - ios native app allowed to save data from web? 从iOS / Android上的web-app调用本机日期选择器 - Invoke native date picker from web-app on iOS/Android 在iOS上打开时停止本机Web应用程序重新加载 - Stop native web app from reloading itself upon opening on iOS Expo react-native app with firebase phone authentication works on web, error on ios simulator and crashes with no warning on Android - Expo react-native app with firebase phone authentication works on web, error on ios simulator and crashes with no warning on Android 在混合应用程序中从 react native 视图导航到 ios 视图 - Navigate from react native view to ios views in a hybrid app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM