I know it is an already discussed topic and I tried all the solutions posted here, but with no success. I have a UIWebView which shows local files and I want to open web links (starting with http://) in Safari, not inside the view.
Here's the code of my "small" app: onceViewController.h#import <UIKit/UIKit.h>
@interface onceViewController : UIViewController <UIWebViewDelegate>{
IBOutlet UIWebView *slampSite;
}
@property (retain, nonatomic) UIWebView *slampSite;
@end
onceViewController.m
#import "onceViewController.h"
@implementation onceViewController
@synthesize slampSite;
#define WWW_ROOT @"files/en"
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"home" ofType:@"html" inDirectory:WWW_ROOT];
NS开发者_开发百科URL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[slampSite loadRequest:request];
}
- (BOOL)webView:(UIWebView *)slampSite shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *requestURL = [ [ request URL ] retain ];
if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ]
|| [ [ requestURL scheme ] isEqualToString: @"https" ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
[ requestURL release ];
return YES;
}
[omissis]
- (void)dealloc {
[slampSite release];
[super dealloc];
}
@end
What am I missing?
Thank you!
You need to implement the webview delegate's webView:shouldStartLoadWithRequest:navigationType:
method. When navigationType is UIWebViewNavigationTypeLinkClicked
, then examine the request.URL
if necessary, and open it in Safari with [[UIApplication sharedApplication] openURL:request.URL]
.
精彩评论