Reading HTML content from a UIWebView

The second question is actually easier to answer. Look at the stringWithContentsOfURL:encoding:error: method of NSString – it lets you pass in a URL as an instance of NSURL (which can easily be instantiated from NSString) and returns a string with the complete contents of the page at that URL. For example: NSString *googleString = @”http://www.google.com”; … 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

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

How to load local html file into UIWebView

probably it is better to use NSString and load html document as follows: Objective-C NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@”sample” ofType:@”html”]; NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; [webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]]; Swift let htmlFile = NSBundle.mainBundle().pathForResource(“fileName”, ofType: “html”) let html = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding) webView.loadHTMLString(html!, baseURL: nil) Swift 3 has … Read more

Stop UIWebView from “bouncing” vertically?

for (id subview in webView.subviews) if ([[subview class] isSubclassOfClass: [UIScrollView class]]) ((UIScrollView *)subview).bounces = NO; …seems to work fine. It’ll be accepted to App Store as well. Update: in iOS 5.x+ there’s an easier way – UIWebView has scrollView property, so your code can look like this: webView.scrollView.bounces = NO; Same goes for WKWebView.

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