2012-12-19 70 views
1

我很困惑這個問題很多天。在我的應用程序中,我需要在tumblr上傳圖片,我已經嘗試過各種教程和更新,但是沒有人在tumblr上發佈圖片。如果你已經完成了這個任務,請幫助我。在iPhone上tumblr上傳圖片

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 
NSURLConnection *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://api.tumblr.com/v2/blog/kashifjilani.tumblr.com/posts"]; 
NSMutableURLRequest *tumblrPost = [NSMutableURLRequest requestWithURL:tumblrURL]; 
[tumblrPost setHTTPMethod:@"POST"]; 

//Add the header info 
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 
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:[@"Content-Disposition: form-data; name=\"data\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"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

向我們展示您的一些代碼或您收到哪些錯誤? – Infinity

+0

我想發佈代碼,但不工作 – Kashif

+0

你的意思是你不能在stackoverflow上發佈代碼?確保你的代碼是簡潔的,並告訴我們你在哪裏得到錯誤。 – Infinity

回答

0

這個工作對我來說:

NSData *imageData = UIImageJPEGRepresentation(yourUploadImage, 0.9); 
NSMutableURLRequest *aRequest = [[[NSMutableURLRequest alloc] init] autorelease]; 
[aRequest setURL:[NSURL URLWithString:@"https://www.tumblr.com/api/write"]]; 
[aRequest setHTTPMethod:@"POST"]; 
NSString *boundary = @"0xKhTmLbOuNdArY"; 
//NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[aRequest addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

/* 
now lets create the body of the post 
*/ 
NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[@"Content-Disposition: form-data; name=\"email\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:Tumblr_UserName_Here dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[@"Content-Disposition: form-data; name=\"password\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:Tumblr_Password_Here dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] 
        dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"type\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"photo" dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"data\"; filename=\"upload.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Transfer-Encoding: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:imageData]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

if(comment available here) 
{ 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] 
         dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Disposition: form-data; name=\"caption\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[commentString dataUsingEncoding:NSUTF8StringEncoding]]; 
} 

// setting the body of the post to the reqeust 
[aRequest setHTTPBody:body]; 
[NSURLConnection connectionWithRequest:aRequest delegate:self]; 

現在的NSURLConnection

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    if(connection) 
    NSLog(@"Success"); 
    else 
    NSLog(@"Something Wrong"); 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; 
{ 
    NSLog(@"%@",[error description]); 
} 
+0

嘿王子感謝您的寶貴信息,我已經嘗試了上述步驟,但你仍然沒有工作,它顯示我在NSLog成功,但不工作或發佈任何東西tumblr – Kashif

+0

在tumblr中使用經過身份驗證的用戶,因爲Tumblr_UserName_和Tumblr_Password應該是正確的然後你將能夠發佈 –

+0

我正在使用正確的用戶名和密碼爲tumblr,一件事情,你是什麼意思,如果(評論在這裏可用),什麼類型的價值,我們可以給在該 – Kashif

0

我一直在爲此而努力了很久也delegate,但我想通了,如何發佈容易。 你可以看到我的post爲這個答案。如果您有任何問題,我很樂意提供幫助。