ios 在 Safari 中打开链接而不是 UIWebVIew?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4452719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:18:42  来源:igfitidea点击:

Open links in Safari instead of UIWebVIew?

iosuiwebview

提问by treasure

I have an app with a UIWebViewinside a UIViewController. I load HTML from a web service as a string like this:

我有一个应用程序,UIWebView里面有一个UIViewController. 我从 Web 服务加载 HTML 作为这样的字符串:

self.webView loadHTMLString:_string baseURL:nil

Is it possible for the HTML links in this string to be opened in the browser and not in the UIWebView in my app? How can I do this?

这个字符串中的 HTML 链接是否可以在浏览器中打开,而不是在我的应用程序的 UIWebView 中打开?我怎样才能做到这一点?

I have tried this in the UIViewController that "hosts" the UIWebVIew:

我在“托管” UIWebVIew 的 UIViewController 中尝试过这个:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
   if (navigationType == UIWebViewNavigationTypeLinkClicked) {
     [[UIApplication sharedApplication] openURL:[request URL]];
     return NO;
   }
   return YES;
}

It doesn't seem to be working....

它似乎不起作用......

Any ideas?

有任何想法吗?

采纳答案by Stephen Darlington

Have you set the delegate of the UIWebViewto your UIViewController? There's nothing obviously wrong with your code as far as I can see, so it's likely to be something small like that.

您是否将 的代表设置UIWebView为您的UIViewController?就我所见,您的代码没有任何明显错误,所以它可能是这样的小东西。

回答by Rajesh_Bangalore

Add this in class..

在课堂上添加这个..

@interface yourViewController : UIViewController
<UIWebViewDelegate>

Add this in View did load

在视图中添加这个确实加载

- (void)viewDidLoad
{
    [description loadHTMLString:string baseURL:nil];
        description.delegate = self;
}

Add this in your .m file

将此添加到您的 .m 文件中

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

    return YES;
}

Note:

笔记:

UIWebView *description;
@synthesize description;

Then It will work perfectly the way you deserve..!! :)

然后它将以您应得的方式完美地工作..!!:)

回答by Karthik

Set the delegateof the UIWebViewto your UIViewControllerafter that use this UIWebview method and check the condition, for example now webview current url is google.com, if suppose you clicked on gmail the url contains the string with gmail. Use the below method it opened a safari browser and automatically loaded that url.

之后使用此 UIWebview 方法将UIWebView委托设置为您的UIViewController并检查条件,例如现在 webview 当前 url 是 google.com,如果假设您单击 gmail,则 url 包含带有 gmail 的字符串。使用以下方法打开 safari 浏览器并自动加载该 url。

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked ) {
        NSURL *url = [inRequest URL];
        if ([[url absoluteString] rangeOfString:@"gmail"].location == NSNotFound) {
            [[UIApplication sharedApplication] openURL:[inRequest URL]];
            return NO;
        }
    }
    return YES;
}

回答by Pierre-Henri Lavigne

If you already setup properly the UIWebViewDelegate, simply doing

如果您已经正确设置了 UIWebViewDelegate,只需执行

self.webView loadHTMLString:_string baseURL:nil
self.webView.delegate = self;

should work

应该管用

回答by Carlos Parada

Add this line ( self.webview.delegate = self; )

添加这一行( self.webview.delegate = self; )

For Example in viewController.m

例如在 viewController.m

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlString baseURL:nil];
self.webview.delegate = self;

回答by Vincent Joy

Apple introduced a Safari View Controller on iOS 9. Safari View Controller brings all of the features user expect from Safari over to your app without ever leaving it.

Apple 在 iOS 9 上引入了 Safari View Controller。Safari View Controller 将用户期望从 Safari 获得的所有功能带到您的应用程序中,而无需离开它。

First we need to import Safari Services

首先我们需要导入Safari服务

#import <SafariServices/SafariServices.h>

For Objective C:-

对于目标 C:-

NSString *yourUrl = @"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];

For Swift:-

对于斯威夫特:-

let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)

回答by M. Ryan

Yes, in your hyperlink tag add

是的,在您的超链接标签中添加

target="blank"

And one question mark will be fine, thank you

一个问号就可以了,谢谢