This project is part of the ChilliSource framework developed by BlueChilli.
ChilliSource.Mobile.Facebook
provides simplified access to Facebook's identity and media sharing features.
To use Facebook's login and identity functionality, first initialize the ChilliSource.Mobile.Facebook.IIdentityService
dependency service:
var identityService = DependencyService.Get<IIdentityService>();
You must also add the following code to your AppDelegate.cs
for iOS:
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
// We need to handle URLs by passing them to their own OpenUrl in order to make the SSO authentication work.
return ApplicationDelegate.SharedInstance.OpenUrl(application, url, sourceApplication, annotation);
}
Logging In
var permissions = { "public_profile", "email" };
var fields = { "id", "name", "email", "gender", "age_range", "first_name", "last_name",
"is_verified", "verified", "picture" };
var result = await identityService.Login(fields, permissions);
if (result.IsSuccessful)
{
var token = result.Result.Token;
Console.WriteLine("Facebook token: " + token);
}
This will present the standard Facebook login page and return back to the application once the user has logged in.
Logging Out
identityService.Logout();
Retrieving User Info
var userInfoResult = await Global.Instance.FacebookIdentityProvider.GetUserInfo(token, fields);
if (userInfoResult.IsSuccessful)
{
JObject data = userInfoResult.Result.Data;
var name = data["name"].ToString();
var facebookId = data["id"].ToString();
}
To use Facebook's media sharing functionality, first initialize the ChilliSource.Mobile.Facebook.ISharingService
dependency service:
var sharingService = DependencyService.Get<ISharingService>();
Invoking any of the sharing methods below will present a Facebook popup prompting the user to share the specified link/video/images.
Sharing a Link
var result = sharingService.ShareLink(linkUrl, hashtags);
Sharing Images
You can share multiple images at the same time by passing in a List of string paths.
var result = sharingService.ShareImageFiles(imagePaths, hashtags);
Note that the image paths have to point to assets in the media library. Use the ChilliSource.Mobile.Media.IMediaService
dependency service to retrieve the media library url for an asset.
Sharing a Video
var result = sharingService.ShareVideoFile(videoPath, hashtags);
Note that the video path has to point to an asset in the media library. Use the ChilliSource.Mobile.Media.IMediaService
dependency service to retrieve the media library url for an asset.
To use Facebook's AppEvents
to log analytics data, first initialize the ChilliSource.Mobile.Facebook.IAppEventsService
dependency service:
var appEventsService = DependencyService.Get<IAppEventsService>();
Then add the following code in your AppDelegate.cs
for iOS:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
...
if (options == null || options[UIApplication.LaunchOptionsUrlKey] == null)
{
var result = await appEventsService.FetchDeferredAppLink();
if (result.IsSuccessful)
{
UIApplication.SharedApplication.OpenUrl(NSUrl.FromString(result.Result));
}
}
...
}
public override void OnActivated(UIApplication uiApplication)
{
appEventsService?.ActivateApp();
}
To log custom events simply call:
appEventsService.LogEvent(eventTitle);
The library is available via NuGet here.
See the releases.
Please see the Contribution Guide.
ChilliSource.Mobile is licensed under the MIT license.
For questions or feedback, please contact chillisource@bluechilli.com.