Wednesday, October 10, 2012

Post Image and Message on Facebook in iPhone


- (void)PostToWall{

     facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
    [facebook authorize:FB_PERMISSION_ARR];
}

- (void)postWall{

    NSData *imageData = UIImageJPEGRepresentation(YOURIMAGE, .1f);
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:facebook.accessToken, @"access_token",
                                   @"MESSAGE POST ON FB WALL", @"message", imageData, @"source", nil];
    [facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];
   
}


#pragma mark -
#pragma mark Facebook Function
/**
 * Called when the user has logged in successfully.
 */
- (void)fbDidLogin {
    [self postWall];
}
- (void)fbDidNotLogin:(BOOL)cancelled{
NSLog(@"fbDidNotLogin");
}

// FBRequestDelegate

/**
 * Called when the Facebook API request has returned a response. This callback
 * gives you access to the raw response. It's called before
 * (void)request:(FBRequest *)request didLoad:(id)result,
 * which is passed the parsed response object.
 */
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"received response");
}

/**
 * Called when a request returns and its response has been parsed into
 * an object. The resulting object may be a dictionary, an array, a string,
 * or a number, depending on the format of the API response. If you need access
 * to the raw response, use:
 *
 * (void)request:(FBRequest *)request
 *      didReceiveResponse:(NSURLResponse *)response
 */
- (void)request:(FBRequest *)request didLoad:(id)result {

NSLog(@"request:.%@", request);

if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
if ([[result allKeys] count] == 1) {
if ([[result allKeys] containsObject:@"id"]) {
            
UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Wall Post Success!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[al show];
[al release];
}
}else {
       
}
};

/**
 * Called when an error prevents the Facebook API request from completing
 * successfully.
 */
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(@"error...%@",[error localizedDescription]);
    
UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"Notification" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[al show];
[al release];
};

No comments:

Post a Comment