Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

ios4 - How to post images in facebook in iphone app?

could anyone pls help me to post fotos on facebook using objective c for iphone app.i tried it but its getting terminated when i check with iphone.its working properly in simulator.following is my code i used to post in fb.i use graph api for developing my app.

-(void)fbGraphCallback:(id)sender {

    if ((fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0)) {

        NSLog(@"You pressed the 'cancel' or 'Dont Allow' button, you are NOT logged into Facebook...I require you to be logged in & approve access before you can do anything useful....");
        //restart the authentication process.....
        [fbGraph authenticateUserWithCallbackObject:self
                                        andSelector:@selector(fbGraphCallback:)
                             andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
        [self.view addSubview:viewshare];
    } 
    else {  
        NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2];

        //imgPicture is my image view name
        NSLog(@"the Data::%@
",imgPicture.image);

        FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:imgPicture.image];

        [variables setObject:graph_file forKey:@"file"];

        [variables setObject:[NSString stringWithFormat:@"%@", txtComment.text] forKey:@"message"];

        FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/photos" withPostVars:variables];
        NSLog(@"postPictureButtonPressed:  %@", fb_graph_response.htmlResponse);

        NSLog(@"Now log into Facebook and look at your profile & photo albums...");

        txtComment.text=@" ";
        [txtComment setHidden:YES];
        [lblcmt setHidden:YES];
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"Successfully posted...Now log into Facebook & look at your profile" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
    [fbGraph release];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First I check if facebook(the facebook object) has a valid session:

if (![facebook_ isSessionValid]) {

   permissions_ = [[NSArray arrayWithObjects:@"read_stream", @"publish_stream", @"offline_access",nil] retain];
   [facebook_ authorize:permissions_];

}

When I can guaranty that i'm logged into facebook I post the image like this:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   image, @"picture",
                                   message, @"message",
                                   nil];

[facebook_ requestWithGraphPath:@"me/photos"
                      andParams:params
                  andHttpMethod:@"POST"
                    andDelegate:self]; 

Finally I check this methods in for being sure if the image post was succesful or not:

-(void)request:(FBRequest *)request didFailWithError:(NSError *)error {

    //Error

}

-(void)request:(FBRequest *)request didLoad:(id)result {

    //Succes
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...