Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK - opening links in a UITextView in a web view

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?

like image 844
Chris Newman Avatar asked Dec 11 '25 12:12

Chris Newman


2 Answers

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.

like image 176
Ben S Avatar answered Dec 13 '25 02:12

Ben S


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.

like image 36
Rich Waters Avatar answered Dec 13 '25 02:12

Rich Waters