2016-02-13 67 views
0

我是新来的Swift,并试图用UIWebView应用程序加载默认网址,与执行快速操作和加载不同的网址的选项。Swift 3D Touch快速操作不加载请求的网址

问题是当我请求快速动作url时,代码执行但新的url没有加载。所以我在某处流失了一些东西。

下面是代码:

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

之前

+0

在哪一行你会得到'致命错误:意外地发现零,而解包可选值'? – Alex

+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。

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

+0

谢谢。这确实有效。我有它在这个状态下工作。我的问题是如果我点击Quick Action,loadUrl2就是不同的网址。 –

+0

您的实际应用程序中的主视图控制器上的webView? –

+0

是的,它在主视图 –