Search This Blog

Apr 26, 2011

How to post Image and text on Facebook using FBGraph ?

This example shows how to post on facebook wall using FBGraph. This example shows hows to shift up and down UIView on appearing of keyboard. This example shows how to take picture from iphone saved photo album or from iphone camera. This example shows UIActionSheet example for selecting pictures from iphone gallery or from iphone camera.


You can download source code from here Download.

Code for .h file.

#import
#import "FbGraph.h"

@interface RootViewController : UIViewController {
   
    FbGraph *fbGraph;
    IBOutlet UIImageView *imgPicture;
    IBOutlet UITextField *txtComment;
    NSData *dataImage;
}

@property (nonatomic, retain) FbGraph *fbGraph;

- (void)postInAppFacebook;
- (void)setViewMovedUp:(BOOL)movedUp;
- (IBAction)btnTakePicture_Clicked:(id)sender;
- (IBAction)btnPostOnFacebook_Clicked:(id)sender;

@end


 Code for .m file.


#import "RootViewController.h"
#import "FbGraphFile.h"

@implementation RootViewController

@synthesize fbGraph;

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //[self authenticateInAppViaFacebook];
}

- (IBAction)btnTakePicture_Clicked:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image from..." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Image Gallary", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    actionSheet.alpha=0.90;
    actionSheet.tag = 1;
    [actionSheet showInView:self.view];
    [actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (actionSheet.tag)
    {
        case 1:
            switch (buttonIndex)
        {
            case 0:
            {              
#if TARGET_IPHONE_SIMULATOR
              
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Saw Them" message:@"Camera not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                [alert release];
              
#elif TARGET_OS_IPHONE  
              
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                picker.delegate = self;
                //picker.allowsEditing = YES;
                [self presentModalViewController:picker animated:YES];
                [picker release];
              
#endif  
            }
                break;
            case 1:
            {
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                picker.delegate = self;
                [self presentModalViewController:picker animated:YES];
                [picker release];
            }
                break;
        }
            break;
          
        default:
            break;
    }  
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
    imgPicture.image = [[UIImage alloc] initWithData:dataImage];
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
   
    [self.navigationController dismissModalViewControllerAnimated:YES];  
}

- (IBAction)btnPostOnFacebook_Clicked:(id)sender
{
    if ([txtComment.text isEqualToString:@""] ) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"Please enter comment for Picture..." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
    else if (dataImage == Nil){
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"Please select Picture from gallery or iPhone camera..." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
    else {
        /*Facebook Application ID*/
        NSString *client_id = @"172038482843979";
      
        self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
      
        [fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(postInAppFacebook) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access" andSuperView:self.view];
      
    }
}

- (void)postInAppFacebook {
   
    NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:3];
   
    //create a UIImage (you could use the picture album or camera too)
    UIImage *picture = [[UIImage alloc]initWithData:dataImage];
   
    //create a FbGraphFile object insance and set the picture we wish to publish on it
    FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
   
    //finally, set the FbGraphFileobject onto our variables dictionary....
    [variables setObject:graph_file forKey:@"file"];
    //[variables setObject:@"http://iphoneapp-dev.blogspot.com/" forKey:@"description"];
    [variables setObject:[NSString stringWithFormat:@"http://iphoneapp-dev.blogspot.com %@", txtComment.text] forKey:@"message"];
   
   
    //the fbGraph object is smart enough to recognize the binary image data inside the FbGraphFile
    //object and treat that is such.....
    [fbGraph doGraphPost:@"me/photos" withPostVars:variables];
    NSLog(@"Now log into Facebook and look at your profile & photo albums...");
}

- (void)setViewMovedUp:(BOOL)movedUp{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    // Make changes to the view's frame inside the animation block. They will be animated instead
    // of taking place immediately.
    CGRect rect = self.view.frame;
    if (movedUp){       
        if(rect.origin.y == 0)
            rect.origin.y = self.view.frame.origin.y - 216;
    }
    else{      
        if(rect.origin.y < 0)
            rect.origin.y = self.view.frame.origin.y + 216;
    }  
    self.view.frame = rect;  
    [UIView commitAnimations];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    // The keyboard will be shown. If the user is editing the comments, adjust the display so that the
    // comments field will not be covered by the keyboard.
    [self setViewMovedUp:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{  
   
    [textField resignFirstResponder];
    [self setViewMovedUp:NO];
    return NO;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
    [fbGraph release];
}

@end
 

No comments:

Post a Comment