有兩個測試應用程序稱爲發件人&接收器在swift中通過url方案在兩個應用之間傳遞數據?
它們通過Url Scheme相互溝通。我想從Sender發送一個字符串到Receiver,這可能嗎?
詳細瞭解字符串:
我都在發件人創建文本框和接收器,我將文字發件人文本字段的一些字符串。當我點擊按鈕時,字符串將顯示在Receiver Textfield上。
這裏是我的應用程序接收代碼:
在AppDelegate中
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
calledBy = sourceApplication
fullUrl = url.absoluteString
scheme = url.scheme
query = url.query
}
在的viewController現在
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.displayLaunchDetails), name: UIApplicationDidBecomeActiveNotification, object: nil)
// Do any additional setup after loading the view, typically from a nib.
}
func displayLaunchDetails() {
let receiveAppdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
if receiveAppdelegate.calledBy != nil {
self.calledByText.text = receiveAppdelegate.calledBy
}
if receiveAppdelegate.fullUrl != nil {
self.fullUrlText.text = receiveAppdelegate.fullUrl
}
if receiveAppdelegate.scheme != nil {
self.schemeText.text = receiveAppdelegate.scheme
}
if receiveAppdelegate.query != nil {
self.queryText.text = receiveAppdelegate.query
}
}
,我只可以顯示有關的URL like this信息
希望得到一些建議!
完美答案!很酷,在計劃之後添加一個查詢。 – HungCLo
很好的答案。如果你想傳遞字符串以外的數據,你應該添加關於base64的信息。 –
很好的答案,很好的解釋和一個明確的例子。 – Josh