Lup*_*upi 6 facebook ios4 ios ios-simulator ios5
最近我一直在尝试将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>
Run Code Online (Sandbox Code Playgroud)
星期一的第一个实现是在4个平台上成功,包括模拟器(iOS 4和iOS 5),iPhone 3G iOS4和iPhone 4 iOS 5.星期二,我一直在开发这个,最后我的自定义UIWebView工作没有问题前三个.但是在iPhone 4(iOS 5)上,Web视图一遍又一遍地重新加载相同的网页,导致评论框永远不会出现.该URL是这样的:
我真的不知道我做错了什么,我已经清理了uiWebView委托方法,我已经检查了断点所有我可以覆盖的方法.没什么......网页在开头加载,然后循环尝试加载上面的URL ...
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
Run Code Online (Sandbox Code Playgroud)
然后我在app delegate didFinishLaunchingWithOptions中注册了它:
[NSURLProtocol registerClass:[FBCommentsFixingURLProtocol class]];
Run Code Online (Sandbox Code Playgroud)
我知道这是一个肮脏的黑客,但它仍然有效.
好的...已解决。\n由于一些奇怪和奇怪的原因,有时仅在 iPhone 4(不是模拟器、iPhone 3G 或 iPad 2)中加载评论 Web 部件时,它会尝试一次又一次重新加载(缓存控制标头为设置为 max-age=0,因此它会不断强制重新加载)
\n\n解决方案是检查 UIWebViewNavigationType,如果它等于 UIWebViewNavigationTypeReload,shouldStartLoadWithRequest: 返回 NO。
\n\n这真的很棘手 \xc2\xac\xc2\xac
\n| 归档时间: |
|
| 查看次数: |
2204 次 |
| 最近记录: |