hridayesh hridayesh - 4 months ago 49
iOS Question

launching app or opening link in safari from ios framework

I am developing an ios framework (which can be installed using cocoapods). When a method of this framework is called, it will open a url on safari. If some other app is able to handle that url using universal link, then it would launch that app.

I was trying to call (in ios 9.3 simulator with target deployment ios 8.0

[[UIApplication sharedApplication] openURL:url];

Above code works perfectly in any ios app. But when I placed same code in my framework which is included in another app, it gives exception and app crashes.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString scheme]: unrecognized selector sent to instance 0x7fc070717e00'

Do things work differently inside framework? what would be correct way. In this example I was trying to open regular https link.



As this might be useful to others, here's what seems have to caused the problem:

[[UIApplication sharedApplication] openURL:url];

was called with an argument that actually wasn't an NSURL, but an NSString, hence an NSInvalidArgumentException was thrown in the process - with a hint in the description for the wrong type.

The compiler should issue at least a warning here, though.

Before the edit, an alternative method was mentioned:

[[UIApplication sharedApplication] openURL:url options:nil completionHandler:^(BOOL success) {
    NSLog(@"opened url %d", success);

The reason for this to fail was different: that method is only available since iOS 10, hence an NSInvalidArgumentException was thrown on the testing environment running iOS 9.3.