Skip to content

Commit 224c054

Browse files
author
Pat Patterson
committed
Updated README for Cordova 4.3.0, made sample filename more explicit.
1 parent 42abd3d commit 224c054

File tree

2 files changed

+61
-48
lines changed

2 files changed

+61
-48
lines changed

README.markdown

+61-48
Original file line numberDiff line numberDiff line change
@@ -151,39 +151,50 @@ Your HTML page will need to include jQuery and the toolkit, then create a client
151151

152152
More fully featured samples are provided in [example.html](Force.com-JavaScript-REST-Toolkit/blob/master/example.html) and [mobile.html](Force.com-JavaScript-REST-Toolkit/blob/master/mobile.html).
153153

154-
Using the Toolkit in a PhoneGap app
155-
-----------------------------------
154+
Using the Toolkit in a Cordova app
155+
----------------------------------
156156

157-
Your HTML page will need to include jQuery, the toolkit, PhoneGap and the ChildBrowser plugin, then create a client object, passing a session ID to the constructor. You can use __https://login.salesforce.com/services/oauth2/success__ as the redirect URI and catch the page load in ChildBrowser.
157+
Your HTML page will need to include jQuery, the toolkit and Cordova. You will also need to install the [InAppBrowser](http://plugins.cordova.io/#/package/org.apache.cordova.inappbrowser) plugin to be able to pop up a browser window for authentication. Create a client object, passing a session ID to the constructor. You can use __https://login.salesforce.com/services/oauth2/success__ as the redirect URI and catch the page load in InAppBrowser.
158158

159159
An absolutely minimal sample using OAuth to obtain a session ID is:
160160

161+
<!DOCTYPE html>
161162
<html>
162163
<head>
163-
<script type="text/javascript" src="static/jquery.js"></script>
164-
<script type="text/javascript" src="forcetk.js"></script>
165-
<script type="text/javascript" src="phonegap.0.9.5.min.js"></script>
166-
<script type="text/javascript" src="ChildBrowser.js"></script>
167-
<script type="text/javascript">
164+
<title>ForceTK Demo</title>
165+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
166+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
167+
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
168+
<script type="text/javascript" src="js/forcetk.js"></script>
169+
<script type="text/javascript" src="cordova.js"></script>
170+
<script type="text/javascript">
168171
// OAuth Configuration
169172
var loginUrl = 'https://login.salesforce.com/';
170-
var clientId = 'YOUR_CLIENT_ID';
173+
var clientId = '3MVG9Km_cBLhsuPzTtcGHsZpj9HSp.uUwbHupEXhWi6k3JJphEv8swpsUYIFCZSLp8pi7YYMbRjeQUxptYdIt';
171174
var redirectUri = 'https://login.salesforce.com/services/oauth2/success';
172175

173176
var client = new forcetk.Client(clientId, loginUrl);
174177

175-
$(document).ready(function() {
176-
var cb = ChildBrowser.install();
177-
$('#login').click(function(e) {
178-
e.preventDefault();
179-
cb.onLocationChange = function(loc){
180-
if (loc.startsWith(redirectUri)) {
181-
cb.close();
182-
sessionCallback(unescape(loc));
183-
}
184-
};
185-
cb.showWebPage(getAuthorizeUrl(loginUrl, clientId, redirectUri));
186-
});
178+
// Make our own startsWith utility fn
179+
if (!String.prototype.startsWith) {
180+
String.prototype.startsWith = function(searchString, position) {
181+
position = position || 0;
182+
return this.lastIndexOf(searchString, position) === position;
183+
};
184+
}
185+
186+
document.addEventListener("deviceready", function(){
187+
$('#login').click(function(e) {
188+
e.preventDefault();
189+
var ref = window.open(getAuthorizeUrl(loginUrl, clientId, redirectUri),
190+
'_blank', 'location=no,toolbar=no');
191+
ref.addEventListener('loadstop', function(evt) {
192+
if (evt.url.startsWith(redirectUri)) {
193+
ref.close();
194+
sessionCallback(unescape(evt.url));
195+
}
196+
});
197+
});
187198
});
188199

189200
function getAuthorizeUrl(loginUrl, clientId, redirectUri){
@@ -192,41 +203,43 @@ An absolutely minimal sample using OAuth to obtain a session ID is:
192203
+'&redirect_uri='+escape(redirectUri);
193204
}
194205
195-
function sessionCallback(loc) {
196-
var oauthResponse = {};
197-
198-
var fragment = loc.split("#")[1];
199-
200-
if (fragment) {
201-
var nvps = fragment.split('&');
202-
for (var nvp in nvps) {
203-
var parts = nvps[nvp].split('=');
204-
oauthResponse[parts[0]] = unescape(parts[1]);
205-
}
206-
}
207-
208-
if (typeof oauthResponse === 'undefined'
209-
|| typeof oauthResponse['access_token'] === 'undefined') {
210-
errorCallback({
211-
status: 0,
212-
statusText: 'Unauthorized',
213-
responseText: 'No OAuth response'
214-
});
215-
} else {
216-
client.setSessionToken(oauthResponse.access_token, null,
206+
function sessionCallback(loc) {
207+
var oauthResponse = {};
208+
209+
var fragment = loc.split("#")[1];
210+
211+
if (fragment) {
212+
var nvps = fragment.split('&');
213+
for (var nvp in nvps) {
214+
var parts = nvps[nvp].split('=');
215+
oauthResponse[parts[0]] = unescape(parts[1]);
216+
}
217+
}
218+
219+
if (typeof oauthResponse === 'undefined'
220+
|| typeof oauthResponse['access_token'] === 'undefined') {
221+
alert("Unauthorized: No OAuth response");
222+
} else {
223+
client.setSessionToken(oauthResponse.access_token, null,
217224
oauthResponse.instance_url);
218-
225+
219226
client.query("SELECT Name FROM Account LIMIT 1",
220227
function(response){
221228
$('#message').text('The first account I see is '
222229
+response.records[0].Name);
223230
}
224231
);
225-
}
226-
}
232+
}
233+
}
227234
</script>
228-
<p id="message">Click here.</p>
235+
<head>
236+
<body>
237+
<button id="login">Click here to login</button>
238+
<p id="message"></p>
239+
</body>
229240
</html>
230241

231-
A fully featured sample (including persistence of the OAuth refresh token to the iOS Keychain) is provided in [phonegap.html](Force.com-JavaScript-REST-Toolkit/blob/master/phonegap.html).
242+
A fully featured sample (including persistence of the OAuth refresh token to the iOS Keychain) is provided in [cordova-ios.html](Force.com-JavaScript-REST-Toolkit/blob/master/cordova-ios.html). The sample uses Cordova 4.3.0 and the InAppBrowser and iOS Keychain plugins. Install these with
232243

244+
cordova plugin add org.apache.cordova.inappbrowser
245+
cordova plugin add com.shazron.cordova.plugin.keychainutil
File renamed without changes.

0 commit comments

Comments
 (0)