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.
Step 3
Click on the "+" icon to add the framework.
Step 4
Now select the Social framework and click on the "Add" button.
Step 5
To pick an image from the image gallery we need one more framework; MobileCoreService.
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
Step 7
Finally we click on the Run button to show the output.
Step 8
Output 1 in iPhone:
Click on "Select Image" Button.
Output 2 in iPhone:
Now choose an image from the Image Gallery. To do that click on the saved photos.
Output 3 in iPhone:
Now select an image.
Output 4 in iPhone:
Now click on the Preview Post button.
Output 5 in iPhone:
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:
Click on the Settings icon.
Output 7 in iPhone:
Now click on Facebook icon.
Output 8 in iPhone:
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:
Click on the sign-in button.
Output 10 in iPhone:
Output 11 in iPhone:
Now again run your app and select an image.
To add a location click on it.
Output 12 in iPhone:
To select a location click on "ok".
Output 13 in iPhone:
This app was tested in the simulator and the simulator by default used "San Francisco" for the current location.
Output 14 in iPhone:
To set photo status we click on the default friend icon and here we set it's status.
Output 15 in iPhone:
Now we click on iOS Photos to the change album of where we want to save the image.
Output 16 in iPhone:
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.