Skip to content

Commit

Permalink
Merge pull request Wizcorp#68 from sgrebnov/master
Browse files Browse the repository at this point in the history
windows phone plugin version - not tested / experimental!!!
  • Loading branch information
Dave Johnson committed Mar 22, 2012
2 parents 1373af2 + f06b07b commit ab0e674
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 0 deletions.
46 changes: 46 additions & 0 deletions www/windows-phone/README.md
@@ -0,0 +1,46 @@
PREREQUISITES
================================

Plugin requires ChildBrowser plugin to work properly. You can find it at https://github.com/purplecabbage/phonegap-plugins/tree/master/WindowsPhone/ChildBrowser

Getting Started
===============

Please use guidance below to start working with the plugin; facebook.html located in this directory provides full usage example.

1. Add js references, for example

<script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
<script type="text/javascript" charset="utf-8" src="pg-plugin-fb-connect.js"></script>
<script type="text/javascript" charset="utf-8" src="facebook_js_sdk.js"></script>
<script type="text/javascript" charset="utf-8" src="ChildBrowser.js"></script>

2. Initialize plugin

document.addEventListener('deviceready', function () {
try {

FB.Cookie.setEnabled(true); // this seems to be duplicate to 'cookie: true' below, but it is IMPORTANT due to FB implementation logic.

FB.init({ appId: "311961255484993", nativeInterface: PG.FB, cookie: true });

} catch (e) {
//alert(e);
console.log("Init error: " + e);
}
}, false);

3. Call facebook api

FB.api('/me/friends', function (response) {
if (response.error) {
console.log(JSON.stringify(response.error));
} else {
var data = document.getElementById('data');
response.data.forEach(function (item) {
var d = document.createElement('div');
d.innerHTML = item.name;
data.appendChild(d);
});
}
});
151 changes: 151 additions & 0 deletions www/windows-phone/facebook.html
@@ -0,0 +1,151 @@
<!DOCTYPE HTML>
<html>
<head>
<!-- meta name="viewport" content="width=device-width, height=device-height, user-scalable=yes, initial-scale=2.0, maximum-scale=4.0, minimum-scale=1.0" / -->
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> <!-- ISO-8859-1 -->
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen"/>

<script type="text/javascript">
// provide our own console if it does not exist, huge dev aid!
if (typeof window.console == "undefined") {
window.console = { log: function (str) { window.external.Notify(str); } };
}

// output any errors to console log, created above.
window.onerror = function (e) {
console.log("window.onerror ::" + JSON.stringify(e));
};

console.log("Installed console ! ");
</script>

<script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
<script type="text/javascript" charset="utf-8" src="pg-plugin-fb-connect.js"></script>
<script type="text/javascript" charset="utf-8" src="facebook_js_sdk.js"></script>
<script type="text/javascript" charset="utf-8" src="ChildBrowser.js"></script>

<script type="text/javascript" charset="utf-8">
/**
* Function called when page has finished loading.
*/
function init() {
document.addEventListener("deviceready", function () {
deviceReady = true;
console.log("Device=" + device.platform + " " + device.version);
}, false);
window.setTimeout(function () {
if (!deviceReady) {
console.log("Error: PhoneGap did not initialize. Demo will not run correctly.");
}
}, 1000);
}

</script>

</head>
<body>
<button onclick="login()">Login</button>
<button onclick="me()">Me</button>
<button onclick="getSession()">Get session</button>
<button onclick="getLoginStatus()">Get login</button>
<button onclick="logout()">Logout</button>

<div id="data">loading ...</div>

<script>

if (typeof PhoneGap == 'undefined') console.log('PhoneGap variable does not exist. Check that you have included phonegap.js correctly');
if (typeof PG == 'undefined') console.log('PG variable does not exist. Check that you have included pg-plugin-fb-connect.js correctly');
if (typeof FB == 'undefined') console.log('FB variable does not exist. Check that you have included the Facebook JS SDK file.');

FB.Event.subscribe('auth.login', function (response) {
console.log('auth.login event');
});

FB.Event.subscribe('auth.logout', function (response) {
console.log('auth.logout event');
});

FB.Event.subscribe('auth.sessionChange', function (response) {
console.log('auth.sessionChange event');
});

FB.Event.subscribe('auth.statusChange', function (response) {
console.log('auth.statusChange event');
});

function getSession() {
var session = FB.getSession();
if (session == null) {
console.log("session is null");
}
else {
console.log(JSON.stringify(session));
}
}

function getLoginStatus() {
FB.getLoginStatus(function (response) {
if (response.session) {
console.log('logged in');
} else {
console.log('not logged in');
}
});
}

function me() {
FB.api('/me/friends', function (response) {
if (response.error) {
console.log(JSON.stringify(response.error));
} else {
var data = document.getElementById('data');
response.data.forEach(function (item) {
var d = document.createElement('div');
d.innerHTML = item.name;
data.appendChild(d);
});
}
});
}

function logout() {
FB.logout(function (response) {
console.log('logged out');
});
}

function login() {
FB.login(
function (response) {
if (response.session) {
console.log('logged in');
} else {
console.log('not logged in');
}
},
{ scope: 'email, read_stream, read_friendlists' }
);
}

document.addEventListener('deviceready', function () {
try {
//alert('Device is ready! Make sure you set your app_id below this alert.');
console.log('Device is ready! Make sure you set your app_id below this alert.');

FB.Cookie.setEnabled(true); // this seems to be duplicate to 'cookie: true' below, but it is IMPORTANT due to FB implementation logic.

FB.init({ appId: "311961255484993", nativeInterface: PG.FB, cookie: true });

document.getElementById('data').innerHTML = "";
} catch (e) {
//alert(e);
console.log("Init error: " + e);
}
}, false);
</script>
<div id="log"></div>
</body>
</html>
187 changes: 187 additions & 0 deletions www/windows-phone/pg-plugin-fb-connect.js
@@ -0,0 +1,187 @@
/* MIT licensed */
// (c) 2011 Dave Johnson, Nitobi
// (c) 2011 Sergey Grebnov
// based on https://github.com/davejohnson/phonegap-plugin-facebook-connect/blob/master/www/pg-plugin-fb-connect.js
// and FBConnect.js (c) 2010 Jesse MacFadyen, Nitobi

PG = ( typeof PG == 'undefined' ? {} : PG );
PG.FB = {
init: function (apiKey) {
console.log("fb-connect init");

// create the fb-root element if it doesn't exist
if (!document.getElementById('fb-root')) {
var elem = document.createElement('div');
elem.id = 'fb-root';
document.body.appendChild(elem);
}

// initializes connection module
FB.fbConnect = FBConnect.install(apiKey, "http://www.facebook.com/connect/login_success.html", "touch");
FB.fbConnect.restoreLastSession();

var session = FB.fbConnect.session;

if (session && session.expires > new Date().valueOf()) {
FB.Auth.setSession(session, 'connected');
}
},
login: function (a, b) {
console.log("fb-connect login");

FB.fbConnect.connect(b.scope);

FB.fbConnect.onConnect = function (e) {
FB.fbConnect.saveSession();
FB.Auth.setSession(FB.fbConnect.session, 'connected');
if (a) a(e);
}
},
logout: function (cb) {
console.log("fb-connect logout");

FB.fbConnect.logout();

FB.fbConnect.onDisconnect = function (e) {
FB.fbConnect.saveSession();
FB.Auth.setSession(null, 'notConnected');
if (cb) cb(e);
}
},
getLoginStatus: function (cb) {
console.log("fb-connect getLoginStatus");

if (FB.fbConnect.session && FB.fbConnect.session.expires > new Date().valueOf()) {
FB.fbConnect.status = "connected";
}
else {
FB.fbConnect.status = "unknown";
}

if (cb) cb(FB.fbConnect);
}
};

/**
* FBConnect implements user authentication logic and session information store
*/

function FBConnect(client_id, redirect_uri, display) {

this.client_id = client_id;
this.redirect_uri = redirect_uri;
this.display = display;

this.resetSession();

if (window.plugins.childBrowser == null) {
ChildBrowser.install();
}

}

/**
* User login
*/
FBConnect.prototype.connect = function (scope) {

var authorize_url = "https://graph.facebook.com/oauth/authorize?";
authorize_url += "client_id=" + this.client_id;
authorize_url += "&redirect_uri=" + this.redirect_uri;
authorize_url += "&display=" + (this.display ? this.display : "touch");
authorize_url += "&type=user_agent";

// extended permissions http://developers.facebook.com/docs/reference/api/permissions/
if (scope) {
authorize_url += "&scope=" + scope;
}

window.plugins.childBrowser.showWebPage(authorize_url);
var self = this;
window.plugins.childBrowser.onLocationChange = function (loc) { self.onLoginLocationChange(loc); };
}

FBConnect.prototype.onLoginLocationChange = function (newLoc) {
if (newLoc.indexOf(this.redirect_uri) == 0) {
var result = unescape(newLoc).split("#")[1];
result = unescape(result);

// TODO: Error Check
this.session.access_token = result.split("&")[0].split("=")[1];
var expiresIn = parseInt(result.split("&")[1].split("=")[1]);
this.session.expires = new Date().valueOf() + expiresIn * 1000;
this.status = "connected";

window.plugins.childBrowser.close();
this.onConnect(this);

}
}

/**
* User logout
*/
FBConnect.prototype.logout = function () {
var authorize_url = "https://www.facebook.com/logout.php?";
authorize_url += "&next=" + this.redirect_uri;
authorize_url += "&access_token=" + this.session.access_token;
console.log("logout url: " + authorize_url);
window.plugins.childBrowser.showWebPage(authorize_url);
var self = this;
window.plugins.childBrowser.onLocationChange = function (loc) {
console.log("onLogout");
window.plugins.childBrowser.close();
self.resetSession();
self.status = "notConnected";
self.onDisconnect(this);
};
}

/**
* Example method - returns your friends
*/
FBConnect.prototype.getFriends = function () {
var url = "https://graph.facebook.com/me/friends?access_token=" + this.session.access_token;
var req = new XMLHttpRequest();

req.open("get", url, true);
req.send(null);
req.onerror = function () { alert("Error"); };
return req;
}

// Note: this plugin does NOT install itself, call this method some time after deviceready to install it
// it will be returned, and also available globally from window.plugins.fbConnect
FBConnect.install = function (client_id, redirect_uri, display) {
if (!window.plugins) {
window.plugins = {};
}
window.plugins.fbConnect = new FBConnect(client_id, redirect_uri, display);

return window.plugins.fbConnect;
}

/**
* Session management functionality
*/
FBConnect.prototype.resetSession = function () {
this.status = "unknown";
this.session = {};
this.session.access_token = null;
this.session.expires = 0;
this.session.secret = null;
this.session.session_key = null;
this.session.sig = null;
this.session.uid = null;
}

FBConnect.prototype.restoreLastSession = function () {
var session = JSON.parse(localStorage.getItem('pg_fb_session'));
if (session) {
this.session = session;
}
}

FBConnect.prototype.saveSession = function () {
localStorage.setItem('pg_fb_session', JSON.stringify(this.session));
}

0 comments on commit ab0e674

Please sign in to comment.