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

andreacipriani
  • 2,519
  • 26
  • 23

2 Answers2

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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Thanks for pointing that out. Is there any viable workaround? That would be appreciated. – Arthur Gevorkyan Oct 09 '15 at 05:57
  • 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. – Tom Harrington Oct 09 '15 at 16:58
0

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

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

Tim Johnsen
  • 1,471
  • 14
  • 33
  • I think that this is not a public API, so Apple would refuse any application using this line of code, no? – andreacipriani Oct 27 '15 at 10:43
  • That is true, if you obfuscate the method signature you may be able to get through though – Tim Johnsen Oct 27 '15 at 15:41
  • 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. – Mazyod Nov 10 '15 at 08:35