Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIApplication's -canOpenURL: -openURL: return misleading result

Since iOS6, I can't tell whether the application can launch Safari or not.

If Safari is restricted on the device (Settings>General>Restrictions), nothing happens when trying to open a URL, and there's no indication of what went wrong:

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
[[UIApplication sharedApplication] canOpenURL:url]; // Returns YES
[[UIApplication sharedApplication] openURL:url]; // Returns YES

However, Safari does not launch, and the user is left wondering why my buttons are "broken".

This seems like a bug to me so I filed a radar #12449905.

Is there another way to solve this problem?

like image 929
hwaxxer Avatar asked Oct 07 '12 17:10

hwaxxer


People also ask

What happen if I return false in didFinishLaunchingWithOptions?

Return Value false if the app cannot handle the URL resource or continue a user activity, otherwise return true . The return value is ignored if the app is launched as a result of a remote notification.

What is UIApplication in Swift?

The application object maintains a list of open windows ( UIWindow objects), which it can use to retrieve any of the app's UIView objects. The UIApplication class defines a delegate that conforms to the UIApplicationDelegate protocol and must implement some of the protocol's methods.

What is the use of application Willfinishlaunchingwithoptions and application didFinishLaunchingWithOptions?

Discussion. Use this method (and the corresponding application(_:didFinishLaunchingWithOptions:) method) to initialize your app and prepare it to run. This method is called after your app has been launched and its main storyboard or nib file has been loaded, but before your app's state has been restored.


1 Answers

If this is an Apple bug, then it looks like the thing for you to do is to program around it. Once the user clicks the button, you can always write something like this:

[self performSelector:@selector(notifyUserOfRestrictedAccess) withObject:self afterDelay:.5];

In the app delegate, you can set a property such as:

- (void)applicationWillResignActive:(UIApplication *)application {
    self.openingExternalProgram = YES;
}

In your view controller, create the method like this:

-(void) notifyUserOfRestrictedAccess {

    if (!appDelegate.openingExternalProgram) {
        // Message the user via UIAlertView about restricted Safari access
    }
    appDelegate.openingExternalProgram = NO;
}

I'm sure there are better ways, but at least you don't have to wait on Apple.

like image 155
Brian Douglas Moakley Avatar answered Oct 16 '22 17:10

Brian Douglas Moakley