There are two test Apps called Sender & Receiver
They communicate with each other by Url Scheme. I would like to send a String from Sender to Receiver, is that possible?
Detail about the String:
I both create Textfields in Sender and Receiver, I would text some String on Sender Textfield. When I click button, the String will show on the Receiver Textfield.
It seems that I have to implement NSNotificationCenter.defaultCenter().postNotificationName in my Apps Receiver
Here is my App Receiver code:
In Appdelegate
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
calledBy = sourceApplication
fullUrl = url.absoluteString
scheme = url.scheme
query = url.query
}
In 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
}
}
Now, I only can show the information about the url like this
Hope to get some suggestion!
URLs in Swift are used in a lot of ways. We fetch data using an API, images to visualize our app, and we often work with local files from our bundle. The Foundation framework allows us to access a lot of URL components easily with default parameters. If we need access to URL components like query items, we can make use of the URLComponents type.
Working with URL Schemes in iOS Apps. The URL scheme is an interesting feature provided by the iOS SDK that allows developers to launch system apps and third-party apps through URLs. For example, let’s say your app displays a phone number, and you want to make a call whenever a user taps that number.
A URL can also be converted into a String by using the absoluteString property: A scenario that’s often used when building an API in Swift is to construct a link by using a base URL. For example, we could build up a blog post category page as follows: At the same time, you can get the base for a certain link if the URL itself is not absolute:
Advanced Swift Home UI Networking Databases Combine Building and Parsing URLs in Swift Subscribe Swift Building and Parsing URLs in Swift Learn how to create a URL from components, parse a URL into components, and work with URL query items in Swift. 28 Mar 2021•3 min read
Yes, you can use query string.
url.query contains the query string. For example, in the URL iOSTest://www.example.com/screen1?textSent="Hello World", the query string is textSent="Hello World".
Normally we use URLSchemes for deeplinking too, therefore the URLScheme specifies which app to open and the path in the url specifies which screen to open and query string has additional parameters which we want to send to the app.
url.query is a string, therefore you will have to parse it to get the value you need: For example, in the URL iOSTest://www.example.com/screen1?key1=value1&key2=value2, the query string is key1=value1&key2=value2. I'm writing code to parse it but make sure you test it for your case:
let params = NSMutableDictionary()
let kvPairs : [String] = (url.query?.componentsSeparatedByString("&"))!
for param in kvPairs{
let keyValuePair : Array = param.componentsSeparatedByString("=")
if keyValuePair.count == 2{
params.setObject(keyValuePair.last!, forKey: keyValuePair.first!)
}
}
params will contain all key value pairs in query string. Hope it helps :]
In case you don't want to do deep-linking, you can directly append the queryString to scheme. eg: iOSTest://?textSent="Hello World"
Of course you can. You just compose your app launch URL and pass the parameters like this
iOSTest://?param1=Value1¶m2=Valuew
and then parse it in UIApplicationDelegate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With