iOS:Facebook评论插件移动版不断重新加载

时间:2012-04-18 12:21:16

标签: ios facebook ios5 ios4 ios-simulator

最近,我一直在尝试将Facebook社交插件集成到iOS中的自定义UIWebView中,以便对网页进行评论。我添加了Like按钮以及Comments插件。这是我加载到Web视图中的HTML代码:

  <html>
    <body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-href="http://www.heretheweb.com" data-send="false" data-layout="button_count" data-width="240" data-show-faces="false"></div>
<div class="fb-comments" data-href="http://www.heretheweb.com" data-num-posts="5" data-width="470"></div>
<body>
</html>

周一的第一个实施是在4个平台上成功,包括模拟器(iOS 4和iOS 5),iPhone 3G iOS4和iPhone 4 iOS 5。 星期二,我一直在开发这个,最后我的自定义UIWebView在前三个没有问题。但是在iPhone 4(iOS 5)上,Web视图一遍又一遍地重新加载相同的网页,导致评论框永远不会出现。该URL是:

https://m.facebook.com/plugins/comments.php?channel_url=http%3A%2F%2Fstatic.ak.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D4%23cb%3Df28c738848%26origin%3Dhttp%253A%252F%252Fwww.heretheweb.com%252Ff3fdf142e8%26domain%3Dwww.heretheweb.com%26relation%3Dparent.parent&href=http%3A%2F%2Fwww.heretheweb.com&locale=en_US&mobile=true&numposts=5&sdk=joey&width=470

我真的不知道我做错了什么,我已经清理了uiWebView委托方法,我已经检查了断点所有我可以覆盖的方法。没什么......网页在开头加载,然后循环尝试加载上面的URL ......

4 个答案:

答案 0 :(得分:5)

facebook评论插件中存在一个错误,当在启用Retina的设备上加载评论插件时会导致无限加载循环。

其中一个fb js脚本中有一行如下:

if(window.devicePixelRatio>1)document.location.reload()

因此,如果您在具有高密度屏幕的设备上访问该页面,则注定要失败。

我报告了问题here

我提出了一个肮脏的黑客来修复它,但在使用之前要三思而后行,它可能随时停止工作。

请注意,此方法仅在您在UIWebView中嵌入插件时才有效,如果您在Safari中访问某个页面时遇到问题,则除了等待来自Facebook的修复之外别无其他选择。

我的想法是,当它被UIWebView加载时,即时'修复'js代码。

为了动态处理请求,我创建了自己的NSURLProtocol实现:

<FBCommentsFixingURLProtocol.h>

#import <Foundation/Foundation.h>

@interface FBCommentsFixingURLProtocol : NSURLProtocol

@end

<FBCommentsFixingURLProtocol.m>
#import "FBCommentsFixingURLProtocol.h"


static NSString *FBCommentsFixingHeader = @"X-FBFix";

@interface FBCommentsFixingURLProtocol () 
@property (nonatomic, readwrite, strong) NSURLRequest *request;
@property (nonatomic, readwrite, strong) NSURLConnection *connection;
@property (nonatomic, readwrite, strong) NSURLResponse *response;

@end

@implementation FBCommentsFixingURLProtocol
@synthesize request = request_;
@synthesize connection = connection_;
@synthesize response = response_;


+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    if (([request.URL.scheme isEqualToString:@"https"] || [request.URL.scheme     isEqualToString:@"http"]) && [request.URL.absoluteString rangeOfString:@"facebook.com/plugins/comments.php"].location != NSNotFound &&
    [request valueForHTTPHeaderField:FBCommentsFixingHeader] == nil)
{
    return YES;
}
return NO;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
    return request;
}

- (id)initWithRequest:(NSURLRequest *)request
       cachedResponse:(NSCachedURLResponse *)cachedResponse
           client:(id <NSURLProtocolClient>)client
{
    // Modify request so we don't loop
    NSMutableURLRequest *myRequest = [request mutableCopy];
    [myRequest setValue:@"" forHTTPHeaderField:FBCommentsFixingHeader];
    self = [super initWithRequest:myRequest
               cachedResponse:cachedResponse
                       client:client];
    if (self)
    {
       [self setRequest:myRequest];
    }
    return self;
}


- (void)startLoading
{
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:[self request]
                                                            delegate:self];
    [self setConnection:connection];

}

- (void)stopLoading
{
    [[self connection] cancel];
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *dataAsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
     //Just modify the script to prevent it from execution on Retina devices.
    //window.devicePixelRatio = 2 for the Retina Display
    NSString* modified =  [dataAsString stringByReplacingOccurrencesOfString:@"if(window.devicePixelRatio>1)document.location.reload();" withString:@""];
    NSData* dataMod=[modified dataUsingEncoding:NSUTF8StringEncoding];
    [[self client] URLProtocol:self didLoadData:dataMod];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [[self client] URLProtocol:self didFailWithError:error];
    [self setConnection:nil];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self setResponse:response];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [[self client] URLProtocolDidFinishLoading:self];
    [self setConnection:nil];
}


@end

然后我在app delegate didFinishLaunchingWithOptions中注册了它:

[NSURLProtocol registerClass:[FBCommentsFixingURLProtocol class]];

我知道这是一个肮脏的黑客,但仍然有效。

答案 1 :(得分:1)

好的......解决了。 出于一些奇怪和奇怪的原因,有时只在iPhone 4(不是模拟器,iPhone 3G或iPad 2)中加载评论Web部件时,它会尝试重新加载一次(Cache-Control标头设置为max-age = 0 ,所以它一直强迫重新加载)

解决方案是检查UIWebViewNavigationType,如果它等于UIWebViewNavigationTypeReload,那么shouldStartLoadWithRequest:返回NO。

真的很棘手¬¬

答案 2 :(得分:0)

我在safari / ios mobile下在sencha touch 1上开发的网络应用程序中遇到同样的问题, 它可以在其他浏览器和SO下工作,但在此基础上,它会继续加载和加载。 我找不到线索。

答案 3 :(得分:0)

以下代码对我来说工作正常

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
NSString *html = @"<div id=\"fb-root\"></div><script>(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = \"https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.5\";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script><div class=\"fb-comments\" data-href=\"URL_OF_YOUR_PAGE\" data-numposts=\"5\"></div>";
    [webView loadHTMLString:html baseURL:[NSURL URLWithString:@"https://facebook.com"]];
    [self.view addSubview:webView];