在safari中打开链接而不是在webview内部

时间:2014-06-05 20:45:42

标签: ios webview

我是制作iOS应用程序的初学者。我做了一个简单的webview显示我的网页。问题是我的网页中按下的每个链接都会在webview中打开。我想在safari中打开一些链接。我想在webview中打开以“..something”开头的链接,并在safari中打开所有其他链接。我还有一个电子邮件和拨号按钮,我想在手机上的拨号应用程序和电子邮件应用程序中打开。这有可能吗?请解释一下。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIWebView *webView;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize webView;

- (void)viewDidLoad
{

NSURL *url = [NSURL URLWithString:@"http://MyWebPage"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

我使用java使用以下代码为Android制作了相同的应用程序

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        try{
            System.out.println("url called:::" + url);
            if (url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent);
            }  else if (url.startsWith("http:")
                    || url.startsWith("https:")) {

                 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
                 startActivity(intent);

            }  else if (url.startsWith("mailto:")) {

                MailTo mt=MailTo.parse(url);

                send_email(mt.getTo());

            }
            else {
                return false;
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        return true;
    }

}

2 个答案:

答案 0 :(得分:1)

对于要在网络视图中打开的网址,请使用与您相同的代码。

要在safari中打开,请使用此

NSString* launchUrl = @"URL to open in safari";

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];

答案 1 :(得分:1)

您需要将控制器设为UIWebViewDelegate并实施webView: shouldStartLoadWithRequest:navigationType:方法。

@interface ViewController () <UIWebViewDelegate>

viewDidLoad应如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    NSURL *url = [NSURL URLWithString:@"http://MyWebPage"];
    NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
    webView.delegate = self;
    [webView loadRequest:requestURL];
}


- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked && [self shouldOpenInSafari:[inRequest URL]]) {
        [[UIApplication sharedApplication] openURL:[inRequest URL]];
        return NO;
    }

    return YES;
}

- (BOOL)shouldOpenInSafari:(NSURL*)url
{
    if ([url.scheme isEqualToString:@"mailto"]) {
        return YES;
    }
    else if ([url.scheme isEqualToString:@"tel"]) {
        return YES;
    }
    else if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"example.com"]) {
        return YES;
    }

    return NO;
}

然后您需要实现shouldOpenInSafari:方法。 openURL:方法还可以处理tel:mailto:链接。

来自here的一些代码。