tumblr iPhone 集成

发布于 2024-10-21 19:17:05 字数 1275 浏览 3 评论 0原文

您好,我已经在不倒翁中完成了状态更新,但是当我以数据形式发送照片时遇到问题,如下所示。

-(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];
}

但它没有得到更新。为什么?

Hi i have done with status update in tumbler but i am facing problem when i am sending photo in the form of data, as shown below.

-(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];
}

But it is not getting updated. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-10-28 19:17:05

我原来的答案被证明是不正确的,但OP发布了一个代码链接,他说解决了他的问题。我复制了下面的代码,以便将来的搜索者可以轻松找到它,因为我知道看到描述的问题并找到解决方案的链接却发现该链接已失效是多么令人沮丧。

代码基于 http://forums.macrumors.com/showthread.php?t=427513< /a>:

- (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;
}

我修改了上面的代码以消除一些内存问题并添加一些参数以使其成为更通用/灵活的解决方案。但是,如果有人想要该网站上发布的原始代码,只需查看此答案的编辑修订版本即可。

My original answer proved to be incorrect, but the OP posted a link to code that he said fixed his problem. I have copied that code below so that future searchers can find it easily, since I know how frustrating it can be to see your problem described and find a link to the solution only to have that link be dead.

Code based on 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;
}

I have modified the above code to eliminate some memory issues and to add some parameters to make this a more generic/flexible solution. However, if someone would like the original code as posted on that site, just look through this answer's edit revisions.

花辞树 2024-10-28 19:17:05

为了将来参考,ASIHTTPRequest 的 ASIFormDatRequest 使这种事情变得更加容易。

For future reference, ASIHTTPRequest's ASIFormDatRequest makes this sort of thing much easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文