7

In my app I want to have two Different URL Schemes.
Like One and Two
So the user can open my app with:
one://something
and
two://something

I am using this:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
}

How will the app know if the user types one or two?

Spidy
  • 1,137
  • 3
  • 28
  • 48
Jonathan Gurebo
  • 285
  • 1
  • 6
  • 19

1 Answers1

11

handleOpenURL is deprecated, so if you're targeting iOS 4.2 or later, you should instead use application:openURL:sourceApplication:annotation:

In both cases, you will be passed an NSURL, on which you can just access the scheme property to find out what scheme was used to access your app.

EDIT: For readability; in your implementation of application:openURL:sourceApplication:annotation:, the code would be something similar to;

if([[url scheme] caseInsensitiveCompare:@"one"] == NSOrderedSame) 
{ 
    /* one here */ 
} else { 
    /* not one here */ 
}
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • so my code will looks like this ? : - (BOOL)application:(UIApplication *)application:openURL:sourceApplication:annotation: handleOpenURL:(NSURL *)url { i dont know, exactly how should i use the application:openURL:sourceApplication:annotation: – Jonathan Gurebo Jan 26 '13 at 17:10
  • @JonathanGurebo Something like; `if(![[url scheme] caseInsensitiveCompare:@"one"]) { /* one here */ } else { /* not one here */ }` – Joachim Isaksson Jan 26 '13 at 17:16
  • @JoachimIsaksson You should edit your answer with that code so it is easier to read. – rmaddy Jan 26 '13 at 18:15
  • @rmaddy Thanks, added the code to the answer for readability. – Joachim Isaksson Jan 26 '13 at 18:50
  • Since `caseInsensitiveCompare:` returns an enum value, you should change that to `if ([[url scheme] caseInsensitiveCompare:@"one"] == NSOrderedSame)`. – rmaddy Jan 26 '13 at 18:57
  • @rmaddy `NSOrderedSame` equals `0`, so a `!` is equivalent. For readability you're right though that it's cleaner. – Joachim Isaksson Jan 26 '13 at 19:04
  • I know it is equal to zero. But it's an enum. Code shouldn't rely on an enum having a specific numerical value. The code is safer and more readable if you use the actual defined enum names. – rmaddy Jan 26 '13 at 19:53