我知道这是一个已经讨论过的主题,我尝试了这里发布的所有解决方案,但没有成功。我有一个显示本地文件的UIWebView,我想在Safari中打开Web链接(以http://开头),而不是在视图中。
这是我的“小”应用程序的代码:
的 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];
NSURL *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
我错过了什么?
谢谢!
答案 0 :(得分:1)
您需要实现webview委托的webView:shouldStartLoadWithRequest:navigationType:
方法。当navigationType为UIWebViewNavigationTypeLinkClicked
时,如有必要,请检查request.URL
,然后使用[[UIApplication sharedApplication] openURL:request.URL]
在Safari中打开它。