Adding Facebook / Twitter / MySpace / Digg / Reddit / Bitly share functionality to your ActionScript 3/Flex application
Posted by Ian Serlin | Tweet This | Filed under Actionscript 3, Adobe Flex, Javascript, Social networking
I’m not sure that I need to write anything about the importance of including social sharing tools in any project destined for more than a few people to use, but in case you think I do, here goes:
If you make something that peoples reputations could benefit from being associated with and that thing is easy to share, people will share it… and more people will use it.
Problem
There are many different social sharing APIs and you’d like to integrate some of these into your Flash/Flex project. Unfortunately, wrangling everything together isn’t as easy as it is in Javascript as long as tools like ShareThis and AddThis continue to treat Flash apps as second class citizens.
Solution
The Share class let’s you initialize the title, description and custom tweet text associated with your content and then easily launch the share pages for Twitter, Facebook, MySpace, Digg and Reddit pre-populated with your specified text. As an added bonus, when you initialize the Share class it will contact bitly to create a bitly url you can use for the current page your swf is embedded on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | package { import flash.errors.IllegalOperationError; import flash.events.Event; import flash.events.HTTPStatusEvent; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.navigateToURL; import flash.external.ExternalInterface; public class Share { public static const BITLY_USERNAME:String = ""; public static const BITLY_API_KEY:String = ""; public var title:String = ""; public var description:String = ""; public var tweet_text:String = ""; public var bitlyURL:String; private var _window_location:String; /** * The Singleton instance of the ShareManager to use when accessing properties and methods of the ShareManager. */ public static const instance:Share = new Share( ShareSingletonLock ); public function Share( lock:Class ){ if( lock != ShareSingletonLock ){ throw new IllegalOperationError( "ShareManager is a Singleton, please use the static instance method instead." ); }else{ _window_location = ExternalInterface.call( "function () { return window.location.href.toString().toLowerCase(); }" ); if( _window_location != "" ){ try{ getBitlyURL(); }catch( e:Error ){ trace( "ShareManager::Constructor > error getting bitly url: " + getBitlyURL() ); } } } } public function init( title:String, description:String, tweet_text:String ):void { this.title = title; this.description = description; this.tweet_text = tweet_text; } private function getBitlyURL():void { var bitly_api:String = "http://api.bit.ly/shorten?version=2.0.1&longUrl="; bitly_api += encodeURIComponent( _window_location ); bitly_api += "&login=" + BITLY_USERNAME; bitly_api += "&apiKey=" + BITLY_API_KEY; var loader:URLLoader = new URLLoader(); loader.addEventListener( Event.COMPLETE, handleBitlyComplete ); loader.addEventListener( HTTPStatusEvent.HTTP_STATUS, handleBitlyStatus ); loader.addEventListener( IOErrorEvent.IO_ERROR, handleBitlyError ); loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, handleBitlyError ); loader.load( new URLRequest( bitly_api ) ); } private function handleBitlyComplete( e:Event ):void { trace( 'BITLY url load complete: ' + e ); var loader:URLLoader = e.target as URLLoader; try{ var response:Object = ExternalInterface.call("function(){return " + ( loader.data as String ) + "}"); if( response.statusCode == "OK" && response.results[ _window_location ] != null && response.results[ _window_location ].errorCode == null ){ bitlyURL = response.results[ _window_location].shortUrl; trace( 'Set BITLY url to: ' + bitlyURL ); } }catch( e:Error ){ trace( "Error decoding BITLY response (" + loader.data + "): " + e ); } } private function handleBitlyStatus( e:HTTPStatusEvent ):void { trace( 'BITLY status: ' + e ); } private function handleBitlyError( e:Event ):void { trace( 'Getting BITLY url failed with error: ' + e ); } public function get redditURL():String { return 'http://www.reddit.com/submit?url=' + encodeURIComponent( _window_location ) + "&title=" + encodeURIComponent( title ); } public function openReddit():void { navigateToURL( new URLRequest( redditURL ), '_blank' ); } public function get diggURL():String { var url:String = "http://digg.com/submit?url=" + encodeURIComponent( _window_location ); url += "&title=" + encodeURIComponent( title ); url += "&bodytext=" + encodeURIComponent( description ); url += "&media=video"; return url; } public function openDigg():void { navigateToURL( new URLRequest( diggURL ), '_blank' ); } public function get twitterURL():String { var url_for_tweet:String = bitlyURL ? bitlyURL : _window_location; var twitter_status:String = encodeURIComponent( tweet_text ); return "http://twitter.com/home/?status=" + twitter_status; } public function openTwitter():void { navigateToURL( new URLRequest( twitterURL ), '_blank' ); } public function get mySpaceURL():String { var url:String = "http://www.myspace.com/index.cfm?fuseaction=postto"; url += "&t=" + encodeURIComponent( title ); url += "&c=" + encodeURIComponent( description ); url += "&u=" + encodeURIComponent( _window_location ); url += "&r=close"; return url; } public function openMySpace():void { navigateToURL( new URLRequest( mySpaceURL ), '_blank' ); } public function get facebookURL():String { var url:String = "http://www.facebook.com/share.php?src=bm&v=4&i=1254383639"; url += "&u=" + encodeURIComponent( _window_location ); url += "&t=" + encodeURIComponent( title ); return url; } public function openFacebook():void { navigateToURL( new URLRequest( facebookURL ), '_blank' ); } } } class ShareSingletonLock {} |
If you’ve created your own sharing API or you improve the above write it up and let me know in the comments!