swift初心者です。
webページを表示するアプリを作っています。
ここのサイト(https://techa1008.com/rocket-note/2017/12/23/web-view-with-swift/)を参考にしているのですが、
実行しした際にエラーが起こります。ログを見てみると、webViewの領域を指定しているところで処理が止まっています。
swift
1import UIKit 2import WebKit 3 4 5class ViewController: UIViewController,WKNavigationDelegate, WKUIDelegate { 6 7 8 @IBOutlet weak var toolBar: UIToolbar! 9 10 @IBOutlet weak var btnBack: UIBarButtonItem! 11 @IBOutlet weak var btnRefresh: UIBarButtonItem! 12 @IBOutlet weak var btnForward: UIBarButtonItem! 13 //戻るボタンの処理 14 @IBAction func actBack(_ sender: UIBarButtonItem) { 15 self.webView.goBack() 16 } 17 18 //更新ボタンの処理 19 @IBAction func actRefresh(_ sender: UIBarButtonItem) { 20 self.webView.reload() 21 } 22 23 //進むボタンの処理 24 @IBAction func actForward(_ sender: UIBarButtonItem) { 25 self.webView.goForward() 26 } 27 28 var webView: WKWebView! 29 30 override func viewDidLoad() { 31 super.viewDidLoad() 32 // Do any additional setup after loading the view, typically from a nib. 33 // ステータスバーの高さを取得 34 let statusBarHeight = UIApplication.shared.statusBarFrame.size.height 35 //ツールバーの高さを取得する 36 let toolBarHeight = toolBar.frame.size.height 37 //webViewの領域を指定する 38 webView = WKWebView(frame: CGRect( x: 0, y: statusBarHeight, width: self.view.frame.width, 39 height: self.view.frame.height - toolBarHeight - statusBarHeight ), 40 configuration: WKWebViewConfiguration()) 41 //webViewをviewに追加 42 self.view.addSubview(webView) 43 //webサイトの読み込み開始時や完了時に処理を行うためのデリゲート 44 webView.navigationDelegate = self 45 //target="_blank"で設定されているリンクに遷移するためのデリゲート 46 webView.uiDelegate = self 47 //webサイトのURL 48 let myURL = URL(string: "www.apple.com/jp") 49 //webサイトの読み込み 50 let myRequest = URLRequest(url: myURL!) 51 webView.load(myRequest) 52 } 53 54 //webサイトの読み込み開始時に起動 55 func webView(_ webView:WKWebView, didFinish navigation:WKNavigation!){ 56 //インジゲータを表示する 57 UIApplication.shared.isNetworkActivityIndicatorVisible = false 58 //ボタンの有効性をチェック 59 self.btnBack.isEnabled = self.webView.canGoBack 60 self.btnForward.isEnabled = self.webView.canGoForward 61 } 62 63 //リンクタップ時の挙動 64 func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, 65 for navigationAction: WKNavigationAction, 66 windowFeatures windowfeatures: WKWindowFeatures) -> WKWebView?{ 67 //リンクの適正をチェック 68 guard let url = navigationAction.request.url else{ 69 return nil 70 } 71 //リンクがtarget = "_blank"で設定されている場合 72 guard let targetFrame = navigationAction.targetFrame, targetFrame.isMainFrame else{ 73 //サファリで開く 74 UIApplication.shared.open(url, options: [:], completionHandler: nil) 75 return nil 76 } 77 return nil 78 } 79 80 81 82 override func didReceiveMemoryWarning() { 83 super.didReceiveMemoryWarning() 84 // Dispose of any resources that can be recreated. 85 } 86 87 88 } 89
エラー内容を書いて下さい。
回答2件
あなたの回答
tips
プレビュー