Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

actionscript 3 - Facebook abobe as3 api for air for mobile

I am planing a project that involves a cross platform (Android and I.O.S) mobile app that logs in using Facebook. I have no experience with the face book API and cant find any use full material for newbies. I want to use air for its cross platform capabilities so want to avoid multiple solutions for each platform. I have done many searches for help but haven't found much. Can any of you point me to resources you found use full starting off with this sort of thing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The AS3 Facebook API is all you need. ( http://code.google.com/p/facebook-actionscript-api/ ) Maybe you will have to change a few things (like the JSON methods in there) but otherwise it seems to work alright. You can download several examples from there as well, you can see the usage for different types of environment.

Also, read this article from Tom Krcha http://www.adobe.com/devnet/games/articles/getting-started-with-facebooksdk-actionscript3.html

If you have more specific questions, ask. This one is too generic.

EDIT: Here is a class I wrote some time ago for a small project

package com.company.social {

    import com.facebook.graph.FacebookMobile;
    import com.company.AppConst;
    import com.company.IDestroyable;
    import com.company.Main;
    import com.company.displayassets.WebViewCloseStripe;
    import com.company.events.FacebookControllerEvent;
    import com.company.events.TwitterControllerEvent;

    import flash.display.BitmapData;
    import flash.display.PNGEncoderOptions;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    import flash.media.StageWebView;
    import flash.utils.ByteArray;
    import flash.utils.clearTimeout;
    import flash.utils.setTimeout;

    public class FacebookController extends EventDispatcher implements IDestroyable {

        private static const APP_ID:String = "1234512345"; // Your App ID.
        private static const SITE_URL:String = "some_url";
        //Extended permission to access other parts of the user's profile that may be private, or if your application needs to publish content to Facebook on a user's behalf.
        private var _extendedPermissions:Array = ["publish_stream","user_website","user_status","user_about_me"];
        private var _stage:Stage;
        private var _webView:StageWebView;
        private var _topStripe:WebViewCloseStripe;
        private var _activity:String;
        private var _timeoutID:uint;
        public static const ACTIVITY_LOGIN:String = "login";
        public static const ACTIVITY_POST:String = "post";

        public function FacebookController(stage:Stage) {
            _stage = stage;
            init();
        }

        private function init():void {
            _activity = ACTIVITY_LOGIN;
            startTimeout();
            FacebookMobile.init(APP_ID, onHandleInit, null);
        }

        private function onHandleInit(response:Object, fail:Object):void {
            if (response) {
                stopTimeout();
                dispatchEvent(new FacebookControllerEvent(FacebookControllerEvent.LOGIN_COMPLETE));
                //FacebookMobile.api("/me", handleUserInfo);
            }
            else {
                /*trace("no response, login -->");
                for(var prop in fail["error"]) {
                    trace(prop+": "+fail["error"][prop]);
                }*/
                loginUser();
            }
        }

        private function startTimeout():void {
            trace("timeout start");
            clearTimeout(_timeoutID);
            _timeoutID = setTimeout(timeout, AppConst.TIMEOUT_TIME);
        }

        private function timeout():void {
            trace("timed out");
            clearTimeout(_timeoutID);
            dispatchEvent(new FacebookControllerEvent(FacebookControllerEvent.TIMEOUT));
        }

        private function stopTimeout():void {
            trace("timeout stop");
            clearTimeout(_timeoutID);
        }

        private function loginUser():void {
            stopTimeout();
            _topStripe = new WebViewCloseStripe();
            _topStripe.getCloseButton().addEventListener(MouseEvent.CLICK, closeClickHandler);
            _stage.addChild(_topStripe);

            _webView = new StageWebView();
            _webView.viewPort = new Rectangle(0, _topStripe.height, _stage.fullScreenWidth, _stage.fullScreenHeight - _topStripe.height);

            FacebookMobile.login(handleLogin, _stage, _extendedPermissions, _webView);
        }

        private function handleLogin(response:Object, fail:Object):void {
            if(_topStripe) {
                _topStripe.getCloseButton().removeEventListener(MouseEvent.CLICK, closeClickHandler);
                _topStripe.destroy();
                _stage.removeChild(_topStripe);
                _topStripe = null;
            }
            if(_webView) {
                _webView = null;
            }
            if(response) {
                dispatchEvent(new FacebookControllerEvent(FacebookControllerEvent.LOGIN_COMPLETE));
                //FacebookMobile.api('/me', handleUserInfo);
            }
            else {
                dispatchEvent(new FacebookControllerEvent(FacebookControllerEvent.LOGIN_ERROR));
            }
        }

        private function closeClickHandler(e:MouseEvent):void {
            dispatchEvent(new FacebookControllerEvent(FacebookControllerEvent.CLOSE));
        }

        private function handleUserInfo(response:Object, fail:Object):void {
            if (response) {
                for(var prop in response) {
                    trace(prop+": "+response[prop]);
                }
            }
        }

        private function handleUploadImage(result:Object, fail:Object):void {
            stopTimeout();
            if(result) {
                dispatchEvent(new FacebookControllerEvent(FacebookControllerEvent.POST_COMPLETE));
            }
            else {
                dispatchEvent(new FacebookControllerEvent(FacebookControllerEvent.POST_ERROR));
            }
        }

        public function postWithImage(message:String, imageData:BitmapData):void {
            _activity = ACTIVITY_POST;
            var byteArray:ByteArray = imageData.encode(new Rectangle(0, 0, imageData.width, imageData.height), new PNGEncoderOptions()); 
            var params: Object = new Object;
            params.image = byteArray;
            params.fileName = "image.png";
            params.message = message;
            startTimeout();
            FacebookMobile.api("/me/photos", handleUploadImage, params, "POST");
        }

        public function reset():void {
            FacebookMobile.logout(handleReset, SITE_URL);
        }

        public function handleReset(response:Object):void {
            dispatchEvent(new FacebookControllerEvent(FacebookControllerEvent.RESET));
        }

        public function destroy():void {
            if(_webView) {
                _webView.dispose();
                _webView = null;
            }

            if(_topStripe) {
                _topStripe.getCloseButton().removeEventListener(MouseEvent.CLICK, closeClickHandler);
                _topStripe.destroy();
                _stage.removeChild(_topStripe);
                _topStripe = null;
            }

            _stage = null;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...