目标c在Safari中从UIWebView打开链接 [英] objective c open links from UIWebView in safari

查看:135
本文介绍了目标c在Safari中从UIWebView打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController子类,它包含一个UIWebView并实现了UIWebViewDelegate。我想要做的是在UIWebView中按下链接以在safari中打开。

我已经读过有关此问题的过去的问题,但仍然无法使其工作。
以下是关于我所做的事情:

I have a UIViewController subclass which includes a UIWebView and implements the UIWebViewDelegate. What i want to do is make links pressed in the UIWebView to open in safari.
I've read past questions about this, but still can't make it work. Here is about what i've done:

- (void)viewDidLoad 方法中我的班级我使用以下代码:

In the - (void)viewDidLoad method of my class i use the following code:

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

我不想显示在articleWebView对象中加载的整个html页面,所以在 - (void)webViewDidFinishLoad:(UIWebView *)webView 方法我使用它:

NSString * content = [articleWebView stringByEvaluatingJavaScriptFromString :@document.getElementsByClassName('myDivId')[0] .outerHTML;];

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:
NSString *content = [articleWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('myDivId')[0].outerHTML;"];

然后我清空(发布)articleWebView并加载内容:

Then i empty(release) the articleWebView and load the content:

[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.

编辑:正如我所知,shouldStartLoadWithRequest没有被调用,所以我猜我的webView委托有问题吗?

As i can see, the shouldStartLoadWithRequest does not get called, so i'm guessing there is something wrong with the delegate of my webView?

推荐答案

我注意到你发布后没有设置articleWebView的委托。

I noticed that you are not setting the delegate of "articleWebView" after you release it.

请尝试改用:

[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;

这篇关于目标c在Safari中从UIWebView打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆