从 UIWeb 视图打开 Mobile Safari
我知道这是一个已经讨论过的主题,我尝试了此处发布的所有解决方案,但没有成功。我有一个 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
我缺少什么?
谢谢你!
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];
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
What am I missing?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要实现 webview delegate 的
webView:shouldStartLoadWithRequest:navigationType:
方法。当 navigationType 为UIWebViewNavigationTypeLinkClicked
时,如有必要,请检查request.URL
,并使用[[UIApplication sharedApplication] openURL:request.URL]
在 Safari 中打开它代码>.You need to implement the webview delegate's
webView:shouldStartLoadWithRequest:navigationType:
method. When navigationType isUIWebViewNavigationTypeLinkClicked
, then examine therequest.URL
if necessary, and open it in Safari with[[UIApplication sharedApplication] openURL:request.URL]
.