如何使用ASIHTTPRequest在Facebook上发布带有标签的照片

时间:2011-08-25 06:56:44

标签: iphone ios facebook facebook-graph-api asihttprequest

我使用Graph API和ASIFormDataRequest从我的iPhone应用程序在Facebook上发布照片。 一切正常(照片上传时带有正确的标题)。当我尝试添加标签时,照片仍然上传但没有标签。

以下是我准备数据的方法

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/photos",userID]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addFile:filePath forKey:@"file"];
[request setPostValue:caption forKey:@"message"];
[request setPostValue:fbManager._facebook.accessToken forKey:@"access_token"];
[request setAllowCompressedResponse:NO];
[request setTimeOutSeconds:60];

//Add tags
NSMutableArray *tagArray=[[NSMutableArray alloc] init];
for(FacebookTag *aTag in aJob.tags) {
    NSNumber *_cordX=[NSNumber numberWithFloat:((aTag.x/aJob.image.size.width)*100.0)];
    NSNumber *_cordY=[NSNumber numberWithFloat:((aTag.y/aJob.image.size.height)*100.0)];

    NSString *cordX=[_cordX stringValue];
    NSString *cordY=[_cordY stringValue];

    NSMutableDictionary* tag = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                aTag.userId, @"to",
                                cordX, @"x",
                                cordY, @"y",
                                nil];
    [tagArray addObject:tag];
}

NSData *tagData = [NSKeyedArchiver archivedDataWithRootObject:tagArray];
NSString* tagString = [[NSString alloc] initWithData:tagData encoding:NSUTF8StringEncoding];
[request setPostValue:tagString forKey:@"tags"];

1 个答案:

答案 0 :(得分:0)

试试此代码

//上传照片 ...

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

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

...

// Response received. Photo upload successful, so add tags
- (void)request:(FBRequest *)request didLoad:(id)result {
    if ([request.params objectForKey:@"picture"]) {
        // Get photo ID from result
        NSString* photoId = (NSString*)[result objectForKey:@"id"];  

        NSMutableDictionary* params = nil;
        for (int i = 0; i < [tagsList count]; i++) {
            // tagsList contains user IDs to tag
            NSString* uid = (NSString*)[tagsList objectAtIndex:i]; 
            params = [NSMutableDictionary dictionaryWithObjectsAndKeys:uid, @"to", 
                      nil];
            NSString* graphUrl = [NSString stringWithFormat:@"%@/tags", [photoId stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
            [self.facebook requestWithGraphPath:graphUrl 
                            andParams:params andHttpMethod:@"POST" andDelegate:self];
        }
    }
    ...
}

正在使用FBGraph API