2016-05-06 29 views
0

私は本質的にwebviewであるアプリケーションを作成しています。これは、通常のWebアプリケーションでは使用できないカスタムフォントを使用するため、アプリラッパーになります。WKWebViewデリゲートの問題(Xcode 7.3 Swiftを使用)

ほとんどの目的でうまくいきますが、URLリクエストが正しく読み込まれているかどうかを確認して、「ページが見つかりません」というメッセージを送信したかったり、このため私は代議員が必要だと信じています。

問題:デリゲートを作成しようとするたびに(例で見たようにいくつかの方法を試しても)、アプリケーションは負荷がかかってクラッシュします。

(ちなみに、私がストーリーボードではなくプログラムでwebviewを作成すると、カスタムフォントがロードされません。私の目的は主な要件の失敗です。 )

コード:アドバイス

import UIKit 
import WebKit 

class ViewController: UIViewController, WKNavigationDelegate { 

@IBOutlet var containerView: UIView! 
var screenEdgeRecognizer: UIScreenEdgePanGestureRecognizer! 
//var currentRadius:CGFloat = 0.0 

@IBOutlet var webView: WKWebView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    screenEdgeRecognizer = UIScreenEdgePanGestureRecognizer(target: self, 
     action: #selector(ViewController.goBack(_:))) 
    screenEdgeRecognizer.edges = .Left 
    view.addGestureRecognizer(screenEdgeRecognizer) 

    // *** This line commented out to prevent app from crashing! 
    //self.webView.navigationDelegate = self 

    webView.hidden = false; 
    // this allows the UIWebView box to your background color seamlessly. 
    webView.opaque = false; 

    // Do any additional setup after loading the view, typically from a nib. 
    let url = NSURL (string: "http://localhost/web_app/mobile_login.php"); 

    let requestObj = NSURLRequest(URL: url!); 

    //let localfilePath = NSBundle.mainBundle().URLForResource("home", withExtension: "html"); 
    //let requestObj = NSURLRequest(URL: localfilePath!); 
    webView.loadRequest(requestObj); 
    //webView.allowsBackForwardNavigationGestures = true; 

} 

// **** This code I hope to use but I think requires a delegate in order to work 
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) { 
    let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .Alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil)) 
    presentViewController(alert, animated: true, completion: nil) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

override func preferredStatusBarStyle() -> UIStatusBarStyle { 

    //LightContent 
    return UIStatusBarStyle.LightContent 

    //Default 
    //return UIStatusBarStyle.Default 

} 

func goBack(sender: UIScreenEdgePanGestureRecognizer) { 
    // 

    self.webView.goBack() 

} 

}

感謝します。

+0

// *** – WCWC

答えて

1

私が知る限り、Interface BuilderでWKWebViewをまだ作成することはできません。私は、プログラムで作成しなければならないと信じています。あなたはストーリーボードのUIWebViewを使用していて、コンセントをWKWebViewとしてセットアップしようとしていますか?もしそうなら、それがクラッシュする理由です。それらはまったく異なるオブジェクトです。

ストーリーボードでビルドする必要がある場合は、UIWebViewを使用できます。しかし、なぜWKWebViewがプログラムで作成されたときに異なった動作をするのか分かりません(最初はIBで作成できません)。

+0

でコメントされているビットです。私はこのプロジェクトをUIwebviewでやり直してきました。誰かの例に基づいてコードをWKwebviewに変更して、デリゲートを追加しようとするまで機能しました。これを指摘してくれてありがとう。 – WCWC

0

上記のマイクコールの答えは、私の問題が何であったかの中心にあります。しかし、さらに調査したFYIでも、カスタムフォントとWKWebViewsでそれらを使用するのに苦労しているこのquestionを発見しました。

関連する問題