views:

1033

answers:

5

I have specified dataDetectorTypes on a UITextView so that URLs open in Safari when touched.

Is it possible to intercept this behaviour so I load the URL in a UIWebView instead? Or would I have write my own URL detector code to re-route this?

A: 

You can try implementing application:handleOpenURL: in your Application Delegate. This method should get called whenever a url gets opened. Here you should be able to make the URL open in your webview.

Elfred
This won't work. Apple hijacks all `http://` and `https://` URLs to Safari.
Ben S
+2  A: 

You would have to do the URL detection yourself and manually load the UIWebView when the URL is tapped.

Everything needs to be custom-done because Apple sends all http:// and https:// URL openings to Safari.

Ben S
A: 

Another Answer :) That works fine for me is to re-implement UIApplication openURL:(NSURL *) url

@interface UIApplication (Private)

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

@end

@implementation UIApplication (Private)

- (BOOL)openURL:(NSURL*)url {
   // all viewcontrollers should change currentViewController to self
   if ([MyWatcher currentViewController]) {
      // Do anything you want
      [[MyWatcher handleURL:url withController:[MyWatcher currentViewController]];
      return YES;
   }
   return NO;
}

@end

... Some view controller
- (void)viewDidLoad {
   [super viewDidLoad];
   [MyWatcher setCurrentController:self];
}
tt.Kilew
what is MyWatcher?
Rahul Vyas
A: 

I did everyone a favor and answered your question with a blog post and demo app.

http://52apps.net/post/879106231/method-swizzling-uitextview-and-safari http://github.com/marksands/UITextViewLinkOptions

To expand on tt.Kilew's post, you create the category, but call your method something else such as customOpenURL. When you want to go back to Safari you do something called Method Swizzling. It looks like this:

#import <objc/runtime.h>
..
Method customOpenUrl = class_getInstanceMethod([UIApplication class], @selector(customOpenURL:));
Method openUrl = class_getInstanceMethod([UIApplication class], @selector(openURL:));
method_exchangeImplementations(customOpenUrl, openUrl);

Just call this method to swap the openURL implementation with your customOpenURL implementation when you do and don't want to use Safari.

Check out the demo app for more detail. Hope this helps! :)

Mark Sands
+1  A: 

The answer above that works best is the replacement of method implementation for [UIApplication openURL:]

Another way to achieve that, without using runtime.h is to subclass UIApplication. Then, override the openURL: selector. With this approach, you can call [super openURL:] from your subclass for URLs you want the default handling for. It also seems a little cleaner to me since you don't need to mess with the internal method implementations of the classes.

If you choose this approach, though, there are 2 other important steps. In the main.m file you need to change the 3rd argument to the UIApplicationMain function call so that it matches the name of your subclass:

int retVal = UIApplicationMain(argc, argv, @"MyApplicationSubclass", nil);

Also, you should probably change the class of the File's Owner in your MainWindow.xib file from UIApplication to your subclass.

Rich Waters
This works perfectly for those who want to have their own implementation through out the application. I have found it really useful
Nareshkumar