Example: Serving the Appropriate Javascript Libraries
To provide the correct version of Javascript libraries, create
a separate bundle for each Salesforce
Mobile SDK version you use. Then, provide Apex code on the server
that downloads the required version.
-
For each Salesforce
Mobile SDK version that your application supports, do the following.
- Create a ZIP file containing the Javascript libraries from the intended SDK version.
- Upload the ZIP file to your org as a static resource.
For example, if you ship a client that uses Salesforce Mobile SDK v. 1.3, add these files to your ZIP file:
- cordova.force.js
- SalesforceOAuthPlugin.js
- bootconfig.js
- cordova-1.8.1.js, which you should rename as cordova.js
-
Create an Apex controller that determines which bundle
to use. In your controller code, parse the user agent string to find
which version the client is using.
- In your org, from Setup, click .
- Create a new Apex controller named SDKLibController with
the following
definition.
1public class SDKLibController { 2 public String getSDKLib() { 3 String userAgent = 4 ApexPages.currentPage(). 5 getHeaders().get('User-Agent'); 6 7 if (userAgent.contains('SalesforceMobileSDK/1.3')) { 8 return 'sdklib13'; 9 } 10 // Add if statements for other SalesforceSDK versions 11 // for which you provide library bundles. 12 } 13}
-
Create a Visualforce page for each library in the bundle,
and use that page to redirect the client to that library.For example, for the SalesforceOAuthPlugin library:
- In your org, from Setup, enter Visualforce Pages in the Quick Find box, then select Visualforce Pages.
- Create a new page called “SalesforceOAuthPlugin” with the following
definition.
1<apex:page controller="SDKLibController" 2 action="{!URLFor($Resource[SDKLib], 3 'SalesforceOAuthPlugin.js')}"> 4</apex:page> - Reference the VisualForce page in a <script> tag in your
HTML code. Be sure to point to the page you created in step 3b. For
example:
1<script type="text/javascript" 2 src="/apex/SalesforceOAuthPlugin" />