views:

889

answers:

3

Hi. I'm developing and iPhone 3.0 application. I'm trying to open web links in a UITextView into a UIWebView instead of Safari. But still no luck. The UITextView is not editable, and it perfectly detects web links and open them in Safari. How to avoid that? How to grab that url so i can use with my own web view? Sorry for my English and thanks for your answers. Regards, Luca

+4  A: 

The simplest way is to override the webView:decidePolicyForNavigationAction:request:frame:decisionListener: method on UITextView like so:

@interface UITextView (Override)
@end

@class WebView, WebFrame;
@protocol WebPolicyDecisionListener;

@implementation UITextView (Override)

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener
{
 NSLog(@"request: %@", request);
}
@end

This will affect all UITextViews in your application. If you only require this on a single view, create a subclass and override the method on that.

Note: this is technically a private API and could be removed at any time. There is no way to do this via the public API.

rpetrich
Uhm, being private, do you think it's gonna be a problem for appstore approval? Btw tonight i'll check your code and i'll let you know. Thanks.
crash
This isn't the sort of thing they can check for, but you may be at a slight risk for future OS changes (this is a public API in the desktop equivalent and it's unlikely that Apple will stop using WebKit, but it's possible). The worst that can happen is the behaviour will return to the default of launching MobileSafari.
rpetrich
Perfect. It worked smoothly. Thanks!
crash
Nice!Another answer is to override [UIApplication openURL:(NSURL *) url ] http://stackoverflow.com/questions/1889262/iphone-sdk-opening-links-in-a-uitextview-in-a-web-view/2251898#2251898
tt.Kilew
@crash - did you ever submit an app using this code? Was it rejected or did it pass? Does anyone else here have any apps that were approved using this code?
Jasarien
I wouldn't risk it. I'm not sure if subclassing UIApplication and overriding openURL: is considered private; it probably is, but is closer to public APIs than my answer
rpetrich
Apple now machine scan code for illegal APIs. I'm certain this is not going to get past them now.
Roger Nolan
I've never submitted an app using this code. I totally agree with @rpetrich and @Roger Nolan
crash
A: 

Sorry, but could anyone show me the rest of the implementation for an application with a navigationController?

How to open an Webview and pass along the request.

Joao Henrique
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

You need to create a UIApplication category sort of overriding openURL, 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