Reputation: 213
I'm getting really frustrated with this, hope anyone who experienced with similar issues can help. Current project using Swift 2, iOS9 and XCode 7.2. I have ViewConroller and trying to add a UIWebView or a WKWebview as a subview, however, UIWebView crashes the app, while my WKWebview is not loading URL, and none of the WKNavigationDelegate methods get called.
``
import UIKit
import WebKit
class TestViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var placeholderView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let webV:UIWebView = UIWebView(frame: CGRectMake(0, 0, placeholderView.bounds.width, placeholderView.bounds.height))
webV.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com")))
webV.delegate = self;
self.view.addSubview(webV)
}
}
``
``
import UIKit
import WebKit
class TestViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var placeholderView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let preferences = WKPreferences()
preferences.javaScriptEnabled = false
let webcon = WKWebViewConfiguration()
webcon.preferences = preferences
let webView: WKWebView = WKWebView(frame: CGRectMake(0, 0, placeholderView.bounds.width, placeholderView.bounds.height), configuration: webcon)
webView.navigationDelegate = self
let url = NSURL(string: "https://www.google.com")!
placeholderView.addSubview(webView)
placeholderView.bringSubviewToFront(webView)
webView.loadRequest(NSURLRequest(URL: url))
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
NSLog("Webview started Loading")
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
NSLog("%@", navigationAction.request.URL!)
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("\nWebview did finish load \(webView.request)", terminator: "")
}
}
``
I already have imported WebKit into frame work, and added app allows arbitrary loads into my info.plist.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Upvotes: 0
Views: 4851
Reputation: 65
Hmm, just try to change your webView declaration.. declare your webView first in your class by var webView: WKWebView
(don't use let
), and then put self.webView = WKWebView(frame: CGRectMake(0, 0, placeholderView.bounds.width, placeholderView.bounds.height), configuration: webcon)
in viewDidLoad()
Upvotes: 1
Reputation: 66224
The reason UIWebView
is crashing is almost certainly because delegate
is set to an object that doesn't exist.
I'm guessing the WKWebView
delegate methods aren't called because you haven't set the navigationDelegate
property (or you did, but the object you set it to was deallocated.)
Upvotes: 1