Reputation: 560
I'm working on a project where I want to load a internet URL
to my UIWebView
I've managed to load a locally stored html file like this:
if let url = NSBundle.mainBundle().URLForResource("index", withExtension: "html") {
webView.loadRequest(NSURLRequest(URL: url))
}
So based on that I tried doing it like this:
if let url = NSURL(string: "http://google.com") {
webView.loadRequest(NSURLRequest(URL: url))
}
But that didn't seem to work.
Upvotes: 1
Views: 146
Reputation: 149
Try below code :
let url = "http://google.com"
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
WebView.loadRequest(request)
Upvotes: 0
Reputation: 1465
As joern says,the security update requires all requests to be via HTTPS if you don't want to override it.
So the basic quick fix to this would be just changing the link to https.
So something like this:
if let url = NSURL(string: "https://google.com") {
webView.loadRequest(NSURLRequest(URL: url))
}
Upvotes: 1
Reputation: 27620
Apple introduced App Transport Security with iOS9. That is a new security feature that enforces certain security practices when working with web requests. For example it won't allow to send requests via HTTP, because it only allows HTTPS requests. The good news is that you can override these security requirements by adding this to your project's Info.plist file:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Just be aware that this circumvents ALL security requirements that came with ATS. You should use this in production only if there really is no other way. If you only access 1 non HTTP url you can disable ATS for that 1 domain only:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourdomain.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
Upvotes: 2