3

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?

Community
  • 1
  • 1
adit
  • 32,574
  • 72
  • 229
  • 373

1 Answers1

4

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];
}
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • I should put this basically to open it in safari? – adit May 29 '11 at 05:56
  • It will depend on the URL. What you're doing is asking the `UIApplication` superclass to behave as it would've had you not subclassed. So for all links that safari can handle, it will. If it is an app URL, then the app will open. – Deepak Danduprolu May 29 '11 at 06:11
  • I always get UIViewController may not respond to openURL – adit May 29 '11 at 16:55
  • @adit I was assuming that code was from the `UIApplication` and not the delegate. Just return `NO` from the delegate. It should work. – Deepak Danduprolu May 29 '11 at 17:06
  • well if this is the case then it will always open it in Safari? I want to have two different things: 1. being able to open it in a UIWebView 2. being able to open it in Safari After showing the site in the modalviewcontroller, I have a button that is when pressed, I want the site to be shown in safari. Hope this clears out a few missing details I had – adit May 29 '11 at 18:26
  • @adit updated the answer with minor modifications to the `UIApplication` subclass based on the linked SO question. – Deepak Danduprolu May 29 '11 at 18:36
  • I tried doing: - (IBAction) openInSafari:(id) sender { [[CVore sharedApplication] openURL:url withOverride:NO]; } Then it gives me a warning -openURL:withOverride not found – adit May 29 '11 at 19:34
  • @adit You will have to cast it. Make that `[(CVore*)[CVore sharedApplication] openURL:url withOverride:NO];`. – Deepak Danduprolu May 29 '11 at 19:39
  • You mean to say you still get the `openURL:withOverride:` method not found warning? – Deepak Danduprolu May 29 '11 at 22:51
  • Its probably because I missed out on adding `-(BOOL)openURL:(NSURL *)url withOverride:(BOOL)override` to the interface. Edited it in. – Deepak Danduprolu May 29 '11 at 22:52