开发者

iPhone - posting image to Tumblr using API will not actually post

Hey guys, I am attempting to post and image-type post to Tumblr using someone's created code in a button press however when the button is pressed the NSLogs do not show up. Th开发者_JAVA百科e view that takes in the information is a modal view, if that is any different.

- (IBAction)createPost {
    NSString *caption = @"This is a test";
    [self sendPhotoToTumblr:UIImagePNGRepresentation(foodImage.image) usingEmail:email andPassword:password withCaption:caption];
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)sendPhotoToTumblr:(NSData *)photo usingEmail:(NSString *)tumblrEmail andPassword:(NSString *)tumblrPassword withCaption:(NSString *)caption;
{
    //get image data from file
    NSData *imageData = 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;
}

I feel as though my problems are from me not being able to understand the API plus the fact that I don't really understand NSURLRequests as much as I have read into it. Also, is there a way to have the UIWebView reload after this modalview is dismissed? Any advice would be great!

EDIT: After doing some NSLog testing, I find out that it bails out after it attempts to get the image data from file. I had just put in an image that I had in the project as a placeholder, however I eventually wish to have the user either pick a photo from their camera roll or take a new one..could this be why? Also..how would I pass in that type of picture using an NSString?

EDIT (AGAIN): I changed the sendPhotoToTumblr to accept NSData of a photo instead of a string because I could not figure out how to pass in the image as a string. I have had it go through the whole code and it says "Request Submitted" however nothing is posted. I know receivedData contains the HTTP error code that Tumblr responds with, however I do not know how to print it. Thoughts?


The reason this was not working is because I did not realize that the iPhone stored the images as .jpgs instead of .png.

[self sendPhotoToTumblr:UIImageJPEGRepresentation(foodImage.image, 0.5) usingEmail:email andPassword:password withCaption:caption];

And change the beginning of sendPhotoToTumblr to

- (BOOL)sendPhotoToTumblr:(NSData *)photo usingEmail:(NSString *)tumblrEmail andPassword:(NSString *)tumblrPassword withCaption:(NSString *)caption;
{
    //get image data from file
    //NSData *imageData = photo;  
    //stop on error
    if (!photo) 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:photo];
    [keysDict release];     


try answer in this question istead

Sending a POST request from Cocoa to Tumblr

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜