Swift 3D Touch快速操作未加载请求的网址

时间:2016-02-13 19:13:35

标签: xcode swift

我是Swift的新手,并尝试使用加载默认网址的UIWebView应用程序,并可选择执行快速操作并加载其他网址。

问题是,当我请求快速操作网址时,代码会执行,但新网址未加载。所以我在某个地方遗漏了一些东西。

以下是代码:

import UIKit
import WebKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet var webView: UIWebView!

    override func loadView() {
        super.loadView()
        self.webView = UIWebView()
        self.view = self.webView!
    }

    override func viewDidLoad() {
        print("view did load")
        super.viewDidLoad()
        let url = NSURL(string: "google.com")
        let req = NSURLRequest(URL:url!)
        webView.loadRequest(req)
        webView.delegate = self
    }

    func loadUrl2() {
        loadView()
        let url = NSURL(string: "example.com")
        print(url)
        let req = NSURLRequest(URL:url!)
        self.webView!.loadRequest(req)
    }

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


}

我正在尝试并将loadView添加到loadUrl2,因为我正在

fatal error: unexpectedly found nil while unwrapping an Optional value

之前。

1 个答案:

答案 0 :(得分:1)

已修改为包含加载辅助链接:

以下是您需要对App Delegate

进行的更改和归档
enum ShortcutIdentifier: String {
        case OpenNewLink
        case OpenBetterLink

        init?(fullIdentifier: String) {
            guard let shortIdentifier = fullIdentifier.componentsSeparatedByString(".").last else {
                return nil
        }
        self.init(rawValue: shortIdentifier)
    }
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsAnnotationKey] as? UIApplicationShortcutItem {
        handleShortcut(shortcutItem)
        return false
    }
    return true
}

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    completionHandler(handleShortcut(shortcutItem))
}

private func handleShortcut(shortcutItem: UIApplicationShortcutItem) -> Bool {
    let shortcutType = shortcutItem.type
    guard let ShortcutIdentifier = ShortcutIdentifier(fullIdentifier: shortcutType) else {
        return false
    }
    return selectLinkForIdentifier(ShortcutIdentifier)
}

private func selectLinkForIdentifier(identifier: ShortcutIdentifier) -> Bool {
    guard let mainView = self.window?.rootViewController as? ViewController else {
        return false
    }

    switch identifier {
    case .OpenNewLink:
        mainView.urlString = "http://www.bing.com"
        mainView.loadWebView(mainView.urlString)
        return true

    case.OpenBetterLink:
        mainView.urlString = "http://www.duckduckgo.com"
        mainView.loadWebView(mainView.urlString)
        return true
    }
}

我还对MainVC

进行了更改
class ViewController: UIViewController, UIWebViewDelegate {

@IBOutlet var webView: UIWebView!
var urlString: String? = nil


override func viewDidLoad() {
    super.viewDidLoad()

    setUpWebView()
    webView.delegate = self
    view.addSubview(webView)
}


func setUpWebView() {
    webView = UIWebView()
    webView.frame = CGRectMake(0, 0, view.frame.width, view.frame.height)
    loadWebView(urlString)
}

func loadWebView(var urlString: String?) {
    if urlString == nil {
        urlString = "http://www.google.com"
    }

    let url = NSURL(string: urlString!)
    let req = NSURLRequest(URL:url!)
    webView.loadRequest(req)

}

}

请务必将NSAppTransportSecurity字典添加到.plist中,并将NSAllowsArbitraryLoads键设置为YES。

我测试了它,它应该适合你。