Skip to content

SNSFacebook Documentation

Guillaume Bonnin edited this page Jun 8, 2016 · 1 revision

Getting Started

Requirements

SNSFacebook provides a simple way to link your application with Facebook's functionalities. So this library contains a reference to the Facebook iOS SDK. However, to use facebook integration, you must have a Facebook developers account and a Facebook application which will be linked to your project. If you haven't, read the Facebook Developers Documentation.

Moreover the SNSFacebook library is only compatible for iOS 8 and above versions.

Installation

For installation, you can clone this project or use CocoaPods :

pod 'SNSSocial/Facebook', '~> 2.0'

Project configuration

Configure the .plist

Before to start using the SDK you must configures the .plist of your xcode project to linked it with your Facebook application.

  1. Add a key called 'FacebookAppID' and set as value the app ID of your Facebook application.
  2. Add a key called 'FacebookDisplayName' and set as value the Display Name of your Facebook application.
  3. Add an item in the array key 'URL types'. In this item add :
    • an array called 'URL Schemes' with a first item with value : "fb" + your Facebook App ID
    • the key 'URL Identifier' and set the bundleId of your application

iOS 9 Compatibility

An important change of your .plist is required to ensure iOS 9 compatibility. You needs to add a key called 'LSApplicationQueriesSchemes' of type Array. In this array, you should add different string items with following values :

  • 'fbapi'
  • 'fb-messenger-api'
  • 'fbauth2'
  • 'fbshareextension'

More informations are available on the Facebook Developers Documentation.

Prepare your App Delegate

The SDK is now ready, therefore you can link your application delegete into Facebook. For that, complete some AppDelegate methods like the following code :

#import <SNSFacebook.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return [SNSFacebook application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [SNSFacebook activateApplication];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSString *stringUrl = [url absoluteString];
    if ([stringUrl hasPrefix:@"fb"])
    {
        return [SNSFacebook application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
    }
    
    return NO;
}

Start Coding

Your application is now ready to use all SNSFacebook functionalities. It's include two main composants : SNSFacebookLogin and SNSFacebookInteractions.

SNSFacebookLogin

It contains all methods relative to the Facebook login and permissions management.

Sample login with read permissions

[SNSFacebookLogin loginWithReadPermissions:@[@"email"] fromViewController:self completion:^(id result, NSError * error)
{
	if (error != nil)
    {
    	// Display error
	}
}];

Sample logout

if ([SNSFacebookLogin isLogged])
{
	[SNSFacebookLogin logout];
}

Sample permissions checking

if ([SNSFacebookLogin hasPermission:@"publish_actions"])
{
	// Do something
}

Sample request to retrieve some user informations (This methods logs the user or add the missing permissions if needed)

[SNSFacebookLogin logToRetrieveUserInformations:@[SNSFacebookUserInfoName, SNSFacebookUserInfoEmail, SNSFacebookUserInfoPicture] fromViewController:self completion:^(id result, NSError * error)
{
    if (error == nil && result != nil)
    {
        NSString *name = [result objectForKey:SNSFacebookUserInfoName];
        NSString *email = [result objectForKey:SNSFacebookUserInfoEmail];
        NSString *pictureUrl = [result objectForKey:SNSFacebookUserInfoPicture];
    }
}];

SNSFacebookInteractions

It contains all methods relative to facebook publications such as sharing or likes. All methods can ask to log the user or to add missing permissions if needed.

Sample status sharing without dialog feed

[SNSFacebookInteractions postStatusWithMessage:@"Sample status update with SNSSocial"
                            completion:^(id result, NSError * error)
{
	if (error != nil)
    {
    	// Display error
    }
}];

Sample link sharing with dialog feed

[SNSFacebookInteractions postLink:@"http://smartnsoft.com/"
                        withTitle:@"Smart&Soft"
                      description:nil
                       pictureUrl:nil
             parentViewController:self
                       completion:^(id result, NSError * error)
{
	if (error != nil)
    {
    	// Display error
    }
}];

Sample like

[SNSFacebookInteractions likeWithObject:@"http://smartnsoft.com/" completion:^(id result, NSError * error)
{
	if (error == nil)
    {
    	if ([result boolValue])
        {
        	// Link has been liked
        }
        else
        {
        	// Link has been disliked
        }
	}
    else
    {
    	// Display error
    }
}];