swiftで簡単なwebviewを作る手順を記載
ソースコード
var webview: UIWebView = UIWebView()
override func viewDidLoad() {
super.viewDidLoad()
self.webview.frame = self.view.bounds
self.webview.delegate = self;
self.view.addSubview(self.webview)
let url:NSURL = NSURL( string: "http://www.google.co.jp" )!
let urlRequest: NSURLRequest = NSURLRequest(URL: url)
self.webview.loadRequest(urlRequest)
}
で、このままだとurlがhttpの場合は以下のようにセキュリティエラーで怒られます
(IOS9から?)
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
なので、AppTransportSecurityを無効にしてやるか、例外のドメインを追加してやる必要があります。
・AppTransportSecurityを無効にする
1.Info.plistを開く
2.「App Transport Security Settings」を「Dictionary」で追加
3.「Allow Arbitrary Loads」を「Boolean」で追加、valueを「YES」に設定
・例外ドメインの追加
1.Info.plistを開く
2.「App Transport Security Settings」を「Dictionary」で追加
3.「Exception Domains」を「Dictionary」で追加
4.「www.google.co.jp」などのドメインを「Dictionary」で追加
5.「NSTemporaryExceptionAllowsInsecureHTTPLoads」を「Boolean」で追加、valueを「YES」に設定
6.「NSIncludesSubdomains」を「Boolean」で追加、valueを「YES」に設定
これで表示されます
以上