Installing Facebook Connect Plugin with Phonegap iOS app for beginners
Upcoming SlideShare
Loading in...5
×
 

Installing Facebook Connect Plugin with Phonegap iOS app for beginners

on

  • 4,132 views

This post has come out of my recent struggle to integrate the Facebook Connect plugin by Dave Johnson and make it work with my Phonegap iOS app. I had a hard time figuring things out as I was new to ...

This post has come out of my recent struggle to integrate the Facebook Connect plugin by Dave Johnson and make it work with my Phonegap iOS app. I had a hard time figuring things out as I was new to Phonegap iOS development. I was already doing Android apps with Phonegap, but iOS has given me some tough time. Nevertheless, around 2 days of gloomy and sad face was finally rewarded with a big smile. So, better I document it somewhere so that I do not struggle again and that’s where the inspiration for this post lies. And I must say, the official documentation is pathetic for newbies.
Alright, let’s get started. I will go step by step into the process with all details and screenshots so that it is very easy for you. In this post however, I will not be talking on how to create a Phonegap iOS app, or how to create Phonegap iOS plugins. For that you still need to look at the official documentation. I will only discuss on how we can install the Facebook plugin with a Phonegap iOS app and get started using it.
For the full blog post download this file or visit the actual blog post link: http://jbkflex.wordpress.com/2013/03/16/installing-facebook-connect-plugin-with-phonegap-ios-app-for-beginners/

Statistics

Views

Total Views
4,132
Views on SlideShare
4,132
Embed Views
0

Actions

Likes
0
Downloads
55
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Hats off to your document.
    We created phone gap app (cordova 2.2.0) with facebook login and tested in ios4.3 iPhone. It worked well when native FB is installed.
    Testing the same in iOS6.1, facing issue in FBlogin. It login to FB app and not redirecting to phone gap app. FB.login() doesn't return any response.
    Could you suggest any solution.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Installing Facebook Connect Plugin with Phonegap iOS app for beginners Installing Facebook Connect Plugin with Phonegap iOS app for beginners Document Transcript

    • Installing Facebook Connect Plugin with Phonegap iOS app for beginnersFor better readability and screenshots, visit the original blog post link: http://jbkflex.wordpress.com/2013/03/16/installing-facebook-connect-plugin-with-phonegap-ios-app-for-beginners/Joseph Khan, March 16,2013This post has come out of my recent struggle to integrate the Facebook Connect plugin by DaveJohnson and make it work with my Phonegap iOS app. I had a hard time figuring things out as I wasnew to Phonegap iOS development. I was already doing Android apps with Phonegap, but iOS has givenme some tough time. Nevertheless, around 2 days of gloomy and sad face was finally rewarded with a bigsmile. So, better I document it somewhere so that I do not struggle again and that’s where the inspirationfor this post lies. And I must say, the official documentation is pathetic for newbies.Alright, let’s get started. I will go step by step into the process with all details and screenshots so that it isvery easy for you. In this post however, I will not be talking on how to create a Phonegap iOS app, or howto create Phonegap iOS plugins. For that you still need to look at the official documentation. I will onlydiscuss on how we can install the Facebook plugin with a Phonegap iOS app and get started using it.Before moving further I would like to inform you that I am using Phonegap 2.2.0 for my demo. There arenew versions available – 2.5.0 being the latest at the time of writing. So you might want to checkthe official pages if you are using the latest version of Phonegap. But the steps mentioned below shouldwork with the new versions of Phonegap as well. OK, time to start now.Note: Phonegap and Cordova are the same (well, at least for me..). I prefer calling Phonegap.1) Do I need a Mac? Simple answer – Yes, you need a Mac. I have heard and read thousand timesabout people asking if iOS apps can be developed in a Windows machine. Simply, I just did not research,instead I have a Mac and I started on it. But the answer is you need a Mac definitely, since you will useXCode and the iOS SDK for development.2) Create a Phonegap iOS App - I am using Phonegap 2.2.0. I am not going to show how to create aPhonegap iOS app. For that look at this pdf documentation here. If you cannot open it, check outthis link which is the pdf source. This should get you started. However I have some screenshots belowwhich should also help you out.a) I have created a basic Phonegap app – FacebookPluginTest inside Cordova22FacebookTest folderunder Documents. See the screenshot below. I have used the Terminal to create the app. You can find
    • details about the command in the document above. So make sure you go through it once.b) Open the iOS app that you have just created in XCode by doubleclicking FacebookPluginTest.xcodeproj. This is how it looks inside XCode. You can see the www folderwhere we need to put our html/css/js code.
    • c) After that test it in a simulator. Here I am running my app in the simulator. If everything goes fine youcan see something similar like the screenshot below.3) Download the latest Facebook SDK from https://developers.facebook.com/ios/downloads and installit on your Mac. I am using the SDK3.1.1, but at the time of writing SDK3.2 is available. After installing youcan find the files in Documents folder (in case you did not specify any other folder at the time of install).See the screenshot below,4) Create a Facebook app – You have to create a Facebook app. Any interaction with the Facebook APIfrom your phonegap iOS app happens through an interface that is created by the Facebook app. So
    • navigate tohttps://developers.facebook.com/apps and create an app. Follow the steps given in point no 4– Create a Facebook App, from the getting started guide here– http://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/a) Use the Bundle Identifier of your Phonegap iOS project. Find it inside XCode’s Project Summary Page.It is the reverse domain url/package name that you typed while creating your project. Check out thescreenshot below.5) Next download the (Dave Jonhson’s) Phonegap Facebook Connect plugin from Github-https://github.com/phonegap/phonegap-facebook-plugin. Navigate to Github – and clone the repository.The files will be downloaded to your Mac. After you download you can find the plugin folder structure asspecified in the “Project Structure”section of the Plugin’s Github page (the readme file). It has theFacebook JavaScript SDK file, iOS Native Facebook Plugin Code and the plugin’s JavaScript interfacecode. We will need all of these to integrate with our Phonegap iOS project in XCode. We will see that laterin the post.6) Now that you have all things in place, let’s see how to integrate the plugin and start using it. If you haveany issues till now, carefully follow the steps above again.7) We already have created our basic Phonegap iOS project – FacebookPluginTest and it is currentlyopen inside XCode.8) Now drag the FacebookSDK.framework folder from the FacebookSDK installation folder (usuallyinside Documents) into the Frameworks section of the project navigator inside XCode. See the image
    • belowWith this step we are adding the FacebookSDK API to our Phonegap project. Choose ‘Create groups forany added folders’and deselect ‘Copy items into destination group’s folder (if needed)’ to keep the reference to the SDKinstallation folder, rather than creating a copy. See the image below. After that click on the Finish buttonto add the SDK. You can see it inside the Frameworks section of the project navigator inside XCode.
    • 9) Now Add the Facebook SDK for iOS resource bundle by draggingthe FacebookSDKResources.bundle file from theFacebookSDK.framework/Resources folder (underyour Facebook SDK installation folder, usually Documents..) into the Frameworks section of your ProjectNavigator inside XCode. Again choose ‘Create groups for any added folders’ and deselect ‘Copy itemsinto destination group’s folder (if needed)’. See the image below,10) Add the headers by dragging the DeprecatedHeaders folder fromtheFacebookSDK.framework/Versions/A/DeprecatedHeaders folder (under your Facebook SDKinstallation folder, usually Documents..) into the Frameworks section of your Project Navigator. Againchoose ‘Create groups for any added folders’ and deselect ‘Copy items into destination group’s folder (ifneeded)’. This adds the headers as a reference. See the image below,
    • 11) After adding the three frameworks your Frameworks folder in the project navigator should look like theimage below,For the three steps above you can find more information in the Getting Started Guide -Step5: “Configure a new XCode Project” from Facebook Developer’s page– http://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/12) The SDK relies on three other frameworks (AdSupport, Accounts and Social) to use the Facebookfeatures built into iOS6. We need to add these to our project. So click on your project name/icon (the
    • topmost item in the project navigator) and then click on the project name (FacebookPluginTest for us)under Targets. See the image belowThen click on the Build Phases tab in the summary page. Open the “Link Binary with Libraries” panel fromthe list. See the image below
    • Click on the (+) icon at the bottom of the panel to add the three frameworks. This will open up a dialogwith the list of all Frameworks. Then type Social in the search input box and this will filterthe Social.framework in the list below. Then add this framework by clicking on the Add button. See theimage below,Similarly add the other two frameworks – Accounts.framework and AdSupport.framework. Search inthe search box as above and then add it. Make sure you make the three frameworks optional. So usethe ‘Optional’ flag for iOS6-specific frameworks to support older versions of the iOS operating systems.
    • See the image below,After adding the three frameworks, the project navigator inside XCode looks like the image below.You can notice that the frameworks are added under your Project’s name. You can drag the threeframeworks to the Frameworks folder as shown in the image below. Or let it be where it is already, as thiswill not create any problem.
    • 13) Now we need to add the SQLite (-lsqlite3.0) SQL Library and -ObjC linker flags to our project inXCode. Click on the project name in the navigator (topmost item in project navigator) and click the projectname under Project (FacebookPluginTest for us…). After that click on the Build Settings tab. See theimage below,
    • Search for Other Linker Flags in the search box on the right. See the image below,Now add the values -lsqlite3.0 and -ObjC to the list. See the image below. (To get the pop up, doubleclick on the Other Linker Flags..)After adding it should look like the image below,14) Now we need to add the Facebook APP ID in our project’s info.plist file. You can find the FacebookAPP ID (if you have already created a Facebook App, if not see step 4 above) from the App summary
    • page inhttps://developers.facebook.com/apps. See the image below for helpNow Open the Resources folder in your project navigator and select the ProjectName-Info.plist file, forus it will be theFacebookPluginTest-Info.plist file. Right click on it and select Open As -> Source Code.See the image below,Now add the following to the file in the editor panel under the root <dict></dict> tags,<key>FacebookAppID</key><string>Add your APP Id here</string><key>CFBundleURLTypes</key><array>
    • <dict> <key>CFBundleURLName</key> <string>Your scheme id here</string> <key>CFBundleURLSchemes</key> <array> <string>fb[Your APP Id here]</string> </array> </dict></array>where Scheme Id is usually a unique identifier for the scheme, in reverse domain name notation (i.ecom.facebook.phonegap.myscheme). You can use any name in place of myscheme. See the imagebelow for helpThis step ensures correct re-direct from Mobile Safari or the Facebook app, after permissionauthorization. But be sure to use your own Facebook APP ID.
    • 15) Now we will add the Facebook JavaScript SDK file to our Phonegap app. So go to your downloadedPhonegap Facebook Connect plugin folder and copy facebook_js_sdk.js under lib folder intothe www directory of your project in Xcode. See the two images below
    • 16) Now we will add the Facebook plugin’s native iOS code to our project. So select the two files(FacebookConnectPlugin.h and FacebookConnectPlugin.m) inside native/ios folder under yourPhonegap Facebook plugin’s downloaded folder and drag the two files and add it to the Plugins folderinside XCode. Choose ‘Create groups for any added folders’ and deselect ‘Copy items into destinationgroup’s folder (if needed)’. See the image below,17) Next step is to add the java script interface of the Facebook plugin’s code. This acts as an interfacefor the native iOS plugin code and your html (note that every Facebook plugin has a javascript .js file andnative files which has native code). So open the www folder inside the downloaded Facebook pluginfolder and copy the cdv-plugin-fb-connect.js file into thewww folder of your project inside XCode. Seethe image below ,
    • Now, add the copied file to the www folder of your Phonegap iOS project like you did above when youadded the Facebook java script SDK file.18) Then we have to register the Facebook Connect plugin in the Plugins list. So open the Resourcesfolder in the project navigator inside XCode and select Cordova.plist. Then under Plugins section addthe following data – For the key, addorg.apache.cordova.facebook.Connect, and its value willbe FacebookConnectPlugin. See the image below
    • When it’s done, it should look like the image below19) After that we will have to add the Facebook domains to the External Host. The Facebook SDK will tryto access various URLs, and their domains must be whitelisted in your Cordova.plist underExternalHosts. You can either add each subdomain separately or you can allow all domains by addinga *. I opted for the later. See the image below,
    • With that we have successfully set up the Facebook Plugin with our Phonegap iOS app. For testing ifeverything is fine, run the app in the simulator. You should see like the image below. We have not triedthe Facebook API yet so you will see the Phonegap ready screen again. But this shows that we have setup everything correctly. There may be some warning messages but fortunately no compilation or runtime
    • errors.Time to run an example nowThe Phonegap Facebook Connect plugin comes with two ready to use examples – The Hackbook andthe Simple example. So copy the Hackbook contents from inside the examples folder under thedownloaded Facebook plugin folder and paste it inside the www folder of your Phonegap iOS project.See the image below,
    • When there are file conflicts during copying select Replace for all the options.Now the Hackbook example code is added to your project. Open the index.html file inside XCode forediting and add the Facebook APP Id to the JavaScript snippet. You can find it inside the callbackfunction of the device ready event. See the image below. Add your Facebook APP Id in the requiredplace.Give a final check. Check if cordova-2.2.0.js, cdv-plugin .js file and the facebook .js sdk file have beenreferenced inside individual <script> tags in the index.html file. It should be there. In case you are using adifferent Phonegap version make sure to rename the cordova-x.x.x.js file inside index.html to the version
    • that you are using.Everything looks fine now. Run the app in the simulator one final time. And Ecstasy!! The Hackbookexample opens up in the simulator. You should see something like the image below,Now in the Hackbook app, you can login and authorize the app and start using it. You can use theFacebook Graph API features there. Similarly you can try out the Simple example that ships with thePhonegap Facebook plugin download. Just copy the files from the downloaded plugin folder to the wwwfolder of your project. And test the app in the simulator.From here on you can use the Facebook API to add numerous features to your Phonegap iOS app. Ihope this will be helpful to all the newbies out there. Leave behind a comment below if I missedsomething or something was incorrect in this post.