2011-03-15 84 views
0

嗨,我已經完成了滾揉機狀態更新,但我正在以數據的形式發送照片時遇到問題,如下所示。tumblr iphone集成

-(IBAction)sendPhoto 
{ 
    NSString *email   = @"[email protected]"; 
    NSString *password  = @"password"; 
    NSString *sendType = @"photo"; 

    UIImage *imageMS = [UIImage imageNamed:@"Submit.png"]; 
    NSData *photoData = [[NSData alloc] initWithData:UIImagePNGRepresentation(imageMS)]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
           initWithURL:[NSURL URLWithString:@"http://www.tumblr.com/api/write"]]; 
    [request setHTTPMethod:@"POST"]; 
    NSString *request_body = [NSString 
      stringWithFormat:@"email=%@&password=%@&type=%@&data=%@", 
      [email   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
      [password  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
      [sendType  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
      photoData]; 
    [request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]]; 
    [web loadRequest:request]; 
} 

但它沒有得到更新。爲什麼?

+0

此外,你不應該發佈你的實際用戶名和密碼,我會建議馬上改變這些。 – theChrisKent 2011-03-15 14:57:13

+0

好吧我會改變,如果你知道請幫助我任何關於上傳照片到tumblr。 – 2011-03-15 15:12:15

回答

2

我原來的答案證明是不正確的,但是OP發佈了一個代碼鏈接,他說他解決了他的問題。我已經複製下面的代碼,以便將來的搜索者可以很容易地找到它,因爲我知道,看到你描述的問題是多麼令人沮喪,並找到解決方案的鏈接,只有這個鏈接已經死了。基於http://forums.macrumors.com/showthread.php?t=427513

代碼:

- (BOOL)sendPhotoToTumblr:(NSString *)photo usingEmail:(NSString *)tumblrEmail andPassword:(NSString *)tumblrPassword withCaption:(NSString *)caption; 
{ 
    //get image data from file 
    NSData *imageData = [NSData dataWithContentsOfFile:photo]; 
    //stop on error 
    if (!imageData) return NO; 

    //Create dictionary of post arguments 
    NSArray *keys = [NSArray arrayWithObjects:@"email",@"password",@"type",@"caption",nil]; 
    NSArray *objects = [NSArray arrayWithObjects: 
      tumblrEmail, 
      tumblrPassword, 
      @"photo", caption, nil]; 
    NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys]; 

    //create tumblr photo post 
    NSURLRequest *tumblrPost = [self createTumblrRequest:keysDict withData:imageData]; 
    [keysDict release];  

    //send request, return YES if successful 
    tumblrConnection = [[NSURLConnection alloc] initWithRequest:tumblrPost delegate:self]; 
    if (!tumblrConnection) { 
     NSLog(@"Failed to submit request"); 
     return NO; 
    } else { 
     NSLog(@"Request submitted"); 
     receivedData = [[NSMutableData data] retain]; 
      [tumblrConnection release]; 
     return YES; 
    } 
} 


-(NSURLRequest *)createTumblrRequest:(NSDictionary *)postKeys withData:(NSData *)data 
{ 
    //create the URL POST Request to tumblr 
    NSURL *tumblrURL = [NSURL URLWithString:@"http://www.tumblr.com/api/write"]; 
    NSMutableURLRequest *tumblrPost = [NSMutableURLRequest requestWithURL:tumblrURL]; 
    [tumblrPost setHTTPMethod:@"POST"]; 

    //Add the header info 
    NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 
    [tumblrPost addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    //create the body 
    NSMutableData *postBody = [NSMutableData data]; 
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    //add key values from the NSDictionary object 
    NSEnumerator *keys = [postKeys keyEnumerator]; 
    int i; 
    for (i = 0; i < [postKeys count]; i++) { 
     NSString *tempKey = [keys nextObject]; 
     [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 

    //add data field and file data 
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"data\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[NSData dataWithData:data]]; 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    //add the body to the post 
    [tumblrPost setHTTPBody:postBody]; 

    return tumblrPost; 
} 

我已經修改了上面的代碼,以消除一些內存問題,並添加一些參數,使之成爲一個更通用/靈活的解決方案。但是,如果有人希望在該網站上發佈原始代碼,只需查看此答案的修改版本即可。

+0

我已添加此代碼,但photoString的內容變爲空 – 2011-03-16 07:40:44

+0

現在,我可以成功發送照片了,我正在使用此鏈接中的代碼。 http://forums.macrumors.com/showthread.php?t=427513。 – 2011-03-16 10:13:58

+0

你可以給我鏈接下載ios sdk翻杯.. – kb920 2012-07-11 09:38:10