在iphone上加载UIWebView时如何检测url的重定向?

时间:2022-05-02 00:20:06

Is there any way to detect url redirects occurs while loading a UIWebView in iphone. when I load www.example.com url, it redirects to some other url. I need to get those redirecting urls and where is actually we handles redirection.

在iphone中加载UIWebView时是否有任何方法可以检测到网址重定向。当我加载www.example.com网址时,它会重定向到其他网址。我需要获取那些重定向网址,实际上我们处理重定向的位置。

3 个解决方案

#1


5  

Yes you can do this. Implement

是的,你可以做到这一点。实行

– webView:shouldStartLoadWithRequest:navigationType:

This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button or hyperlink on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity or in your case bring up comments page or change nav bar etc.

这个代表。只要您的webview即将发出请求,就会调用此方法。因此,当有人点击您网页上的按钮或超链接时,您将调用此方法。在您接听此电话后,您可以选择随意使用它。比如通过您自己的服务器重定向链接,或者向您的服务器记录有关用户活动的请求,或者在您的情况下打开评论页面或更改导航栏等。

Maybe from the request you can figure out if the HTTP response code is 3xx redirection then you can do what you want...

也许从请求你可以弄清楚HTTP响应代码是否是3xx重定向然后你可以做你想要的...

#2


2  

I found this thread when having to do the same. I used Srikar's answer, but here is my code.

我必须这样做时才发现这个帖子。我使用了Srikar的答案,但这是我的代码。

I'm using Lumberjack logging, but you can replace the below with just NSLog. You can also get specific parts of the URL if you were looking at host, path, or query string to know if there is a failure (which is what I'm about to have to do). If it redirects several times, each request will get called here and if you want you can stop it and do something or just observe.

我正在使用Lumberjack日志记录,但您可以使用NSLog替换以下内容。如果您查看主机,路径或查询字符串以了解是否存在故障(这是我将要做的事情),您还可以获取URL的特定部分。如果它重定向多次,那么每个请求都会在这里被调用,如果你想要,你可以停止它并做一些事情或者只是观察。

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest   navigationType:(UIWebViewNavigationType)inType
{
    DDLogVerbose(@"Redirecting to URL: %@", inRequest.URL.absoluteString);
    return YES;
}

#3


1  

Try this:

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

#1


5  

Yes you can do this. Implement

是的,你可以做到这一点。实行

– webView:shouldStartLoadWithRequest:navigationType:

This delegate . This method gets called whenever your webview is about to make a request. So now when someone clicks a button or hyperlink on your webpage, you will get a call to this method. After you catch this call, you can choose to do whatever you want with it. Like redirect the link through your own servers, or log a request to your server about user activity or in your case bring up comments page or change nav bar etc.

这个代表。只要您的webview即将发出请求,就会调用此方法。因此,当有人点击您网页上的按钮或超链接时,您将调用此方法。在您接听此电话后,您可以选择随意使用它。比如通过您自己的服务器重定向链接,或者向您的服务器记录有关用户活动的请求,或者在您的情况下打开评论页面或更改导航栏等。

Maybe from the request you can figure out if the HTTP response code is 3xx redirection then you can do what you want...

也许从请求你可以弄清楚HTTP响应代码是否是3xx重定向然后你可以做你想要的...

#2


2  

I found this thread when having to do the same. I used Srikar's answer, but here is my code.

我必须这样做时才发现这个帖子。我使用了Srikar的答案,但这是我的代码。

I'm using Lumberjack logging, but you can replace the below with just NSLog. You can also get specific parts of the URL if you were looking at host, path, or query string to know if there is a failure (which is what I'm about to have to do). If it redirects several times, each request will get called here and if you want you can stop it and do something or just observe.

我正在使用Lumberjack日志记录,但您可以使用NSLog替换以下内容。如果您查看主机,路径或查询字符串以了解是否存在故障(这是我将要做的事情),您还可以获取URL的特定部分。如果它重定向多次,那么每个请求都会在这里被调用,如果你想要,你可以停止它并做一些事情或者只是观察。

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest   navigationType:(UIWebViewNavigationType)inType
{
    DDLogVerbose(@"Redirecting to URL: %@", inRequest.URL.absoluteString);
    return YES;
}

#3


1  

Try this:

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