WebView 与 Safari

发布于 2024-11-17 15:42:40 字数 899 浏览 3 评论 0原文

我有一个网络应用程序,我正在尝试将其包装为本机应用程序。我正在使用 webView 来显示网络应用程序。该应用程序包含大量文章,其中一些文章具有外部页面的链接。当我点击这些链接时,webView 将加载外部页面,我的所有导航都会消失,我基本上被锁定在外部站点。如果不完全退出该应用程序,我就不可能返回该应用程序。

我想做的是,有一个脚本在将 URL 加载到 webView 之前对其进行评估。如果服务器与我拥有网络应用程序的服务器不同,我想要启动移动 Safari 的链接。我在网上找到了很多脚本,它们的功能与我想要的类似,但又不完全一样。它们检查 href,如果存在 href,则调用移动 Safari。这在我的情况下不起作用,因为应用程序中几乎所有链接都包含 href。

我在这里有一个我想做的事情的例子:

NSURL *requestURL = [ [请求 URL] 保留 ];

if (( ![ [requestURL schema] isEqualToString: @"http://www.some-site.dk/" ] ) && ( 导航类型 == UIWebViewNavigationTypeLinkClicked ))
{
NSLog(@"链接在本机 Safari 中打开");
返回 ![ [ UIApplication 共享应用 ] openURL: [ requestURL自动释放]];
}
NSLog(@"链接在 webView 中打开");
[ requestURL release ];
return YES;

如何检查 url 字符串是否不等于 Objective-C 中的某些内容?

任何帮助将不胜感激!

I have a web-app, that I'm trying to wrap as a native app. I'm using a webView, to display the web-app. The app contains a lot of articles, and some of them, have links to external pages. When I tap these links, the webView will load the external page, and all my navigation disappears, and I'm essentially locked at the external site. I have no possibility of going back to the app, without quitting it completely.

What I want to do is, have a script that evaluates the URL's before loading them in the webView. If the server differs from the one where I have the web-app, I want the link to launch mobile Safari. I have found a lot of scripts online, that does something like what I want, but not quite. They check for an href, and invokes the mobile Safari if the href is present. This will not work in my case, as pretty much all links in the App contains href's.

I have an example of what I'm trying to do here:

NSURL *requestURL = [ [request URL] retain ];

if (( ![ [requestURL scheme] isEqualToString: @"http://www.some-site.dk/" ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ))
{
NSLog(@"Link opened in native Safari");
return ![ [ UIApplication sharedApplication ] openURL: [
requestURL autorelease ] ];
}
NSLog(@"Link opened in webView");
[ requestURL release ];
return YES;

How do I check if an url string is not equal to something in objective-c?

Any help will be greatly appreciated!

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

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

发布评论

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

评论(2

潦草背影 2024-11-24 15:42:40

我不确定你保存 URL 字符串的内容,或者它是否完全转义,但如果你将 URL 移动到某种 NSString,那么有很多工具可以查找子字符串。

如果你只是想检查不平等那么你可以做类似的事情

if( ![urlString isEqualToString:@"Whatever you want to test against"] )
{

... execute code here

}

I'm not sure what you are holding your URL string in or if it is fully escaped or not but if you move the URL to some kind of NSString then there are lots of tools to look for substrings.

If you just want to check inequality then you can do something like

if( ![urlString isEqualToString:@"Whatever you want to test against"] )
{

... execute code here

}
素衣风尘叹 2024-11-24 15:42:40

我最终使用 rangeOfString 函数来确定所选链接是否包含包含 Web 应用程序的 Web 服务器的域名。其他所有内容都会发送到本机 Safari。

这是一个例子:

NSRange infoUrl = [filePath rangeOfString:@"DOMAIN_NAME.COM"];

if (infoUrl.location == NSNotFound)
{
     return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}

return YES;

I ended up using the rangeOfString function to determine if the selected link included the domain name of the web-server containing the web-app. Everything else gets sent to the native Safari.

Here's an example:

NSRange infoUrl = [filePath rangeOfString:@"DOMAIN_NAME.COM"];

if (infoUrl.location == NSNotFound)
{
     return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}

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