开发者

Facebook and twitter share for mobile web

Is there a special url for sharing to Facebook and Twitter for mobile? Or are they the same as those from websites?

Using

<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>   
<a href="javascript:return false;" rel="nofollow" onclick="window.open('https://twitter.com/share?text=sometext&url=someurl', 'Twitter', 'toolbar=0,status=0,width=626,height=436')">Twitter</a>

for Twitter and

<script>
    function fbs_click() 
    {
        u='www.something';
        t='title';
        window.open('http://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
        return false;
    }
</script>

<a rel="nofollow" href="http://www.facebook.com/sharer/sharer.php?u=someu开发者_运维技巧rl" onclick="return fbs_click()" target="_blank" class="">Facebook</a>

for Facebook.


Your sharing implementation should be the same and should work so long as your mobile users have devices capable or rendering JavaScript. Otherwise you can use each platforms' API url to share inside a link, the only problem is the callback redirects them either to Twitter or Facebook and they'll have to manually return to your site. The links are as below:

Sharing on Facebook:

http://m.facebook.com/sharer.php?u=<urlencoded url>t=<urlencoded title>

For example:

http://m.facebook.com/sharer.php?u=http://www.google.com

Updating status on Twitter:

http://mobile.twitter.com/home?status=<urlencoded status>


A couple of notes regarding Simpleton's answer.

  1. The http://m.facebook.com/sharer.php URL may not be what you want. The site that you provide in the u query parameter needs to have some tags in the page in order for the share page to be at all interesting. Just try putting another URL in there other than Google to see what I mean. I was never really able to figure out what it is looking for in the page of the site in order to show anything useful in your share page. I did not see any way to provide extra information about the site you are sharing.

  2. The t query parameter is no longer read by facebook as far as I could tell

  3. I found that the Facebook feed dialog was a better sharing alternative (https://developers.facebook.com/docs/reference/dialogs/feed/). See the example at the bottom of that page for the information you can share. Paste that example URL in to your browser to see what it looks like. You can play around with the parameters to see what each parameter controls in the post. For example, if you don't provide the caption parameter, it seems that the base URL of the link parameter is used in it's place. To use the feed dialog approach, you need to register your app in Facebook to get an app_id that you will need to include in the feed dialog URL. You also associate your app with a web site URL, which you also use in the feed dialog URL. If you want Facebook to serve you a mobile-friendly page, append &display=touch to the end of the feed dialog URL. Lastly, you must provide redirect_uri parameter where the user will be redirected (with the post ID included as a query parameter), so you have to have something at that URL to handle the response.

  4. Regarding the http://mobile.twitter.com/home?status URL, I learned a couple of things: first, any URL's in include in the status will not be shortened (bummer); second, if your status includes any single quotes, they will be encoded as ' (HTML entity code) but will not get decoded when your post the tweet. However, this doesn't happen if you are posting via the desktop, using the same URL. You can see what I mean by trying the following URL from both your iPhone (I was using Safari on iPhone) and then from your desktop (I was using Safari):

http://mobile.twitter.com/home?status=Wayne's%20World

Note, I do an javascript encodeURI on the status, but single quotes are not generally encodable characters by most URL/I encoders. There are some that will replace the single quote with a %27, but I tried inserting that manually and it still didn't get decoded in the status text.

I hope this information helps someone comes here looking for simple Facebook and Twitter sharing options.


Twitter - This is how I solved the tweet by url for all devices/browsers issue:

http://twitter.com/intent/tweet?text= + encodeURIComponent(tweet);

encodeURIComponent - is a built in javascript function, explained here.

The 'http://mobile.twitter.com/home?status=' is not supported, and you will get encoding issues when twitter requests a login.

Facebook - For facebook sharing I used Facebook API example


The url is ok, but you need to do some work on your server to get Facebook to really make it look decent. Facebook now uses "Open Graph", which allows you to add special tags to your web page that Facebook understands. You can define what types of media are on the page, even add your own objects with definitions you create inside your Facebook app. The link to the developer Open Graph objects page is Here.

A sample of Open Graph on a detail page might look like this:

<meta property="fb:app_id"            content="YOUR FACEBOOK_APP_ID }}" /> 
<meta property="og:url"               content="The URL that this page is on" /> 
<meta property="og:site_name"         content="Your domain" />
<meta property="og:title"             content="Title of your page" />
<meta property="og:type"              content="This is very important - it is how
  Facebook refers to your post.  Image, video, text, etc.  Even custom stuff is
  possible" />
<meta property="og:image"             content="image url for facebook to display on
the user's wall" />
<meta property="og:determiner"        content="auto" />   
<meta property="og:description"       content="A description you want with this
content" />

And so on. This all goes in the <head> of the page of the actual url you are supplying to Facebook. Then Facebook will translate this into the appropriate post. In addition, you need to set up the Open Graph on your apps developer page, so that Facebook knows to look for the og: tags. It's complicated, but it does work.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜