Clearing UIWebview cache

I actually think it may retain cached information when you close out the UIWebView. I’ve tried removing a UIWebView from my UIViewController, releasing it, then creating a new one. The new one remembered exactly where I was at when I went back to an address without having to reload everything (it remembered my previous UIWebView … Read more

UIWebView open links in Safari

Add this to the UIWebView delegate: (edited to check for navigation type. you could also pass through file:// requests which would be relative links) – (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if (navigationType == UIWebViewNavigationTypeLinkClicked ) { [[UIApplication sharedApplication] openURL:[request URL]]; return NO; } return YES; } Swift Version: func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, … Read more

Disabling user selection in UIWebView

Here are a few ways to disable selection: Add the following to your mobile web documents <style type=”text/css”> * { -webkit-touch-callout: none; -webkit-user-select: none; /* Disable selection/copy in UIWebView */ } </style> Programmatically load the following Javascript code: NSString * jsCallBack = @”window.getSelection().removeAllRanges();”; [webView stringByEvaluatingJavaScriptFromString:jsCallBack]; Disable the Copy / Paste user menu: – (BOOL)canPerformAction:(SEL)action withSender:(id)sender … Read more

Using HTML and Local Images Within UIWebView

Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL: NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [webView loadHTMLString:htmlString baseURL:baseURL]; You can then refer to your images like this: <img src=”https://stackoverflow.com/questions/747407/myimage.png”> (from … Read more

Change User Agent in UIWebView

Modern Swift Here’s a suggestion for Swift 3+ projects from StackOverflow users PassKit and Kheldar: UserDefaults.standard.register(defaults: [“UserAgent” : “Custom Agent”]) Source: https://stackoverflow.com/a/27330998/128579 Earlier Objective-C Answer With iOS 5 changes, I recommend the following approach, originally from this StackOverflow question: UIWebView iOS5 changing user-agent as pointed out in an answer below. In comments on that page, … Read more

How to call Objective-C from Javascript?

The standard workaround for UIWebView is to set a UIWebViewDelegate, and implement the method webView:shouldStartLoadWithRequest:navigationType:. In your JavaScript code, navigate to some fake URL that encodes the information you want to pass to your app, like, say: window.location = “fake://myApp/something_happened:param1:param2:param3”; In your delegate method, look for these fake URLs, extract the information you need, take … Read more

Can I set the cookies to be used by a WKWebView?

Edit for iOS 11+ only Use WKHTTPCookieStore: let cookie = HTTPCookie(properties: [ .domain: “example.com”, .path: “https://stackoverflow.com/”, .name: “MyCookieName”, .value: “MyCookieValue”, .secure: “TRUE”, .expires: NSDate(timeIntervalSinceNow: 31556926) ])! webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie) Since you are pulling them over from HTTPCookeStorage, you can do this: let cookies = HTTPCookieStorage.shared.cookies ?? [] for cookie in cookies { webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie) } Old answer for … Read more