Using Facebook SDK native framework for iOS and Android for Social Login and more (Part 2)
January 30, 2017 Allen Drennan
January 30, 2017 Allen Drennan
[SHOWTOGROUPS=4,20]
In this article we will demonstrate how to embed the Android SDK for Facebook for using the Facebook APIs and simplifying social login in your mobile app. The techniques discussed in this article are also universal for adapting other 3rd party Android SDKs into Delphi apps.
Для просмотра ссылки Войдиили Зарегистрируйся focuses on Facebook/iOS, Для просмотра ссылки Войди или Зарегистрируйся focuses Facebook/Android, Part 3 focuses on Twitter/iOS and Part 4 focuses on Twitter/Android.
Introduction
If you are currently creating mobile apps and you need authentication, you have to seriously consider using social login. Many apps offer this functionality and users have a sense of comfort when they install your app and see that they can use their existing social credentials to interact with your app.
Facebook offers SDKs that you can embed into your iOS and Android applications. The current method that is widely recommended and used in Delphi is to authenticate using OAuth via a web browser. This method requires your user to launch a browser to authenticate and internally retrieve an OAuth token. While this works, it is not ideal because users launch the web browser while your app goes into the background and when authentication is completed or fails, must return to your app.
Additionally the user is presented with the same behavior on subsequent launches of your application to help verify your authenticating credentials. This is fine if necessary and as a backup method but it is hardly ideal when all you want to present a smoother experience for your user.
The beauty of embedding the SDK is that once the user approves your app for Facebook login, future app launches can be completely automatic and transparent because the Facebook framework abstracts the complexities of managing the access token and approval process for us.
For more information about us, our support and services visit the Для просмотра ссылки Войдиили Зарегистрируйся or the Для просмотра ссылки Войди или Зарегистрируйся.
The example contained here depends upon part of our Для просмотра ссылки Войдиили Зарегистрируйся.
The source code and related example repository are hosted on GitHub at Для просмотра ссылки Войдиили Зарегистрируйся.
Getting Started with the Facebook SDK for Android
To get started using the Facebook SDK for Android inside your Delphi application you need to take care of a few steps.
Create a Facebook developers account
You should start by creating a Facebook developers accounts at Для просмотра ссылки Войдиили Зарегистрируйся if you haven’t already done so.
To setup a new app for Facebook so you can test and build:
1. Select the Create New Facebook, then provide a Display Name, choose a Category and then click Create App ID.
In order to interact with the Facebook APIs from your Delphi Android application you need to supply a key hash. In Delphi there is a distinct key hash for any and all development test apps that applies to all apps you build in Debug configuration for a given installation of Delphi. However, for each app you build in Release configuration there is a different key hash.
For our purposes here let’s discuss how to create a Facebook key hash for your Delphi apps in Debug configuration.
To create the correct Key Hash for your Delphi application, navigate to the folder containing your debug.keystore file which is normally, C:\Users\name\AppData\Roaming\Embarcadero\BDS\version
From inside this folder, run the following command:
Apply the Facebook keyhash for Debug configuration apps
Return to your Facebook developers page at Для просмотра ссылки Войдиили Зарегистрируйся and select your application. Under the Setting page,
Creating a Facebook compatible keyhash for Release configuration apps
To create a keyhash for a Release build application you need to first go through the procedure at,
Для просмотра ссылки Войдиили Зарегистрируйся
The resulting .keystore file should be exported as a keyhash using the following command:
In the above example replace project.keystore with the actual name. When asked for a password, hit Enter. The output string is your project keyhash, so you should copy and paste it and add the keyhash to the list of Key Hashes under the Android application settings on the Facebook Developers console.
But since we are building a Debug configuration, for now, we don’t need this keyhash so you can skip this step.
Download the Facebook SDK for Android
You can download the Facebook SDK for Android from the following location, Для просмотра ссылки Войдиили Зарегистрируйся
At the time of writing this article, the current SDK version for Android is 4.19. Facebook routinely updates their APIs and changes the instructions and breaks things, so please be aware.
To extract the libraries required, unzip the SDK:
To add the Facebook SDK to your project simply add the facebook-sdk.jar file to the list of libraries for your Android target. You can also drag and drop the library onto your project in the Delphi Project Manager.
Copy the Facebook SDK resources to your project
Under the unzipped facebook folder \facebook\facebook-android-sdk-version\res copy the entire \res folder to a project location. We copy it to a project location because we will be adding additional resources that are project specific. We recommend something like Project\Resources\Android\res. We will refer to this as the resources \res folder from now on.
Create strings.xml in your \res\values folder
Create a new file under the \res\values folder called strings.xml that looks like the following:
The key facebook_app_id should be your actual Facebook App Id as presented in the developers console for Facebook and the app_name key should be a name of your choosing.
Modify your project’s AndroidManifest.template.xml
Your AndroidManifest.template.xml needs 3 new sections added to the list of keys that are specific to the Facebook SDK for Android.
Add the resources to the Delphi Deployment Manager for your Android project
Delphi will need to copy all the various Facebook SDK resources over to the target during the final build process so you must add them to the Delphi Deployment Manager. Those resources that you are adding to your project are under the Project\Resources\Android\res folder and subfolders if you copied them as recommended in the previous steps.
I bet you are wondering right now how long will it take to add more than 140 files to the Delphi Deployment Manager for your Android project? I would be ready to give up at this point and I wouldn’t blame you.
In Android Studio we simply specify our resource locations and Android Studio automatically makes sure that all the resources and sub-folder resources are collected and merged. Isn’t that nice?
Here at Grijjy we needed a solution so we developed our own Delphi Deployment Manager alternative (for iOS and Android platforms only) called Для просмотра ссылки Войдиили Зарегистрируйся.
[/SHOWTOGROUPS]
In this article we will demonstrate how to embed the Android SDK for Facebook for using the Facebook APIs and simplifying social login in your mobile app. The techniques discussed in this article are also universal for adapting other 3rd party Android SDKs into Delphi apps.
Для просмотра ссылки Войди
Introduction
If you are currently creating mobile apps and you need authentication, you have to seriously consider using social login. Many apps offer this functionality and users have a sense of comfort when they install your app and see that they can use their existing social credentials to interact with your app.
Facebook offers SDKs that you can embed into your iOS and Android applications. The current method that is widely recommended and used in Delphi is to authenticate using OAuth via a web browser. This method requires your user to launch a browser to authenticate and internally retrieve an OAuth token. While this works, it is not ideal because users launch the web browser while your app goes into the background and when authentication is completed or fails, must return to your app.
Additionally the user is presented with the same behavior on subsequent launches of your application to help verify your authenticating credentials. This is fine if necessary and as a backup method but it is hardly ideal when all you want to present a smoother experience for your user.
The beauty of embedding the SDK is that once the user approves your app for Facebook login, future app launches can be completely automatic and transparent because the Facebook framework abstracts the complexities of managing the access token and approval process for us.
For more information about us, our support and services visit the Для просмотра ссылки Войди
The example contained here depends upon part of our Для просмотра ссылки Войди
The source code and related example repository are hosted on GitHub at Для просмотра ссылки Войди
Getting Started with the Facebook SDK for Android
To get started using the Facebook SDK for Android inside your Delphi application you need to take care of a few steps.
Create a Facebook developers account
You should start by creating a Facebook developers accounts at Для просмотра ссылки Войди
To setup a new app for Facebook so you can test and build:
1. Select the Create New Facebook, then provide a Display Name, choose a Category and then click Create App ID.
- Once the app is created on Facebook’s dashboard, take note of the App ID. You will need this value to use the SDK from your Delphi app.
In order to interact with the Facebook APIs from your Delphi Android application you need to supply a key hash. In Delphi there is a distinct key hash for any and all development test apps that applies to all apps you build in Debug configuration for a given installation of Delphi. However, for each app you build in Release configuration there is a different key hash.
For our purposes here let’s discuss how to create a Facebook key hash for your Delphi apps in Debug configuration.
To create the correct Key Hash for your Delphi application, navigate to the folder containing your debug.keystore file which is normally, C:\Users\name\AppData\Roaming\Embarcadero\BDS\version
From inside this folder, run the following command:
1 | keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64 |
When asked for a password, hit Enter. The output string is your debug keyhash, so you should copy and paste it (ex: Hit Ctrl-A, Ctrl-C, etc. from a DOS command prompt).Note: This requires both Java runtime and OpenSSL binaries in the path.
Apply the Facebook keyhash for Debug configuration apps
Return to your Facebook developers page at Для просмотра ссылки Войди
- Select Add Platform button at the bottom and choose Android.
- Under Key Hashes add the keyhash string you copied above.
- Set Single Sign On to Yes.
- Click Save Changes.
Creating a Facebook compatible keyhash for Release configuration apps
To create a keyhash for a Release build application you need to first go through the procedure at,
Для просмотра ссылки Войди
The resulting .keystore file should be exported as a keyhash using the following command:
1 | keytool -exportcert -alias productionkey -keystore project.keystore | openssl sha1 -binary | openssl base64 |
In the above example replace project.keystore with the actual name. When asked for a password, hit Enter. The output string is your project keyhash, so you should copy and paste it and add the keyhash to the list of Key Hashes under the Android application settings on the Facebook Developers console.
But since we are building a Debug configuration, for now, we don’t need this keyhash so you can skip this step.
Download the Facebook SDK for Android
You can download the Facebook SDK for Android from the following location, Для просмотра ссылки Войди
At the time of writing this article, the current SDK version for Android is 4.19. Facebook routinely updates their APIs and changes the instructions and breaks things, so please be aware.
To extract the libraries required, unzip the SDK:
- Unzip the file under the facebook folder \facebook\facebook-android-sdk-version.aar where version represents the current version of the Facebook Android SDK.
Note: The file ends in the extension .AAR but it is internally just a .ZIP file. - Copy and rename the classes.jar to facebook-sdk.jar
- Move the facebook-sdk.jar to a shared library location.
To add the Facebook SDK to your project simply add the facebook-sdk.jar file to the list of libraries for your Android target. You can also drag and drop the library onto your project in the Delphi Project Manager.
Copy the Facebook SDK resources to your project
Under the unzipped facebook folder \facebook\facebook-android-sdk-version\res copy the entire \res folder to a project location. We copy it to a project location because we will be adding additional resources that are project specific. We recommend something like Project\Resources\Android\res. We will refer to this as the resources \res folder from now on.
Create strings.xml in your \res\values folder
Create a new file under the \res\values folder called strings.xml that looks like the following:
1 2 3 4 5 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="facebook_app_id">123456788012345</string> <string name="app_name">my app name</string> </resources> |
The key facebook_app_id should be your actual Facebook App Id as presented in the developers console for Facebook and the app_name key should be a name of your choosing.
Modify your project’s AndroidManifest.template.xml
Your AndroidManifest.template.xml needs 3 new sections added to the list of keys that are specific to the Facebook SDK for Android.
- Add uses-permission for android.permission.INTERNET
- Add new activity for com.facebook.FacebookActivity
- Add meta-data resource reference for your Facebook Application Id.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?xml version="1.0" encoding="utf-8"?> <!-- BEGIN_INCLUDE(manifest) --> <manifest xmlns:android="Для просмотра ссылки Войди package="%package%" android:versionCode="%versionCode%" android:versionName="%versionName%" android:installLocation="%installLocation%"> <!-- This is the platform API where NativeActivity was introduced. --> <uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" /> <%uses-permission%> <--- Add your uses permission here as follows: <uses-permission android:name="android.permission.INTERNET"/> <uses-feature android:glEsVersion="0x00020000" android:required="True"/> <application android android:restoreAnyVersion="%restoreAnyVersion%" android:label="%label%" android:debuggable="%debuggable%" android:largeHeap="%largeHeap%" android:icon="%icon%" android:theme="%theme%" android:hardwareAccelerated="%hardwareAccelerated%"> <%application-meta-data%> <%services%> <!-- Our activity is a subclass of the built-in NativeActivity framework class. This will take care of integrating with our NDK code. --> <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity" android:label="%activityLabel%" android:configChanges="orientation|keyboard|keyboardHidden|screenSize" android:launchMode="singleTask"> <!-- Tell NativeActivity the name of our .so --> <meta-data android:name="android.app.lib_name" android:value="%libNameValue%" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <-- Add your activity here as follows: <activity android:name="com.facebook.FacebookActivity" android:configChanges= "keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@String/app_name" /> <-- Add your meta-data reference here as follows: <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@String/facebook_app_id"/> <%activity%> <%receivers%> </application> </manifest> <!-- END_INCLUDE(manifest) --> |
Add the resources to the Delphi Deployment Manager for your Android project
Delphi will need to copy all the various Facebook SDK resources over to the target during the final build process so you must add them to the Delphi Deployment Manager. Those resources that you are adding to your project are under the Project\Resources\Android\res folder and subfolders if you copied them as recommended in the previous steps.
I bet you are wondering right now how long will it take to add more than 140 files to the Delphi Deployment Manager for your Android project? I would be ready to give up at this point and I wouldn’t blame you.
In Android Studio we simply specify our resource locations and Android Studio automatically makes sure that all the resources and sub-folder resources are collected and merged. Isn’t that nice?
Here at Grijjy we needed a solution so we developed our own Delphi Deployment Manager alternative (for iOS and Android platforms only) called Для просмотра ссылки Войди
[/SHOWTOGROUPS]