如何使用Graph api将图像与文本一起发布?

时间:2013-02-15 06:04:26

标签: iphone ios facebook facebook-graph-api

下面的代码成功地在Facebook墙上发布我的文字现在我想使用下面的代码发布图像和文本

- (IBAction)callFacebookAPI:(id)sender
 {
[self.txtinputfield resignFirstResponder];
if (txtinputfield.text.length !=0) 
{
   //create the instance of graph api
    objFBGraph = [[FbGraph alloc]initWithFbClientID:FbClientID];

    //mark some permissions for your access token so that it knows what permissions it has

    [objFBGraph authenticateUserWithCallbackObject:self andSelector:@selector(FBGraphResponse) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins,publish_checkins,email"];
}
else 
{
    UIAlertView *objAlert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Kindly enter data in the text field" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [objAlert show];
}
}

 - (void)FBGraphResponse
{

@try 
{
    if (objFBGraph.accessToken) 
    {
        SBJSON *jsonparser = [[SBJSON alloc]init];

        FbGraphResponse *fb_graph_response = [objFBGraph doGraphGet:@"me" withGetVars:nil];

        NSString *resultString = [NSString stringWithString:fb_graph_response.htmlResponse];
        NSDictionary *dict =  [jsonparser objectWithString:resultString];
        NSLog(@"Dict = %@",dict);

        NSMutableDictionary *variable = [[NSMutableDictionary alloc]initWithCapacity:1];

        [variable setObject:txtinputfield.text forKey:@"message"];
        [objFBGraph doGraphPost:@"me/feed" withPostVars:variable];

        UIAlertView *objAlert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"String posted on your wall and you may check the console now" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [objAlert show];


    }
}
@catch (NSException *exception) {
    UIAlertView *objALert = [[UIAlertView alloc]initWithTitle:@"Alert" message:[NSString stringWithFormat:@"Something bad happened due to %@",[exception reason]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [objALert show];
}

txtinputfield.text = clearText;
}

我尝试了一些方法,但没有为我工作,我没有使用Graph Api的经验任何帮助都会被推荐。谢谢

1 个答案:

答案 0 :(得分:2)

尝试如下:

- (IBAction)buttonClicked:(id)sender
{
    NSArray* permissions = [[NSArray alloc] initWithObjects:
                            @"publish_stream", nil];
    [facebook authorize:permissions delegate:self];
    [permissions release];

}
- (void)fbDidLogin
{
  NSString *filePath =pathToImage;
       NSData *videoData = [NSData dataWithContentsOfFile:filePath];
       NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   videoData, pathToImage,
                                   @"picture/jpeg", @"contentType",
                                   @"Video Test Title", @"title",
                                   @"Video Test Description", @"description",
                                   nil];
    [facebook requestWithGraphPath:@"me/photos"
                         andParams:params
                     andHttpMethod:@"POST"
                       andDelegate:self];
    }
-(void)fbDidNotLogin:(BOOL)cancelled
{
    NSLog(@"did not login");
}
- (void)request:(FBRequest *)request didLoad:(id)result
{
    if ([result isKindOfClass:[NSArray class]])
    {
        result = [result objectAtIndex:0];
    }
    NSLog(@"Result of API call: %@", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}