我无法从另一个函数加载第二个URL

时间:2016-08-14 14:56:23

标签: ios swift webview

我会尝试从3D触控快捷键加载第二个网址,但是当我尝试从另一个viewDidLoad函数中将网址加载到webview中时。

我总是收到EXC_BREAKPOINT警告。我已经尝试过,任何人都可以知道如何从AppDelegate调用的函数中将url加载到webview中。

handleShortcut:此函数从AppDelegate调用。

import UIKit

class FirstViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var loading_splash_text: UIImageView!
    @IBOutlet weak var loading_splash: UIImageView!
    //internal var urlToLoad: String?

    func didFailLoadWithError(webView: UIWebView) {
        //print("connection error");
        /*
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("ConnectionError")
        self.navigationController!.pushViewController(vc, animated: true)
         */
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = NSURL (string: "http://example.com/");
        let requestObj = NSURLRequest(URL: url!);
        webView.delegate = self
        webView.loadRequest(requestObj);

    }

    func webViewDidFinishLoad(webView: UIWebView) {
        print("did finished");
        loading_splash.hidden = true;
        loading_splash_text.hidden = true;
    }

    func webViewDidStartLoad(webView : UIWebView) {
        print("did load");
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func handleShortcut() {
        let url = NSURL (string: "http://www.example.com/");
        let requestObj = NSURLRequest(URL: url!);
        webView.loadRequest(requestObj);
    }

}

AppDelegate.swift:

import UIKit

public var urlToLoad:String?

@UIApplicationMain

class AppDelegate:UIResponder,UIApplicationDelegate {

var window: UIWindow?
var vc = FirstViewController()

@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    if(shortcutItem.type == "com..videos"){
        urlToLoad = "example.com";
    } else if(shortcutItem.type == "com..contact") {
        urlToLoad = "example.com";
    } else if(shortcutItem.type == "com..projects") {
        urlToLoad = "example.com";
    } else {
        urlToLoad = "example.com";
    }

    completionHandler(false)

    vc.handleShortcut();
    print("Shortcut: "+urlToLoad!);

0 个答案:

没有答案