Question

i need to open an URL from local notification. I pass URL by notification.userInfo and I retrieve it by this code:

(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif {
  NSLog(@"didReceiveLocalNotification");
  NSString *url = [notif.userInfo objectForKey:@"urlMedia"];
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

}

When I click the application is opened, but the openURL don't work.
Is it not possible to open URL using local notification ?

Was it helpful?

Solution

You should first check to make sure that UIApplication canOpenURL before attempting to open it. This will also help identify what the problem might be. If UIApplication doesn't know how to handle the URL given this will return NO. Here is what the documentation states specifically...

Returns whether an app can open a given URL resource. This method guarantees that that if openURL: is called, another app will be launched to handle it. It does not guarantee that the full URL is valid.

Example Usage:

NSString *urlString = [notif.userInfo objectForKey:@"urlMedia"];
NSURL* url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top