4

I have successfuly added my own url schemes to my App. The App correctly launches using the schemes.

Now I want to handle the incoming data but the delegate is not called. It is an universal app and I have added the following function to both AppDelegates:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if (!url) {  return NO; }

    NSString *URLString = [url absoluteString];
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:NSLocalizedString(@"test message", nil) 
                          message:URLString
                          delegate:self 
                          cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];     
    [alert release];
    return YES;
}

I am testing with a schema like: myapp://appalarm.com …and would expect to be appalarm.com in the URLString

What is wrong with it?

Thanks for your responses!

favo
  • 5,426
  • 9
  • 42
  • 61

3 Answers3

7

I tried to clarify in another post. The answer of Ashley Clark is only partly correct. Under OS 4.0 the handleOpenURL gets called (at least for file URLs) and you have to implement it to handle open url calls for when the app is in background. Thus, opening the file in both methods may open it twice (if applicationDidFinishLaunchingWithOptions returned YES, which it should). See another post.

Community
  • 1
  • 1
Christian Fries
  • 16,175
  • 10
  • 56
  • 67
5

If your application delegate has implemented 'applicationDidFinishLaunchingWithOptions:' then the 'handleOpenURL:' method will never be called. Look at the options passed in through the other method to determine how your app was launched and what behavior you should implement.

Ashley Clark
  • 8,813
  • 3
  • 35
  • 35
  • 3
    thanks! the url is also contained in the launchOptions in the key UIApplicationLaunchOptionsURLKey – favo Jun 03 '10 at 10:20
  • 1
    Wrong! handleOpenURL will be called shortly after applicationDidFinishLaunchingWithOptions if you return YES. see section 'Handling URL Requests' in chapter 'Inter-App Communication' on the 'App Programming Guide for iOS' by Apple. – AmitP Aug 25 '15 at 15:14
  • @AmitP is right. `application(_:open:options:)` will not be called only if your implementations return false from both the `application(_:willFinishLaunchingWithOptions:)` and `application(_:didFinishLaunchingWithOptions:)` methods. – Bartosz Kunat Oct 09 '19 at 09:52
2

Try your code into below function

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if (!url) {  return NO; }

    NSString *URLString = [url absoluteString];
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:NSLocalizedString(@"test message", nil) 
                          message:URLString
                          delegate:self 
                          cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];     
    [alert release];
    return YES;
}
lee
  • 7,955
  • 8
  • 44
  • 60