PhoneGap 检测 iOS5 UIWebView 中的特定 URL

发布于 2024-12-27 08:09:46 字数 1448 浏览 2 评论 0原文

我正在将 PhoneGap 用于 iOS 项目,除了我想检测 Web 应用程序中注销链接的点击并使用本地注销逻辑拦截它之外,一切正常。我能够检测到注销链接,但是当 PhoneGap 的 UIWebView 中 about:blank 触发时,下面的逻辑也认为它是注销页面。有什么想法我做错了吗?

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    // If we detect the logout.jsp we need to go back to the login form and reset the credentials
    if ([[url path] rangeOfString:@"logout.jsp"].location == NSNotFound) {
        if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
            return YES;
        }
        else {
            return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
        }
    }
    else {
        // have PhoneGap go back to index page
        NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"www/index" ofType:@"html"];

        NSURL *url = [NSURL fileURLWithPath:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [theWebView loadRequest:requestObj];
        return NO;
    }
}

编辑:

我还没有解决为什么会发生这种情况,所以现在我在主要的 else 子句中添加了这个解决方法:

if ([[url path] rangeOfString:@"about:blank"].location != NSNotFound) {
    return YES;
}

赏金将授予那些能够弄清楚为什么我在初始 IF 仅检查时甚至需要该解决方法的人url 中的 logout.jsp 字符串和 about:blank 永远不应该匹配,但在本例中却匹配。

I am using PhoneGap for an iOS project and everything is working except I want to detect a click of the logout link in our web app and intercept it with local logout logic. I am able to detect the logout link, but the logic below ALSO thinks it is the logout page when about:blank fires in PhoneGap's UIWebView. Any ideas what I'm doing wrong?

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    // If we detect the logout.jsp we need to go back to the login form and reset the credentials
    if ([[url path] rangeOfString:@"logout.jsp"].location == NSNotFound) {
        if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
            return YES;
        }
        else {
            return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
        }
    }
    else {
        // have PhoneGap go back to index page
        NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"www/index" ofType:@"html"];

        NSURL *url = [NSURL fileURLWithPath:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [theWebView loadRequest:requestObj];
        return NO;
    }
}

EDIT:

I haven't solved why this is happening, so for now I have added this workaround in the main else clause:

if ([[url path] rangeOfString:@"about:blank"].location != NSNotFound) {
    return YES;
}

The bounty will go to someone who can figure out why I even need that workaround when the initial IF is only checking for the logout.jsp string in the url and about:blank should never match that, but does in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你げ笑在眉眼 2025-01-03 08:09:46

如果请求 url 为“about:blank”,则 [url path] 返回 nil。

所以 [nil rangeOfString:@"logout.jsp"].location 返回 0 和 NSNotFound != 0。

这就是为什么句子 ([[url path] rangeOfString:@"about:blank" ].location != NSNotFound) 为 TRUE。

If the request url is "about:blank", then [url path] returns nil.

So [nil rangeOfString:@"logout.jsp"].location return 0 and NSNotFound != 0.

That's why the sentence ([[url path] rangeOfString:@"about:blank"].location != NSNotFound) is TRUE.

杀手六號 2025-01-03 08:09:46

您是否尝试过在每次调用 shouldStartLoadWithRequest 时添加 NSLog ?

Have you tried to put an NSLog in each call to shouldStartLoadWithRequest?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文