Trying to load Facebook-Comments plugin in a UIWebView

爷,独闯天下 提交于 2019-12-09 12:01:29

问题


I'm trying to view a facebook-comments plugin in a UIWebView on an iPhone application.

I began with Facebook's iOS tutorial, where I implemented Single Sign-On. The app delegate takes care of Facebook's Single Sign-on, then redirects me to my single view controller, which displays a title bar and a UIWebView.

I load the webview with a local file called "comments.html" which has been added and copied to my bundle/project directory.

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"comments" ofType:@"html"] isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

My html file is the bare-bones version of the plugin implementation, as generated at the Facebook Comments Plugin description site. I plugged in a url and clicked "Get Code." I took the HTML5 code that they provided me, and stuck it in an html file as so

<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_GB/all.js#xfbml=1";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>

    <div class="fb-comments" data-href="http://www.bombslo.com" data-num-posts="4" data-width="470"></div>

</body>
</html>

The UIWebView loads just fine on my simulator. I hosted and launched the html on my device and it loads fine in Safari. The problem is that the UIWebView will not load the Facebook-comments plugin in an embedded UIWebView.

An important thing to note is that I am not getting a blank screen. I see two animated loading bars, typical to Facebook. Also note that the Facebook-generated code comes in both HTML5 and XFBML. I've tried using both.


回答1:


I had the same issue. But after integrating with Facebook iOS sdk and by putting the following statement inside the AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
facebook = [[Facebook alloc] initWithAppId:@"your app id" andDelegate:self];
....
}

It resolves my problem.

Good luck!




回答2:


This problem seems to be resolved by adding http:// to the js.src

Try changing the FB script to this.

js.src = "http://connect.facebook.net/en_GB/all.js#xfbml=1";



回答3:


Invoking a feed dialoge unexpectedly did the trick for me! I called

[facebook dialog:@"feed" andDelegate:self];

and when closed, the comments plugin is properly loaded.




回答4:


I tried all of these solution, but none of these works. The thing that works for me is setting the Base URL of the UIWebView. According to the example of the question, lets say i've read data into variable htmlStr then the loading strategy would be like following

webview.loadHTMLString(htmlStr, baseURL: URL(string: "http://www.bombslo.com")!)

This works for me.



来源:https://stackoverflow.com/questions/10809249/trying-to-load-facebook-comments-plugin-in-a-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!