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
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 UITextView
s 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.
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.
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! :)