在UIWebView中获取iPhone的移动Twitter webapp [英] Getting iPhone's mobile twitter webapp inside UIWebView

查看:172
本文介绍了在UIWebView中获取iPhone的移动Twitter webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这就是场景:我正在构建一个显示UIWebView的iPad应用程序,宽度= 320像素(或现在点数)。它会在应用程序内部出现小部件。



我不得不修改webview的用户代理以使其显示mobileversion,而不是桌面版本,通过在self.viewDidLoad上执行此操作。

  NSDictionary * dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@Mozilla / 5.0( iPhone; CPU iPhone OS 5_0,如Mac OS X)AppleWebKit / 534.46(KHTML,如Gecko)Version / 5.1 Mobile / 9A334 Safari / 7534.48.3,@UserAgent,nil]; 
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

我还将webview的帧设置为320像素宽,并将settingsPageToFit设置为YES。 / p>

好的,但问题出在这里:webview显示移动版本,但显示的是TABLET移动版本,可能出现在Honeycombs平板电脑上。它有点像一个巨大的iPhone网络应用程序,有600像素宽。 UIwebView的框架宽度为320px,但它的内容要宽得多。



我不明白可能会有什么问题,因为用户代理被设置为Mobile iPhone上的Safari。有关如何使这个320px宽的UIWebView显示与iPhone的Mobile Safari上键入mobile.twitter.com时完全相同的版本的想法吗?



编辑:
好吧,问题似乎与twitter webapp使用设备宽度来确定向用户显示哪个webapp这一事实有关。



我发现此解决方案:在UIPopoverController中的UIWebView中显示Wiki移动页面



它有点工作:

   - (void)webViewDidFinishLoad :( UIWebView *)webView {
if(hasLoaded == NO){
NSString * webHTML = [NSString stringWithContentsOfURL:self.webView.request.URL encoding:NSUTF8StringEncoding error:NULL];
NSRange range = [webHTML rangeOfString:@device-width];
if((range.location!= NSNotFound)&&(range.length!= 0)){
webHTML = [webHTML stringByReplacingOccurrencesOfString:@device-widthwithString:@320选项:0范围:范围];
[self.webView loadHTMLString:webHTML baseURL:[NSURL URLWithString:@http://mobile.twitter.com]];
hasLoaded = YES;
}
}

它将代码中的设备更改为320 ,那是完美的,但是...当webview重新加载(使用新代码)时,我得到的是旧的(非常旧的)推特网络应用程序,而不是当前版本。有关如何修复它的任何想法吗?

解决方案

这似乎是一个更好的解决方案,因为它不需要重新加载HTML: https://stackoverflow.com/a/6104537/1449618



以下适用于iPad上我的应用程序。



将self设置为UIWebViewDelegate:

   - (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@document.querySelector('meta [name = viewport]')。setAttribute('content ','width =%d;',false);,(int)webView.frame.size.width]];
}


Ok, here's the scenario: I'm building an iPad app that will display an UIWebView, width = 320 pixels (or points now). It will wind of a "widget" inside the app.

I had to modify the user-agent of the webview to make it display the mobileversion, instead of the desktop version, by doing this on self.viewDidLoad.

NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1        Mobile/9A334 Safari/7534.48.3", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

I'm also setting the webview's frame to be 320 pixels wide, and settings scalesPageToFit to YES.

Ok, but here's the problem: The webview displays the mobile version, but the TABLET mobile version, the one that probably would appear on Honeycombs tablets. It's kinda of a giant iPhone web app, with something like 600 pixels wide. The frame of UIwebView is 320px wide, but it's content is much wider.

I dont' understand what may be happing, since the user-agent is being set to be Mobile Safari on iPhone. Any idea about how to make this 320px wide UIWebView display the exactly same version that you get when you type mobile.twitter.com on iPhone's Mobile Safari?

EDIT: Ok, the problems seems to be related to the fact that twitter webapp uses the "device-width" to determine which webapp to show to the user.

I found this solution: displaying Wiki mobile page in UIWebView within UIPopoverController

It kinda of works:

 -(void)webViewDidFinishLoad:(UIWebView *)webView{
if (hasLoaded == NO){
     NSString *webHTML = [NSString stringWithContentsOfURL:self.webView.request.URL encoding:NSUTF8StringEncoding error:NULL];
     NSRange range = [webHTML rangeOfString:@"device-width"];
     if ((range.location!=NSNotFound)&&(range.length != 0)) {   
          webHTML = [webHTML stringByReplacingOccurrencesOfString:@"device-width" withString:@"320" options:0 range:range];
          [self.webView loadHTMLString:webHTML baseURL:[NSURL URLWithString:@"http://mobile.twitter.com"]];
          hasLoaded = YES;
      }
}

It changes the device-with on the code to 320, that's perfect, BUT... when the webview reloades (with the new code) I get the OLD (very old) twitter web app, instead of the current one. Any idea about how to fix that?

解决方案

This seems to be a better solution because it doesn't require reloading the HTML: https://stackoverflow.com/a/6104537/1449618

The following works in my app on iPad.

Set self as UIWebViewDelegate then:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ", (int)webView.frame.size.width]];
}

这篇关于在UIWebView中获取iPhone的移动Twitter webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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