简体   繁体   English

目标 c 从 safari 中的 UIWebView 打开链接

[英]objective c open links from UIWebView in safari

I have a UIViewController subclass which includes a UIWebView and implements the UIWebViewDelegate.我有一个 UIViewController 子类,其中包括一个 UIWebView 并实现 UIWebViewDelegate。 What i want to do is make links pressed in the UIWebView to open in safari.我想要做的是让在 UIWebView 中按下的链接在 safari 中打开。
I've read past questions about this, but still can't make it work.我已经阅读了过去有关此问题的问题,但仍然无法使其正常工作。 Here is about what i've done:这是关于我所做的事情:

In the - (void)viewDidLoad method of my class i use the following code:在我的 class 的- (void)viewDidLoad方法中,我使用以下代码:

[[self articleWebView] setDelegate: self];  
[articleWebView loadRequest:requestObj];

I don't want to display the whole html page that is loaded in the articleWebView object, so in the -(void)webViewDidFinishLoad:(UIWebView *)webView method i use this:我不想显示在文章WebView object 中加载的整个 html 页面,所以在-(void)webViewDidFinishLoad:(UIWebView *)webView方法中我使用这个:
NSString *content = [articleWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('myDivId')[0].outerHTML;"];

Then i empty(release) the articleWebView and load the content:然后我清空(释放)articleWebView 并加载内容:

[articleWebView release]; 
articleWebView= [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,380)];  
[articleWebView loadHTMLString:content baseURL:[NSURL URLWithString:@"http://www.mysite.gr/"]];  
self.view = articleWebView;

I tried to use the following, but it's not working我尝试使用以下内容,但它不起作用

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

Any ideas what i am missing?任何想法我错过了什么?
Thank you in advance.先感谢您。

EDIT: As i can see, the shouldStartLoadWithRequest does not get called, so i'm guessing there is something wrong with the delegate of my webView?编辑:如我所见, shouldStartLoadWithRequest 没有被调用,所以我猜我的 webView 的代表有问题吗?

I noticed that you are not setting the delegate of "articleWebView" after you release it.我注意到您发布后没有设置“articleWebView”的委托。

Try using this instead:尝试改用这个:

[articleWebView release]; 

articleWebView= [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,380)];  

**articleWebView.delegate = self;** 

[articleWebView loadHTMLString:content baseURL:[NSURL 
URLWithString:@"http://www.mysite.gr/"]];  

self.view = articleWebView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM