2016-02-13 68 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

是的,它在主視圖 –