无法在iOS UIWebView中加载完整的Facebook评论插件

4
我有一个简单的ViewController,用于在UIWebView中加载FB评论插件。
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 1505.0f)];

    NSString * html = @"\
    <!DOCTYPE html>\
    <html xmlns:fb='http://ogp.me/ns/fb#'>\
    <head>\
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>\
    </head>\
    <body style='background-color:red;'>\
    \
    <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 = 'http://connect.facebook.net/en_US/all.js#xfbml=1&appId=xxx';\
    fjs.parentNode.insertBefore(js, fjs);\
    }(document, 'script', 'facebook-jssdk'));</script>\
    \
    <fb:comments href='http://example.com' num_posts='10'></fb:comments>\
    \
    </body>\
    </html>\
    ";

    [webView loadHTMLString:html baseURL:nil];
    [self.view addSubview:webView];

我能看到评论正在加载,但高度很奇怪,似乎自动调整大小失败了?如果我使用移动版Safari,则可以查看整个评论。

模拟器截图


你能找到一个合适的解决方案吗? - benuuu
4个回答

4
您可以使用这段代码在iOS UIWebview中显示Facebook评论。
  UIWebView *fbWebview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 1505)];
  [fbWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.facebook.com/Levis"]]];

CGRect scrollViewFrame = CGRectMake(0, 0, 320, 1024);
UIScrollView  *scrollViewMain = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
CGSize scrollViewContentSize = CGSizeMake(320, 1505);
[scrollViewMain setContentSize:scrollViewContentSize];
[scrollViewMain setBounces:NO];
[scrollViewMain setScrollEnabled:YES];
[scrollViewMain setShowsVerticalScrollIndicator:YES];

[scrollViewMain setBackgroundColor:[UIColor redColor]];
[scrollViewMain addSubview:fbWebview];

[self.view addSubview:scrollViewMain];

使用您的FB URL,而不是"https://www.facebook.com/Levis"。

这可能会对您有所帮助。


4
这并不是与 FB 评论插件相同的东西,它提供的功能与他所寻找的完全不同。你不能使用它来接收有关 URL example.com 的评论。 - Andy McSherry

2

你需要指定一个基础URL

[webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.example.com"]];

0

我也遇到了同样的问题。解决方案是不要使用本地 HTML 文件(或 HTML 字符串)。将 HTML 文件上传到服务器并使用以下方式:

NSURL *url = [NSURL URLWithString:@"http://www.blablblab.com/yourfile.html"]; 
[self.webview loadRequest:[NSURLRequest requestWithURL:url]];

而不是:

[self.webview loadHTMLString:localString baseURL:nil];

我还不知道为什么在文件放在服务器上时会有布局上的差异,但在本地文件时,我的Facebook评论视图的高度总是被减少到“height:160”。显然这是因为“all.js” Facebook脚本的原因(根据this question)。

0

@Howard你可以使用http://www.facebook.com/plugins/comments.php?href=http://www.google.com' scrolling='yes' profile='yes' frameborder='0' style='border:none; overflow:hidden; width:300px; height:30px;' data-href='http://www.google.com' allowTransparency='true'>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接