- Launching Your App Via URL
- Passing Data to the App
- Displaying an App Banner with Smart App Banner
- Summary
Passing Data to the App
If you want to pass data to your app through the URL, you can do so by appending the data after the scheme, as shown in the bold statements in the following example:
<!DOCTYPE html> <html> <body> <a href="smartappbanner://">Launch SmartAppBanner app</a><br/> <a href="smartappbanner://domain/mypath?arg1=value1&arg2=value2"> Launch SmartAppBanner app with parameters</a> </body> </html>
In order for your app to receive the data passed in through the URL, you need to implement the application:openURL:sourceApplication:annotation: method in your application delegate:
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var scheme: String! var path: String! var query: String! func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?)-> Bool { scheme = url.scheme path = url.path query = url.query return true }
In this example, the application:openURL:sourceApplication:annotation: method is fired when the application is launched through a URL. With this method, you can obtain the scheme, path, and query of the URL that was used to launch the app.
Select the Main.storyboard file to edit it in the Storyboard Editor. Add a couple of Labels and Text Fields to the View controller as shown in Figure 3.
Figure 3 Populating the View window.
Create three outlets for the three text fields and name them as follows:
import UIKit class ViewController: UIViewController { @IBOutlet weak var txtScheme: UITextField! @IBOutlet weak var txtPath: UITextField! @IBOutlet weak var txtQuery: UITextField!
Add the following statements in bold to the ViewController.swift file:
import UIKit class ViewController: UIViewController { @IBOutlet weak var txtScheme: UITextField! @IBOutlet weak var txtPath: UITextField! @IBOutlet weak var txtQuery: UITextField! override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver( self, selector: "displayLaunchDetails", name: UIApplicationDidBecomeActiveNotification, object: nil) } func displayLaunchDetails() { var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate if appDelegate.scheme != nil { self.txtScheme.text = appDelegate.scheme } if appDelegate.path != nil { self.txtPath.text = appDelegate.path } if appDelegate.query != nil { self.txtQuery.text = appDelegate.query } } deinit { NSNotificationCenter.defaultCenter().removeObserver( self, name: UIApplicationDidBecomeActiveNotification, object: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
Notice that we used the NSNotificationCenter to call the displayLaunchDetails() method every time the application comes to the foreground. This approach ensures that your application will be able to update the URL details used to launch your app, even if the app was previously in the background.
Deploy the application onto the device and then reload the MyApp.html page on the browser. Tapping the second link (see Figure 4) launches the app, with the details of the URL displayed (see Figure 5).
Figure 4 Tapping the second link launches the app and passes data to it.
Figure 5 The application is loaded and displaying the data passed to it.