【问题标题】:Opening Mobile Safari from UIWeb View从 UIWeb 视图打开 Mobile Safari
【发布时间】:2010-11-29 15:19:21
【问题描述】:

我知道这是一个已经讨论过的话题,我尝试了这里发布的所有解决方案,但没有成功。我有一个显示本地文件的 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

我错过了什么?

谢谢!

【问题讨论】:

    标签: ipad ios uiwebview mobile-safari


    【解决方案1】:

    您需要实现 webview 委托的 webView:shouldStartLoadWithRequest:navigationType: 方法。当navigationType 为UIWebViewNavigationTypeLinkClicked 时,必要时检查request.URL,并在Safari 中使用[[UIApplication sharedApplication] openURL:request.URL] 打开它。

    【讨论】:

    • 谢谢!!我尝试了您的建议,但我认为仍然缺少某些东西(我想这很愚蠢)。我更新了代码,也许你能找到问题所在!
    • 已解决...我不得不使用界面生成器修改 xib 文件。
    猜你喜欢
    • 2012-05-10
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多