Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept URL and opening URL in Safari

I have intercepted URL opening by doing the following:

- (BOOL)openURL:(NSURL *)url{
    URLViewController * web = [[URLViewController alloc] init];
    web.url = url;
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:web];
    [nav.navigationBar setTintColor:[UIColor blackColor]];
    [nav setModalPresentationStyle:UIModalPresentationFormSheet];
    [self.detailViewController presentModalViewController:nav animated:NO];
    [web release];
    [nav release];
    return YES;
}

I have a UITextView in which detects URL and when clicking on the URL it opens up the link in a ModalViewController. Full detail on what's going on can be seen here. Now the issue is, what if I want to open a URL in safari, is it still possible?

like image 829
adit Avatar asked Dec 28 '25 21:12

adit


1 Answers

You should add an override flag indicating whether you want to exercise control or not.

@interface MyApplication : UIApplication {

}

-(BOOL)openURL:(NSURL *)url withOverride:(BOOL)override;

@end

@implementation MyApplication


-(BOOL)openURL:(NSURL *)url withOverride:(BOOL)override {
    if ( !override ) {
        return [super openURL:url];
    }

    if  ([self.delegate openURL:url]) {
        return YES;
    } else {
        return [super openURL:url];
    }
}

-(BOOL)openURL:(NSURL *)url{
    return [self openURL:url withOverride:YES];
}
@end

So now all calls that you want to bypass can be sent like this.

[[MyApplication sharedApplication] openURL:url withOverride:NO];

Original Answer

This is what you should do. Put it before the return YES; statement.

if ( [super canOpenURL:aURL] ) {
    return [super openURL:aURL];
}
like image 84
Deepak Danduprolu Avatar answered Dec 30 '25 11:12

Deepak Danduprolu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!