25

A link should open the app. I've got that to work. I just want to know how to pass a parameter. Let's say the url is "addappt://?code=abc". When a view controller pops up, a code field should have populated text - the letters after the equals to sign. I've got part of this to work. I use the following (in app delegate.m):

NSArray *elements = [url.query componentsSeparatedByString:@"="];
NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
          val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

(BTW: val is declared in appdelegate.h

I am also able to pass val to the view controller. My only problem is populating the textfield, named 'code'. How can you populate code as soon as the app is opened by the link?

Help Appreciated.

DHShah01
  • 553
  • 1
  • 7
  • 16

2 Answers2

23

Here is a nice tutorial on Using Custom URL Scheme in iOS

As in the tutorial, you should parse the URL parameters and store them to use in the app in this method:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  // Do something with the url here
}
Nikola Kirev
  • 3,152
  • 3
  • 22
  • 30
  • 1
    @AmoghTalpallikar Sorry for that. Thanks for pointing it out. It should work fine now. – Nikola Kirev Jan 23 '13 at 10:54
  • is it possible to make such URL which will send the user to Appstore if the app is not installed. – nr5 Jan 28 '15 at 12:27
  • 4
    Apple recommends to implement the method `application:openURL:sourceApplication:annotation:` to handle custom urls. – Juan Catalan May 30 '15 at 15:07
  • Swift 3, XCode 8, the method is: `func application(_ app: UIApplication, open url: URL, options: [String : AnyObject] = [:]) -> Bool` Cheers – Sasho Jul 30 '16 at 04:52
  • but by hitting the url in mail it cannot open because that url convert like this chatapp/ – lokesh Apr 15 '17 at 07:58
0

On Xcode 12 this code works perfectly. You can imagine that this is also a URL as regular. In Source app you can call and open destination url with parameter like

    let url = URL(string: "DestinationApp:PATH?PARAMETER=11111")
           
    UIApplication.shared.open(url!) { (result) in
        if result {
            print(result)
           // The URL was delivered successfully!
        }
    }

Destination app can handle metod in AppDelegate with this method. Alert is for double check.

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            // Determine who sent the URL.
            let sendingAppID = options[.sourceApplication]
            print("source application = \(sendingAppID ?? "Unknown")")
    
            // Process the URL.
            guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
                let path = components.path,
                let params = components.queryItems else {
                    print("Invalid URL or path missing")
                    return false
            }
    
            if let parameter = params.first(where: { $0.name == "PARAMETER" })?.value {
                print("path = \(path)")
                print("parameter = \(parameter)")
    
                let alert = UIAlertController(title: "Path = \(path)", message: "Parameter = \(parameter)", preferredStyle: .alert)
    
                let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    
                if var topController = keyWindow?.rootViewController {
                    while let presentedViewController = topController.presentedViewController {
                        topController = presentedViewController
                    }
    
                    topController.present(alert, animated: true, completion: nil)
                }
                return true
            } else {
                print("parameter is missing")
                return false
            }
}
LoGoCSE
  • 558
  • 5
  • 13