Search code examples
iosfacebookios5ios4ios-simulator

iOS: Facebook Comments Plugin mobile keeps reloading


Recently I have been trying to integrate Facebook Social Plugins into a custom UIWebView in iOS for commenting a webpage. I have added the Like Button as well as the Comments plugin. Here is the HTML code I load into 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 sucessfull in 4 platforms, both simulators (iOS 4 and iOS 5), iPhone 3G iOS4 and iPhone 4 iOS 5. On Tuesday, I kept developing this and I ended up with my custom UIWebView working with no problems in the first three. But on iPhone 4 (iOS 5) the web view kept reloading the same web page over and over again, resulting in the comment box never appearing. That URL is this:

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

I don't know what I am doing wrong really, I have cleaned the uiWebView delegate methods, I have check with breakpoints ALL the methods I could override. Nothing... The webpage gets loaded at the beginning, and then it loops trying to load the above URL...


Solution

  • Ok... solved. For some strange and weird reason, sometimes when loading the comments web part ONLY in iPhone 4 (not simulator, iPhone 3G or iPad 2) it tries to reload once and once again (the Cache-Control header is set to max-age=0, so it keeps forcing the reload)

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

    It was REALLY tricky ¬¬