避免UIWebView加载iTunes App [英] Avoid UIWebView load iTunes App

查看:48
本文介绍了避免UIWebView加载iTunes App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加载iTunes URL时如何避免UIWebView打开iTunes App?

How to avoid an UIWebView from opening the iTunes App when loading an iTunes URL?

示例网址: http://itunes.apple.com/nl/app/bluppr-postcards/id348147229?mt = 8

以上URL直接加载iTunes App.UIWebViewDelegate似乎无法控制它,只是将页面加载到UIWebView中即可.

Above URL loads the iTunes App directly. UIWebViewDelegate doesn't seem to be able to control this to just load the page in the UIWebView.

似乎由JavaScript函数detectAndOpenItunes()控制;在页面的body标签中.

It seems it's controlled by the JavaScript function detectAndOpenItunes(); in the body tag of the page.

有什么想法吗?

推荐答案

您是对的,相关的函数是detectAndOpenItunes(),该函数包含在

You're right that the relevant function is detectAndOpenItunes(), which is contained in this file and requires 'iPhone' or 'iPod' to be in the user agent string. I wrote a quick little test app with a web view that just does the following upon viewDidLoad:

[webView loadRequest:
      [NSURLRequest requestWithURL:
        [NSURL URLWithString:
          @"http://itunes.apple.com/nl/app/bluppr-postcards/id348147229?mt=8"]]];

正如您所说,这将打开App Store应用程序.所以我将其修改为:

As you say, that opens the App Store app. So I modified it to:

// set some user agent that doesn't have 'iPod' or 'iPhone' in the name
[[NSUserDefaults standardUserDefaults] 
             registerDefaults:[NSDictionary 
                        dictionaryWithObject:@"some old phone or other" 
                        forKey:@"UserAgent"]];

[webView loadRequest:
      [NSURLRequest requestWithURL:
        [NSURL URLWithString:
          @"http://itunes.apple.com/nl/app/bluppr-postcards/id348147229?mt=8"]]];

这会在不打开应用程序的情况下将页面显示为网页,但是格式存在问题,显示的页面太宽.快速搜索 var deviceDetect = ,您会发现用户代理也用于确定格式.

That displays the page as a web page without opening the app, but there's a problem with the formatting, the displayed page being far too wide. Do a quick search for var deviceDetect= and you'll see that the user agent is also used to determine formatting.

我能提出的唯一解决方案实质上涉及屏幕抓取级别的行为.您可以通过

The only solutions I can come up with for that essentially involve screen-scraping-level behaviour. You may subclass NSURLProtocol and add any protocol handler you like via +registerClass:. If you design your protocol to perform HTTP requests then it'll replace the built-in methods for handling HTTP requests. By being selective in which requests you accept or decline, you can do real loading by allowing the HTTP requests you don't want to fall down to the real protocol handler. Hence, you can selectively catch and alter any fetched file before allowing it to be passed to the web view.

您可以使用它来捕获和编辑您喜欢的.js和.html的任意位,但是每当Apple调整页面时,您肯定会遇到问题.

You could use that to catch and edit whichever bits of .js and .html you like, but then you're pretty much bound to encounter problems whenever Apple adjust their page.

类似地,您可以使用webview的 stringByEvaluatingJavaScriptFromString:来执行合适的Javascript,以在加载后重新格式化页面,但是我无法确切确定要运行的内容,并且解决方案同样脆弱

Similarly, you could use webview's stringByEvaluatingJavaScriptFromString: to execute suitable Javascript to reformat the page after loading, but I'm unable to determine exactly what you'd run and your solution would be just as fragile.

这篇关于避免UIWebView加载iTunes App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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