using this method to post the image
- (void)postInAppFacebook {
btnForLogout.hidden=NO;
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:3];
NSData *imageData = UIImageJPEGRepresentation(imgPicture.image, 4.0);
UIImage *picture = [[UIImage alloc]initWithData:imageData];
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
[variables setObject:graph_file forKey:@"file"];
[variables setObject:[NSString stringWithFormat:@"%@", txtComment.text] forKey:@"message"];
**[fbGraph doGraphPost:@"me/photos" withPostVars:variables];**
NSLog(@"Now log into Facebook and look at your profile & photo albums...");
UIAlertView *alertForImageHasPost=[[UIAlertView alloc]initWithTitle:@"MAP IMAGE POST" message:@"SELECTED IMAGE POSTED" delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:@"OK",nil];
[alertForImageHasPost show];
[alertForImageHasPost release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
problem in this method second time its crash but working well in simulator
- (FbGraphResponse *)doGraphPost:(NSString *)action withPostVars:(NSDictionary *)post_vars {
NSLog(@"action is %@",action);
FbGraphResponse *return_value = [[FbGraphResponse alloc] init];
NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@", action];
NSURL *url = [NSURL URLWithString:urlString];
NSString *boundary = @"----1010101010";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSEnumerator *enumerator = [post_vars keyEnumerator];
NSString *key;
NSString *value;
NSString *content_disposition;
//loop through all our parameters
while ((key = (NSString *)[enumerator nextObject])) {
//if it's a picture (file)...we have to append the binary data
if ([key isEqualToString:@"file"]) {
/*
* the FbGraphFile object is smart enough to append it's data to
* the request automagically, regardless of the type of file being
* attached
*/
FbGraphFile *upload_file = (FbGraphFile *)[post_vars objectForKey:key];
[upload_file appendDataToBody:body];
//key/value nsstring/nsstring
} else {
value = (NSString *)[post_vars objectForKey:key];
content_disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key];
[body appendData:[content_disposition dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}//end else
[body appendData:[[NSString stringWithFormat:@"\r开发者_高级运维\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}//end while
//add our access token
[body appendData:[@"Content-Disposition: form-data; name=\"access_token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[accessToken dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//button up the request body
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];
//quite a few lines of code to simply do the business of the HTTP connection....
NSURLResponse *response;
NSData *data_reply;
NSError *err;
**//crash after this line**
***
> data_reply = [NSURLConnection sendSynchronousRequest:request
> returningResponse:&response error:&err];
***
return_value.htmlResponse = (NSString *)[[NSString alloc] initWithData:data_reply encoding:NSUTF8StringEncoding];
if (err != nil) {
return_value.error = err;
}
/*
* return the json array. we could parse it, but that would incur overhead
* some users might not want (not to mention dependencies), besides someone
* may want raw strings back, keep it simple.
*
* See: http://code.google.com/p/json-framework for an easy json parser
*/
return return_value;
}
Have a look at below functions. I hope you will get success with it.
Please have a look at all the methods as below:
- (IBAction)uploadPhoto:(id)sender {
[self postOnFacebook];
}
//Post Message and Photos on Facebook Function
-(void)postOnFacebook
{
NSString *strPostMessage = [self createMessageForPostOnFacebook:74.112 withLongitude:21.85];
//UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:objClsProductSearch.strProductImageURL]]];
//UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg"]]];
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/08/HUDTutorial.jpg"]]];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
strPostMessage,@"message",img, @"source",
nil];
[_facebook requestWithGraphPath:@"/me/photos"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
[img release];
}
//Create Message for posting on Facebook
-(NSString *)createMessageForPostOnFacebook:(double)pfltLatitude withLongitude:(double)pfltLongitude
{
//NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",pfltLatitude, pfltLongitude];
//NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",@"", @""];
//NSError* error;
//NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
//NSArray *listItems = [locationString componentsSeparatedByString:@","];
NSString *strMessage = nil;
/*if([[listItems objectAtIndex:0] isEqualToString:@"200"])
{
strMessage = [NSString stringWithFormat:@"I found a %@ using BH for iPhone \n Location : %@ \n Latitude : %f & Longitude : %f \n Product Rating : %d",objClsProductSearch.strCategoryname,[locationString substringFromIndex:6],appDelegate.dblLatitude,appDelegate.dblLongitude,btnTmp.tag];
}*/
strMessage = [NSString stringWithFormat:@"I found a %@ using BH for iPhone \n Location : %@ \n Latitude : %f & Longitude : %f \n Product Rating : %d", @"Cat Name", @"Location", 74.112, 21.85, 1];
return strMessage;
}
I hope you will have success using the above code.
Let me know in case of any difficulty.
In you app delegate, have a look at below methods whether it is implemented or not.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
//return [[controller facebook] handleOpenURL:url];
//return [[objPage2ViewController facebook] handleOpenURL:url];
return [[[self.navigationController topViewController] facebook] handleOpenURL:url];
}
Cheers
精彩评论