从 UIWeb 视图打开 Mobile Safari

发布于 2024-10-04 23:44:02 字数 1583 浏览 1 评论 0原文

我知道这是一个已经讨论过的主题,我尝试了此处发布的所有解决方案,但没有成功。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2024-10-11 23:44:02

您需要实现 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 is UIWebViewNavigationTypeLinkClicked, then examine the request.URL if necessary, and open it in Safari with [[UIApplication sharedApplication] openURL:request.URL].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文