Check if URL can be opened from an iOS today widget
Asked Answered
L

2

7

My problem is that I can't find out if a certain URL can be opened from an iOS widget. The method canOpenURL: is not available on today's widget because there is no UIApplication class.

Moreover the method openURL: of NSExtensionContext returns YES for the boolean "success", even with an invalid URL.

The code below enters the else condition (success BOOL is always YES) but in the same time the simulator shows a popup error, as you can seen in the attached image.

NSURL* invalidURL = [NSURL URLWithString:@"fake://blablabla"];
[self.extensionContext  openURL:invalidURL completionHandler:^(BOOL success) {
    if (success == NO) {
        DDLogWarn(@"Can't open URL: %@", invalidURL);
    }
    else{
        DDLogInfo(@"Successfully opened URL: %@",invalidURL);
    }
}];

enter image description here

Lazulite answered 8/10, 2015 at 9:1 Comment(1)
Have you tried this on a real device? The behviour may be different.Undershrub
U
1

It's a known bug. I filed this issue with Apple last year (rdar://18107612) when iOS 8.0b5 was current, and it's still an open issue.

File your own bug with Apple at http://bugreport.apple.com and hope for the best.

Ununa answered 8/10, 2015 at 21:29 Comment(2)
Thanks for pointing that out. Is there any viable workaround? That would be appreciated.Boohoo
Not a good one. Best I can think of is to have your main app check on the URL and then save info in user defaults to indicate whether it works. The extension could read that. However the info might not be current if the user adds or removes apps.Ununa
N
0

You can get to the shared UIApplication instance by using performSelector:, something like

UIApplication *sharedApplication = [[UIApplication class] performSelector:NSSelectorFromString(@"sharedApplication")];

Napier answered 27/10, 2015 at 4:29 Comment(3)
I think that this is not a public API, so Apple would refuse any application using this line of code, no?Lazulite
That is true, if you obfuscate the method signature you may be able to get through thoughNapier
obfuscate it and add a NSDate check to make sure the call is only made after the expected app review date is over.. But seriously, I don't think you should do this.Einhorn

© 2022 - 2024 — McMap. All rights reserved.