Ios 嵌入的Facebook帖子未在UIWebView中正确显示
我试图在Ios 嵌入的Facebook帖子未在UIWebView中正确显示,ios,facebook,uiwebview,Ios,Facebook,Uiwebview,我试图在UIWebView中显示facebook嵌入的帖子。 我从facebook开发者门户获取示例代码,并将其加载到UIWebView。 并且只能看到空白屏幕 然后我把这段代码放在本地html文件中,并在Chrome中打开它。仍然没什么,但我看到嵌入出现在半秒钟内,然后消失了。 我尝试了许多不同的嵌入式html示例,得到了相同的结果 这是我的密码: <html> <body> <div id="fb-root"></div> <script
UIWebView
中显示facebook嵌入的帖子。
我从facebook开发者门户获取示例代码,并将其加载到UIWebView
。
并且只能看到空白屏幕
然后我把这段代码放在本地html文件中,并在Chrome中打开它。仍然没什么,但我看到嵌入出现在半秒钟内,然后消失了。
我尝试了许多不同的嵌入式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 = "https://connect.facebook.net/ru_RU/sdk.js#xfbml=1&version=v2.3&appId=334367389999440";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-post" data-href="https://www.facebook.com/FacebookDevelopers/posts/10151471074398553" data-width="500">
<div class="fb-xfbml-parse-ignore">
<blockquote cite="https://www.facebook.com/FacebookDevelopers/posts/10151471074398553"><p>Be the first to know
when we release a new video by subscribing to our official Facebook Developers YouTube channel!
http://www.youtube.com/facebookdevelopers</p>Posted by <a
href="https://www.facebook.com/FacebookDevelopers">Facebook Developers</a> on <a
href="https://www.facebook.com/FacebookDevelopers/posts/10151471074398553">29 Май 2013 г.</a>
</blockquote>
</div>
</div>
</body>
</html>
(功能(d、s、id){
var js,fjs=d.getElementsByTagName[0];
if(d.getElementById(id))返回;
js=d.createElement;
js.id=id;
js.src=”https://connect.facebook.net/ru_RU/sdk.js#xfbml=1&version=v2.3&appId=334367389999440";
fjs.parentNode.insertBefore(js,fjs);
}(文档“脚本”、“facebook jssdk”);
第一个知道
当我们通过订阅官方Facebook开发者YouTube频道发布新视频时!
http://www.youtube.com/facebookdevelopers在上发布
我找不到在HTML中添加脚本的任何理由,正如您所说,您在FB文档中找到了示例代码
以下代码工作正常:-
<html>
<body>
<div id="fb-root"></div>
<div class="fb-post" data-href="https://www.facebook.com/FacebookDevelopers/posts/10151471074398553" data-width="500">
<div class="fb-xfbml-parse-ignore">
<blockquote cite="https://www.facebook.com/FacebookDevelopers/posts/10151471074398553"><p>Be the first to know
when we release a new video by subscribing to our official Facebook Developers YouTube channel!
http://www.youtube.com/facebookdevelopers</p>Posted by <a
href="https://www.facebook.com/FacebookDevelopers">Facebook Developers</a> on <a
href="https://www.facebook.com/FacebookDevelopers/posts/10151471074398553">29 Май 2013 г.</a>
</blockquote>
</div>
</div>
</body>
</html>
第一个知道
当我们通过订阅官方Facebook开发者YouTube频道发布新视频时!
http://www.youtube.com/facebookdevelopers在上发布
解决此问题的唯一方法是使用FacebookEmbed创建代理服务。在将HTML代码插入UIWebView之前,请检查fb post和fb视频:
// Facebook Video Embed
if ([HTMLcontent rangeOfString:@"fb-video"].location != NSNotFound) {
[self appendFBEmbed:htmlContent type:@"video"];
}
// Facebook Post Embed
if ([HTMLcontent rangeOfString:@"fb-post"].location != NSNotFound) {
[self appendFBEmbed:htmlContent type:@"post"];
}
... // Your some code before </body></html>
[htmlContent appendString:@"</body></html>"];
[[self webView] loadHTMLString:[self HTMLcontent] baseURL:nil];
...
- (void)appendFBEmbed:(NSMutableString*)html type:(NSString* )type
{
NSString *locale = getYourCurrentLocaleIdentifier();
[html appendFormat:@"<script type=\"text/javascript\">"];
[html appendFormat:@"var items = document.getElementsByClassName('fb-%@');", type];
[html appendFormat:@"for (i=0; i<items.length;i++) {"];
[html appendFormat:@" var item = items[i];"];
[html appendFormat:@" var url = item.getAttribute(\"data-href\");"];
[html appendFormat:@" var src = 'http://YOUR-APP-HOST/facebook/emded.html?type=%@&url=' + encodeURIComponent(url) + '&locale=%@&width=%d';", type, locale, getYourCurrentDisplayWidth()];
[html appendFormat:@" item.innerHTML = '<iframe class=\"facebook fb-%@\" frameborder=\"0\" src=\"' + src + '\"></iframe>';", type];
[html appendFormat:@"}"];
[html appendFormat:@"</script>"];
}
//Facebook视频嵌入
如果([HTMLcontent rangeOfString:@“fb视频”].location!=NSNotFound){
[self AppendfBedded:htmlContent类型:@“视频”];
}
//Facebook帖子嵌入
if([HTMLcontent rangeOfString:@“fb post”].location!=NSNotFound){
[self-appendfbedded:htmlContent类型:@“post”];
}
... // 你以前有一些代码
[htmlContent appendString:@”“;
[[self webView]loadHTMLString:[self HTMLcontent]baseURL:nil];
...
-(void)appendFBEmbed:(NSMutableString*)html类型:(NSString*)类型
{
NSString*locale=getYourCurrentLocaleIdentifier();
[html格式:@”“;
[html appendFormat:@“var items=document.getElementsByClassName('fb-%@');”,类型];
[html appendFormat:@”用于(i=0;i在其中添加脚本的原因是什么?我也有同样的问题。当将包含此内容的本地html文件加载到Chrome中时,我将其放在实际的服务器上,并像其他任何网站一样点击它,视频最终显示出来。这一定与尝试使用非“http(s)”协议加载html有关“。是的,这段代码很好,但是帖子显示时没有任何“facebook风格”。只有纯文本。我想像许多网站一样嵌入帖子。据我所知,脚本需要加载facebook css、加载图像和处理操作。嗯,这不是嵌入在屏幕截图中的Twitter吗?
// FacebookController.php
<?php
class FacebookController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$type = trim($this->request->get('type'));
if (in_array($type, ['post', 'video']) === FALSE) {
throw new \Exception('Unknown fb-post type');
}
$url = trim($this->request->get('url'));
$host = parse_url($url, \PHP_URL_HOST);
if ($host === FALSE) {
throw new \Exception('Invalid URL'); // URL is not URL
}
if ($host !== NULL /* related URL */ && $host !== 'facebook.com' && $host !== 'www.facebook.com') {
throw new \Exception('Forbidden URL'); // Not a Facebook-URL
}
$locale = trim($this->request->get('locale'));
if ($locale) {
if (strlen($locale) > 10) {
throw new \Exception('Invalid Locale'); //
}
$localesXML = new \SimpleXMLElement(ROOT_PATH . '/../data/facebook-locales.xml', NULL, TRUE); // Where ROOT_PATH . '/../data/facebook-locales.xml is a local copy of https://www.facebook.com/translations/FacebookLocales.xml
$result = $localesXML->xpath('//representation[.="' . htmlentities($locale) . '"]');
if (count($result) > 0) {
$locale = (string) $result[0];
} else {
$locale = NULL;
}
} else {
$locale = NULL;
}
if ($locale === NULL) {
$locale = "en_US";
}
$width = intval(trim($this->request->get('width')));
if ($width < 100 || $width > 3000) {
throw new \Exception('Invalid width param');
}
$viewData = [
'type' => $type,
'url' => $url,
'width' => $width,
'locale' => $locale,
];
foreach ($viewData as $k => $v) {
$this->view->setVar($k, $v);
}
}
}
<!-- /views/facebook/index.phtml -->
<html>
<body>
<div class="fb-<?= $type ?>" data-href="<?= $url ?>" data-width="<?= $width ?>"></div>
<script src="https://connect.facebook.net/<?= $locale ?>/sdk.js#xfbml=1&version=v2.3"></script>
</body>
</html>