如何使用自定义URL方案将数据传递给未运行的应用程序?

时间:2016-04-20 03:36:20

标签: ios swift url-scheme

有两个叫做Sender和Receiver的应用程序,我实现自定义url方案来传递数据

现在,我可以使用url scheme

传递数据

发件人代码:

@IBAction func SendButton(sender: AnyObject) {
    let sendTextMessage : String = sendText.text!
    let ourApplication = UIApplication.sharedApplication()
    let ourPath : String = "iOSTest://?\(sendTextMessage)"
    let ourUrl : NSURL = NSURL(string: ourPath)!
    if ourApplication.canOpenURL(ourUrl) {
        ourApplication.openURL(ourUrl)
    } else {
        print("The App doesn't install")
    }
}

App-delegate中的接收者代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    return true
}


func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {

    calledBy = sourceApplication
    fullUrl = url.absoluteString
    scheme = url.scheme
    query = url.query

    return true
}

ViewController中的接收器代码

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.displayLaunchDetails), name: UIApplicationDidBecomeActiveNotification, object: nil)
}

我从Apple那里读到了这份文件 UIApplicationDelegate Protocol Reference

似乎当我的应用程序处于前台时,数据传递将起作用。

当接收器未运行时,我也可以通过openURL打开接收器。但我通过的数据并没有显示出来。

真的需要一些信息!

0 个答案:

没有答案