Facebook Integration In iPhone

Introduction

In this article I will create a Single View application. Here I use an image view and two buttons from outlet. In this app I will implement uploading an image to Facebook. For this we implement an image gallery path from which we pick an image and upload it to Facebook. Finally we test this app in a device. It was successfully uploaded to Facebook.

To understand it we use the following.

Step 1

Here first we add the framework Social that is required to implement the Facebook integration.

To import this framework we use the following.

Step 2

Click on the project and select Build Phase.

Build-phase-in-iphone.jpg

Step 3

Click on the "+" icon to add the framework.

frameworrk-in-iPhone.jpg

Step 4

Now select the Social framework and click on the "Add" button.

Social-framework-in-iPhone.png

Step 5

To pick an image from the image gallery we need one more framework; MobileCoreService.

mobile-core-servive-framework-in-iPhone.png

Step 6

Now we write the code for each class.

FBAppDelegate.h

#import <UIKit/UIKit.h>
@class FBViewController;
@interface FBAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) FBViewController *viewController;
@end

FBAppDelegate.m

#import "FBAppDelegate.h"
#import "FBViewController.h"

@implementation FBAppDelegate
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[FBViewController alloc] initWithNibName:@"FBViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
@end


FBViewController.h

#import <UIKit/UIKit.h>
#import <Social/Social.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface FBViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) SLComposeViewController *PostView;
-(IBAction)preview;
-(IBAction)selectImage;
@end

FBViewController.m

#import "FBViewController.h"
@interface FBViewController ()
@end
@implementation FBViewController
@synthesize PostView, imageView;
- (void) preview
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController*fvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fvc setInitialText:@"My Name is Rock Star"];
[fvc addImage:imageView.image];
[self presentViewController:fvc animated:YES completion:nil];
}
}
- (void) selectImage
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end

FBViewController.xib

Xib-file-in-iPhone.png

Step 7

Finally we click on the Run button to show the output.

Step 8

Output 1 in iPhone:

output1-in-iPhone.png

Click on "Select Image" Button.

Output 2 in iPhone:

Output2-in-iPhone.png

Now choose an image from the Image Gallery. To do that click on the saved photos.

Output 3 in iPhone:

Output3-in-iPhone.png

Now select an image.

Output 4 in iPhone:

Output4-in-iPhone.png

Now click on the Preview Post button.

Output 5 in iPhone:

Output5-in-iPhone.png

To upload an image to Facebook we need a Facebook account. To do that we click on the Setting button that is shown in the alert view.

Output 6 in iPhone:

Output6-in-iPhone.png

Click on the Settings icon.

Output 7 in iPhone:

Output7-in-iPhone.png

Now click on Facebook icon.

Output 8 in iPhone:

Output8-in-iPhone.png

If you have an account on Facebook simply provide the user name and password and click on the sign-in button.

Output 9 in iPhone:

Output9-in-iPhone.png

Click on the sign-in button.

Output 10 in iPhone:

Output10-in-iPhone.png

Output 11 in iPhone:

Now again run your app and select an image.

Output11-in-iPhone.png

To add a location click on it.

Output 12 in iPhone:

Output12-in-iPhone.png

To select a location click on "ok".

Output 13 in iPhone:

Output13-in-iPhone.png

This app was tested in the simulator and the simulator by default used "San Francisco" for the current location. 

Output 14 in iPhone:

Output14-in-iPhone.png

To set photo status we click on the default  friend icon and here we set it's status.

Output 15 in iPhone:

Output15-in-iPhone.png

Now we click on iOS Photos to the change album of where we want to save the image.

Output 16 in iPhone:

Output16-in-iPhone.png

Here we select the album of where we want to save the image in Facebook.

Output in iPhone

To check that the photo uploaded we enter the URL "www.facebook.com" in the browser and check the post.

Output-in-iPhone.png