Bashar Abdullah Bashar Abdullah - 5 months ago 32
Swift Question

Swift 3D Touch Quick Action not loading requested url

I'm new to Swift, and trying my hands with UIWebView app that loads default url, with option to perform quick action and load a different url.

Problem is when I request the quick action url, code executes but the new url is not loading. So I'm missing something in the flow somewhere.

Here is the code:

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.
}


}


I was experimenting and added loadView to loadUrl2, as I was getting

fatal error: unexpectedly found nil while unwrapping an Optional value


before that.

Answer

Edited to Include loading secondary link:

Here are the changes and files you'll need to make to the 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
    }
}

I also made changes in the 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)

}

}

Be sure to add NSAppTransportSecurity dictionary to your .plist and add NSAllowsArbitraryLoads key set to YES.

I tested it and it should work for you.