IOS: Facebook Comments Plugin mobile continues to reboot

I recently tried to integrate Facebook social plugins into the UIWebView user interface in iOS to comment on a web page. I added a Like button, as well as a comment plugin. Here is the HTML code that I upload to the web view:

<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> 

The first implementation on Monday was successful on four platforms: both simulators (iOS 4 and iOS 5), iPhone 3G iOS4 and iPhone 4 iOS 5. On Tuesday I continued to develop this, and in the end I got the UIWebView user interface without problems in the first three . But on iPhone 4 (iOS 5), web browsing continued to reload the same web page again and again, as a result of which the comment window never appeared. This url is:

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 & sumposts = 5 & true & numposts = 5

I do not know what I'm doing wrong, I cleared the uiWebView delegation methods, I have a check with breakpoints ALL methods that I could override. Nothing ... The web page loads at the beginning, and then it tries to load the above URL ...

+6
source share
4 answers

Good ... decided. For some strange and strange reason, sometimes when loading the comment web part ONLY on iPhone 4 (not a simulator, iPhone 3G or iPad 2), it tries to restart once and again (the Cache-Control header is set to max-age = 0, so it continues to cause a reboot)

The solution was to check the UIWebViewNavigationType if it is equal to UIWebViewNavigationTypeReload, shouldStartLoadWithRequest: returns NO.

It was REALLY difficult:

+1
source

An error appears in the facebook comment plugin that causes an endless loading cycle when the comment plugin is loaded on devices that support Retina.

One of the fb js scripts has a line that looks like this:

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

therefore, if you are viewing a page on a device with a high-density screen, you are doomed.

I reported a problem here

I came up with a dirty hack to fix this, but think twice before using it, it may stop working at any time.

Please note that this approach only works when you insert the plug-in into UIWebView, if you have a problem when you access the page on safari, there is no other choice but to wait for a fix from facebook.

My idea was to "fix" the js code on the fly when it loads using UIWebView.

To process requests on the fly, I created my own implementation of 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 

And then I registered it in the delegate’s doing didFinishLaunchingWithOptions:

 [NSURLProtocol registerClass:[FBCommentsFixingURLProtocol class]]; 

I know this is a dirty hack, but still it works.

+5
source

I have the same problem in Safari / ios mobile, in a web application developed on sencha touch 1, it works under other browsers and SO, but on this it continues to download and upload. I can not find a clue.

0
source

Below code works fine for me

 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]; 
0
source

Source: https://habr.com/ru/post/913523/


All Articles