I am new to Swift / iOS and am trying to use webView to be able to view a url. Searching this site and countless others I managed to be able to access google by using
var url = NSURL(string: "https://www.google.com")
This works fine with "https" but does not work if I use "http". Is the "s" a security setting? How can I view sites with a "URL scheme" of "http". Hope you can assist.
import UIKit
import WebKit
import Foundation
class ViewController: UIViewController {
@IBOutlet var containerView : UIView? = nil
var webView:WKWebView?
//instatiating the webView
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
//Loading and showing a web-page
var url = NSURL(string: "https://www.google.com")
//var url = NSURL(string: "http://www.google.com")
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
}
}
编辑 info.plist 并添加 App Transport Security Settings 并添加到 Allow Arbitrary Loads 和 Boolean = yes
just add this line in your info.plist info.plist is an XML file so you can place this code more or less anywhere inside the file. this is the configuration to allow a specific domain to use HTTP instead of HTTPS:
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Try with http://stackoverflow.com . it worked for me
var url = NSURL(string: "http://stackoverflow.com/")
//var url = NSURL(string: "http://www.google.com")//Comment this
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.