facebook 评论插件中存在一个错误,当评论插件在启用 Retina 的设备上加载时,该错误会导致无限加载循环。
在一个 fb js 脚本中有一行如下所示:
if(window.devicePixelRatio>1)document.location.reload()
因此,如果您在具有高密度屏幕的设备上访问该页面,那么您注定要失败。
我在这里报告了这个问题
我想出了一个肮脏的黑客来修复它,但在使用它之前要三思而后行,它可能随时停止工作。
请注意,此方法仅在您将插件嵌入 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
然后我在应用程序委托 didFinishLaunchingWithOptions 中注册了它:
[NSURLProtocol registerClass:[FBCommentsFixingURLProtocol class]];
我知道这是一个肮脏的黑客,但它仍然有效。