How can I enable zoom in on UIWebView which inside the UIScrollView?
You MUST set scalesPageToFit=YES for any pinching and zooming to work on a UIWebView
You MUST set scalesPageToFit=YES for any pinching and zooming to work on 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
Also set : [webView setBackgroundColor:[UIColor clearColor]]; [webView setOpaque:NO];
If you’re using iOS >= 6 and you have mountain lion (10.8) or Safari >= 6, you can just: Open the application in the simulator (or your device in XCode >= 4.5.x). Open Safari (go to Preferences -> Advanced and make sure “Show Develop Menu in Menubar” is on. From the Menu-bar (of Safari) select … Read more
For those who just scroll down to find the answer: – (void)webViewDidFinishLoad:(UIWebView *)webView{ NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@”document.title”]; } This will always work as there is no way to turn off Javascript in 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
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
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
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.
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