1

I am new in Swift. I wish to access the sourceApplication value inside the options parameter of this method.

optional func application(_ app: UIApplication, 
                     open url: URL, 
                  options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool

I checked the documentation and I found out that UIApplicationOpenURLOptionsKey is a structure. It contains a type property: static let sourceApplication: UIApplicationOpenURLOptionsKey

But when I tried to access the sourceApplication value by doing this:

options.sourceApplication

...this error was thrown:

Value of type '[UIApplicationOpenURLOptionsKey : Any]' has no member 'sourceApplication'

I have a thought that this might be a dictionary, But the type of the key is UIApplicationOpenURLOptionsKey. I don't have any idea how to access the value using the key type UIApplicationOpenURLOptionsKey.

I checked out tutorials point and some other sites, but didn't find a solution.

My questions are:

  1. How do I access the value named sourceApplication from options?
  2. What do you call this type of parameter? ([UIApplicationOpenURLOptionsKey : Any] = [:])? Is this a dictionary?
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Random Forkson
  • 71
  • 3
  • 17

1 Answers1

8

UPDATE for Swift 4.2

The method was changed to:

func open(_ url: URL, 
          options: [UIApplication.OpenExternalURLOptionsKey : Any] = [:], 
          completionHandler completion: ((Bool) -> Void)? = nil)

You can access the keys in the same way.


options is a dictionary. You should access the sourceApplication key of it.

if let sourceApp = options[.sourceApplication] as? String {
    //work with the value
}

From Apple's documentation about sourceApplication:

Contains the bundle ID of the app that sent the open-URL request to your app. Value is an NSString object.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • Thank you so much for your quick answer! Now I understand how it's done. But may I ask what = [:] this means? – Random Forkson Dec 05 '17 at 12:57
  • 1
    Means the default it is an empty dictionary – Leo Dabus Dec 05 '17 at 12:58
  • Note that UIApplicationOpenURLOptionsKey is redundant. (Not necessary) `[.sourceApplication]` – Leo Dabus Dec 05 '17 at 12:59
  • @LeoDabus Thank you, I am aware of that, I wrote it like this deliberately. (it's easier for a beginner to see it through this way) – Tamás Sengel Dec 05 '17 at 13:00
  • 1
    The comment was aimed at the OP – Leo Dabus Dec 05 '17 at 13:01
  • @RandomForkson Leo Dabus is right, it's the default value of the `options` argument. To learn more about Swift functions, take a look at [the documentation](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html). – Tamás Sengel Dec 05 '17 at 13:01
  • @RandomForkson you might be interested in this https://stackoverflow.com/questions/46492353/ios-copying-files-from-inbox-folder-to-document-path – Leo Dabus Dec 05 '17 at 13:05