0

In my Browser app I want to make the URL Scheme of my app (myapp://) to open URLs that come after the myapp:// part.

I want to have a user use myapp://http://google.com and it will open Google.

I set up the URL scheme itself in the app's Info.plist file, and it works (using myapp:// in Safari launches the app). I set up my App Delegate with this function from this question:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    if let string = url.absoluteString {
        let value = string[string.endIndex ..< string.endIndex]
            NSUserDefaults.standardUserDefaults().setObject(value, forKey: AppDefaultKeys.URLSchemeRecievedURL.rawValue)
            return true
    }
    return false
}

then, I set up my main ViewController (where the view resides) with this code in the viewDidLoad function:

var _urlSchemeRecievedURL = NSUserDefaults.standardUserDefaults().stringForKey(AppDefaultKeys.URLSchemeRecievedURL.rawValue)

if _urlSchemeRecievedURL != nil {
        loadURL(_urlSchemeRecievedURL!)
    }

This is the code for the loadURL function:

func loadURL(urlString: String) {
    let addr = NSURL(string: urlString)
    if let webAddr = addr {
        let req = NSURLRequest(URL: webAddr)
        _webView!.loadRequest(req)
    } else {
        displayLoadingErrorMessage()
    }

}

I used NSUserDefaults since it's the way only way I currently know how to pass data from AppDelegate to my ViewController.

Something isn't working though, since when I use myapp://http://google.com in Safari it only launches the app (without google.com being loaded).

I also tried using this code:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    if let string = url.absoluteString {
        if let range = string.rangeOfString("q=") {
            let value = string[range.endIndex ..< string.endIndex]
            NSUserDefaults.standardUserDefaults().setObject(value, forKey: AppDefaultKeys.URLSchemeRecievedURL.rawValue)
            return true
        }
    }
    return false
}

With myapp://?q=http://google.com but it doesn't work too.

UPDATE:

Using the code with the ?q= query works, but only on the next launch. And after the next launch, the app always launches with did URL (since it's saved in NSUserDefaults).

So the question now is, How can I pass the URL from the AppDelegate to ViewController without NSUserDefaults?

Thanks,

PastaCoder

Community
  • 1
  • 1
PastaCoder
  • 1,031
  • 1
  • 9
  • 16
  • Apparently, using `?q=` does work, but only on the next launch. Also, since it's stored on NSUserDefaults, the app constantly launching to this URL, even if wasn't addressed right now... How can I set it up without NSUserDefaults? I'm editing the question to include this update. – PastaCoder May 05 '15 at 07:10

2 Answers2

0

If you're using the URL only for the current session, I don't recommend putting it in NSUserDefaults. Just pass it to the view controller, either via a property, or via an initWithURL: method.

rounak
  • 9,217
  • 3
  • 42
  • 59
  • Hi Rounak, Thanks for your answer :) I managed to solve it without using `initWithURL:` though. Check my answer above :) Can you check my other questions please? ([How to enable Location Services for a WKWebView in Swift?](http://stackoverflow.com/questions/30015943/how-to-enable-location-services-for-a-wkwebview-in-swift) and [How to include an Objective-C Extension in my Swift ActivityViewController?](http://stackoverflow.com/questions/30017304/how-to-include-an-objective-c-extension-in-my-swift-activityviewcontroller)) Thanks :) – PastaCoder May 05 '15 at 07:48
0

I managed to solve this by using this code in the AppDelegate:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    if let string = url.absoluteString {
        if let range = string.rangeOfString("q=") {
            let value = string[range.endIndex ..< string.endIndex]
            println(sourceApplication!)
            let viewController:ViewController = window!.rootViewController as! ViewController
            viewController.loadURL(value)
            return true
        }
    }
    return false
}

I also removed the code I mentioned on the question from the viewDidLoad() function on ViewController.

PastaCoder
  • 1,031
  • 1
  • 9
  • 16