Call JavaScript function from native code in WKWebView

(I filed a Radar for this shortly after asking the question here.) A new method was just added a few days ago (thanks jcesarmobile for pointing it out): Add -[WKWebView evaluateJavaScript:completionHandler:] http://trac.webkit.org/changeset/169765 The method is available in iOS 8 beta 3 and up. Here’s the new method signature: /* @abstract Evaluates the given JavaScript string. … Read more

WKWebView equivalent for UIWebView’s scalesPageToFit

you can also try the WKUserScript. here’s my working config: NSString *jScript = @”var meta = document.createElement(‘meta’); meta.setAttribute(‘name’, ‘viewport’); meta.setAttribute(‘content’, ‘width=device-width’); document.getElementsByTagName(‘head’)[0].appendChild(meta);”; WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; WKUserContentController *wkUController = [[WKUserContentController alloc] init]; [wkUController addUserScript:wkUScript]; WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; wkWebConfig.userContentController = wkUController; wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig]; you can … Read more

Request Permission for Camera and Library in iOS 10 – Info.plist

You can also request for access programmatically, which I prefer because in most cases you need to know if you took the access or not. Swift 4 update: //Camera AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in if response { //access granted } else { } } //Photos let photos = PHPhotoLibrary.authorizationStatus() if photos == .notDetermined { PHPhotoLibrary.requestAuthorization({status … Read more

WKWebView open links from certain domain in safari

You can implement WKNavigationDelegate, add the decidePolicyForNavigationAction method and check there the navigationType and requested url. I have used google.com below but you can just change it to your domain: Xcode 8.3 • Swift 3.1 or later import UIKit import WebKit class ViewController: UIViewController, WKNavigationDelegate { let webView = WKWebView() override func viewDidLoad() { super.viewDidLoad() … Read more

Getting all cookies from WKWebView

Cookies used (created) by the WKWebView are actually correctly stored in the NSHTTPCookieStorage.sharedHTTPCookieStorage(). The problem is that the WKWebView does not write back the cookies immediately. I think it does this on its own schedule. For example when a WKWebView is closed or maybe periodically. So eventually they do end up in there, but when … Read more

Why is WKWebView not opening links with target=”_blank”?

My solution is to cancel the navigation and load the request with loadRequest: again. This will be come the similar behavior like UIWebView which always open new window in the current frame. Implement the WKUIDelegate delegate and set it to _webview.uiDelegate. Then implement: – (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { if … Read more

ITMS-90809: Deprecated API Usage — Apple will stop accepting submissions of apps that use UIWebView APIs

Check if you use in your code the UIWebView class; if yes replace your implementation with WKWebView, else need check your Pods. Go with terminal into your project folder and execute the command: grep -r “UIWebView” . All matched pod must be updated. Now I’m stuck because I found UIWebView into Google AdMob (version 7.49.0) … Read more

How to detect AVplayer and get url of current video from WKWebView?

This is kind of a hack, but the only way I found to accomplish this. First set yourself as WKWebView navigation delegate: self.webView?.navigationDelegate = self Now listen to all navigation changes, and save the requested url: func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if let urlStr = navigationAction.request.url?.absoluteString { … Read more

WKWebView not loading local files under iOS 8

They finally solved the bug! Now we can use -[WKWebView loadFileURL:allowingReadAccessToURL:]. Apparently the fix was worth some seconds in WWDC 2015 video 504 Introducing Safari View Controller For iOS8 ~ iOS10 (Swift 3) As Dan Fabulish’s answer states this is a bug of WKWebView which apparently is not being solved any time soon and as … Read more