从iOS端Facebook分享的预览效果到Open Graph Protocol(开放内容协议)

主要记录了在iOS端进行Facebook分享时遇到的一个问题.

设置Facebook分享的预览效果.

在iOS端将图片或链接分享至Facebook, 使用social框架即可非常方便地做到:

import social

func actionFacebookShare(sender: UIButton) {
    var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    controller.setInitialText("分享的内容")
    controller.addURL(url) // 分享的网页
    // controller.addImage(imageToShare) // 分享的图片
    self.presentViewController(controller, animated: true, completion: nil)
}

然而, 在SLComposeViewController中, 却遇到了不能显示分享网页中的预览图片的情况.
一番搜索之后, 发现Facebook分享中的预览效果是通过在网页中设置一些特殊的meta数据来实现的.
参考如下链接:

facebook-sharing-best-practices

How does Facebook Sharer select Images and other metadata when sharing my URL?

其大意是:

Facebook has a set of open-graph meta tags that it looks at to decide which image to show.
The keys one for the Facebook image are:
and it should be present inside the <head></head> tag at the top of your page.

<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
<meta property="og:image:secure_url" content="https://secure.example.com/ogp.jpg" />

即, 我们需要在网页中设置og:image作为预览图片, 另外, 还有og:title和og:description等其他所需的预览信息.
Facebook还贴心地提供了一系列的工具用于检测类似的情况.

tools-and-support

其中, Sharing Debugger专用于在FB上分享网页时的调试.
我们只需要输入分享链接, 即可查看分享的预览效果. 如下图:

facebook-share-debugger-1.png
facebook-share-debugger-2.png

可以看到关键的几个meta数据为og:title, og-description, og:image.
完整的meta数据如下:

<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=no" />
<meta property="og:title" content="AirBrush: The Secret to a Perfect Selfie" />
<meta property="og:type" content="article" />
<meta property="og:description" content="Use Airbrush for a free, easy-to-use photo editor that lets you enhance your natural beauty without harsh filters." />
<meta property="og:image" content="http://xiuxiu.mobile.meitudata.com/tuiguang/airbrush/download/img/bg_en.jpg" />
<meta property="og:image:secure_url" content="http://xiuxiu.mobile.meitudata.com/tuiguang/airbrush/download/img/bg_en.jpg" />
<meta property="og:url" content="http://xiuxiu.mobile.meitudata.com/tuiguang/airbrush/download/en/" />

以上, 都是前端开发的同学需要在网页中添加的meta数据.
如果实在没办法添加呢, Facebook也在自己的分享SDK中添加了对应的解决方案:

FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
params.link = [NSURL URLWithString:link];
params.name = title;
params.caption = title;
params.linkDescription = linkDescription;
params.picture = [NSURL URLWithString:pictureLink];

通过FBLinkShareParams可以添加分享网页的预览效果所需的meta数据.

那么, 看了这些后, 你肯定在想, 到底这个og是神马意思? 貌似之前接触的meta数据中没有这样的写法呢?
这就需要引入Open Graph Protocol了.

Open Graph Protocol

开放内容协议的主页: The Open Graph protocol.

The Open Graph protocol enables any web page to become a rich object in a social graph. For instance, this is used on Facebook to allow any web page to have the same functionality as any other object on Facebook.
While many different technologies and schemas exist and could be combined together, there isn't a single technology which provides enough information to richly represent any web page within the social graph. The Open Graph protocol builds on these existing technologies and gives developers one thing to implement. Developer simplicity is a key goal of the Open Graph protocol which has informed many of the technical design decisions.

即这种协议可以让网页成为一个"富媒体对象"。用了Meta Property=og标签,就是你同意了网页内容可以被其他社会化网站引用等,目前这种协议被SNS网站如Facebook、renren采用。
最基本的og标签有以下:

og:title - The title of your object as it should appear within the graph, e.g., "The Rock".
og:type - The type of your object, e.g., "video.movie". Depending on the type you specify, other properties may also be required.
og:image - An image URL which should represent your object within the graph.
og:url - The canonical URL of your object that will be used as its permanent ID in the graph, e.g., "http://www.imdb.com/title/tt0117500/".

此外, 还有一些标签如下:

og:audio - A URL to an audio file to accompany this object.
og:description - A one to two sentence description of your object.
og:determiner - The word that appears before this object's title in a sentence. An enum of (a, an, the, "", auto). If auto is chosen, the consumer of your data should chose between "a" or "an". Default is "" (blank).
og:locale - The locale these tags are marked up in. Of the format language_TERRITORY. Default is en_US.
og:locale:alternate - An array of other locales this page is available in.
og:site_name - If your object is part of a larger web site, the name which should be displayed for the overall site. e.g., "IMDb".
og:video - A URL to a video file that complements this object.

以及

og:image:url - Identical to og:image.
og:image:secure_url - An alternate url to use if the webpage requires HTTPS.
og:image:type - A MIME type for this image.
og:image:width - The number of pixels wide.
og:image:height - The number of pixels high.

Open Graph Protocol是Facebook主导的, 当然, 其他的一些公司可能有各自的一些格式, 如

<!-- for Google -->
<meta name="description" content="" />
<meta name="keywords" content="" />

<meta name="author" content="" />
<meta name="copyright" content="" />
<meta name="application-name" content="" />

<!-- for Facebook -->
<meta property="og:title" content="" />
<meta property="og:type" content="article" />
<meta property="og:image" content="" />
<meta property="og:url" content="" />
<meta property="og:description" content="" />

<!-- for Twitter -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="" />
<meta name="twitter:description" content="" />
<meta name="twitter:image" content="" />

因此, 再涉及到相关的使用场景时, 可去其各自的官网找寻.

其他问题

CDN

最后, 正确设置好了所有必须的Open Graph meta数据后, 还出现过Facebook分享预览效果不正确的情况, 诡异的是有的地区可以, 而有的地区却不行.
排查了之后, 发现是CDN相关的配置引起的, 可能需要做些网页部署相关的步骤.

可供参考的资料

facebook-sharing-best-practices
18 Meta Tags Every Webpage Should Have in 2013

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,568评论 25 707
  • 和煦:形容温暖的阳光。 版图:原指户籍和地图,今泛指国家的领土、疆域。 翩翩起舞:形容轻快地跳起舞来。载歌载舞、手...
    晨子超爱喝奶茶阅读 721评论 0 1
  • 今晚和我的男朋友聊了很多 突然才发现我和他之间的价值观实在是相差太多了,这个是我之前一直始料未及的。我总以为我们只...
    jinababy阅读 142评论 0 0
  • 服务器的多域名配置 1. 常用的WEB服务器有Apache和nginx,小编偏向使用nginx。日常开发机器使用的...
    OneTODO阅读 790评论 0 5
  • 忙碌,快节奏,往往会使人忘记快乐,忘记温柔。 做人一定要懂得关心身边的人,同事,普通人,陌生人…… 自己没付出爱是...
    成为富婆阅读 476评论 0 0