开发者

Adding Facebook's Like button inside iOS WebView

I use a UIWebView to run the facebook code. The problem is that the WebView is not authenticated in facebook so a login is required.

My user is already logged in to facebook using the native API (that uses Safari).

How can I authenticate the user in the webview in the background using the access-token so that when the "like" is displayed the user is already authenticated?

Load the html code below once and perform the login. Session information is stored and the like button will work fine. Clean your facebook cookies and reload the html. The session is loaded from the localStorage but Login is required again.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    
    <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>  
</head>
<body onload="onload()开发者_C百科">
    Hello Facebook, can you hear me?

    <div id="fb-root" >     
        <fb:like href='http://www.stackoverflow.com' send='true' layout='button_count' width='220' show_faces='true' font='arial' ref='dropalert'></fb:like>        
    </div>

    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
    <script type="text/javascript">
        function onload() {         
            var session = null;
            var sessionStr = localStorage.getItem("session");
            if (sessionStr){
                session = JSON.parse(sessionStr);
                alert("stored session " + sessionStr);
            }
            else {
                alert("session not stored");
            }
            FB.init({
                appId   : <your-app-id>,
                session : session,        
                cookie  : true,                     
                xfbml   : true              
            });

            FB.getLoginStatus(function(response) {
                alert("getLoginStatus");                
                if (response.status == "connected") {
                    alert("Logged in");
                    parseFBML();
                } 
                else {
                    // no user session available, someone you dont know
                    alert("NOT Logged in");
                    login();
                }               
            });
        }

        function login() {
            alert("login starting");
            // Login to facebook to get an access-token
            FB.login(function (response) {  
                if (response.session) {
                    var sessionStr = JSON.stringify(response.session)
                    alert("session: \n\n" + sessionStr + "\n\n\n\n");
                    localStorage.setItem("session", sessionStr);                    
                } 
                else {
                    alert("Login failed");
                }
            }, {scope:'publish_stream,offline_access'});
        }    

        function parseFBML() {
            FB.XFBML.parse(document.getElementById('fb-root'));
        }       
    </script>
</body>
</html>

Thanks.


I don't know if this is an option for you but, if you can pass a session parameter to your web content you can use it to initialise the JavaScript SDK within your web content.

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId  : <my-app-id>,
    session : { "uid":"<users facebook id>", "access_token":"<valid access token>", "expires": <expires value>, "sig":"<valid signature>"},
    cookie : true, 
    xfbml  : true
  });
</script>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜