Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a UIWebView interact (communicate) with the app?

I went to use UIWebView to display dynamic content, instead of doing it natively using UI-elements. Is it possible to trigger native app functions from simply hitting links inside the UIWebView? Example: hitting a link which then switches current View?

like image 589
Leonardo DaVintik Avatar asked Apr 13 '13 03:04

Leonardo DaVintik


People also ask

What is the difference between UIWebView and WKWebView?

Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.

What is UIWebView on Apple Iphone?

A view that embeds web content in your app.

Is WKWebView same as Safari?

You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features. If you just need to display a specific web page, WKWebView is the best option for this scenario.

What is Mobile Safari WebView?

WKWebView is an API that helps render web pages in iOS and macOS apps.

Is WKWebView secure?

The WKWebView is a modern API applying all the modern web security mechanisms, it's still maintained by Apple and gets updates. The good thing about WKWebView is that it does out-of-process rendering, so if the attackers find a memory corruption vulnerability in it, your application's process is still isolated.

Whats iOS WebView?

WebView can be defined as an object which can display the interactive web content and load HTML strings within the iOS application for an in-app browser. It is an instance of the WKWebView class, which inherits the UIView class.


1 Answers

Yes, it's possible. In your html, you write a JS to load a URL with a fake scheme such as

window.location = "request_for_action://anything/that/is/a/valid/url/can/go/here";

Then, in your iOS code, assign a delegate to your webView, and in your delegate, handle

webView:shouldLoadWithRequest:navigationType

with something like

if( [request.URL.scheme isEqualToString: @"request_for_action"] )
{
   // parse your custom URL to extract parameter, use URL parts or query string as you like
   return NO; // return NO, so webView won't actually try to load this fake request
}

--

Just an aside, you can do the other way, let iOS code invoke some JS codes in your html by using

NSString* returnValue = [self.webView stringByEvaluatingJavaScriptFromString: "someJSFunction()"];
like image 106
yc.lin Avatar answered Sep 21 '22 18:09

yc.lin