iphone - 使用 iphone SDK 和 "/api/write "发布到 tumblr

标签 iphone ios cocoa-touch

<分区>

如何从适用于 tumblr 的 iphone SDK 进行身份验证,然后将文本发布到 tumblr 请帮忙!

最佳答案

**@charanjit 我已经为此编写了一个代码,您可以通过它在 tumbler 上发布文本试试这个代码:-

@interface NSString (MIMEAdditions)
+ (NSString*)MIMEBoundary;
+ (NSString*)multipartMIMEStringWithDictionary:(NSDictionary*)dict;
@end

@implementation NSString (MIMEAdditions)

//this returns a unique boundary which is used in constructing the multipart MIME body of the POST request
+ (NSString*)MIMEBoundary
{
    static NSString* MIMEBoundary = nil;

    if(!MIMEBoundary)
        MIMEBoundary = [[NSString alloc] initWithFormat:@"----_=TheRealTester%@_=_----",[[NSProcessInfo processInfo] globallyUniqueString]];

    return MIMEBoundary;
}

//this create a correctly structured multipart MIME body for the POST request from a dictionary
+ (NSString*)multipartMIMEStringWithDictionary:(NSDictionary*)dict 
{
    NSMutableString* result = [NSMutableString string];

    for (NSString* key in dict)
    {
        [result appendFormat:@"--%@\nContent-Disposition: form-data; name=\"%@\"\n\n%@\n",[NSString MIMEBoundary],key,[dict objectForKey:key]];
    }

    [result appendFormat:@"\n--%@--\n",[NSString MIMEBoundary]];
    return result;
}

@end

**以上代码在执行前写入.m文件。 之后写这段代码..

- (IBAction)postToTumblr1
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL b = [emailTest evaluateWithObject:emailtext.text];

    if (b==NO) {        
        UIAlertView *messageBox = [[UIAlertView alloc]initWithTitle:@" " message:@"Invalid Email ID" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [messageBox show];
        [messageBox release];  
    }
    else {
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://www.tumblr.com/api/write"]];
        [request setHTTPMethod:@"POST"];

        //tell the server to expect 8-bit encoded content as we're sending UTF-8 data, and UTF-8 is an 8-bit encoding
        [request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];

        //set the content-type header to multipart MIME
        [request addValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@",[NSString MIMEBoundary]] forHTTPHeaderField: @"Content-Type"];

        //create a dictionary for all the fields you want to send in the POST request
        NSDictionary* postData = [NSDictionary dictionaryWithObjectsAndKeys:
                                 emailtext.text, @"email",
                                 password.text, @"password",
                                 @"regular", @"type",
                                 @"The Real Compatibility Tester", @"title",
                                 postText, @"body",
                                 nil];

        //set the body of the POST request to the multipart MIME encoded dictionary
        [request setHTTPBody: [[NSString multipartMIMEStringWithDictionary: postData] dataUsingEncoding: NSUTF8StringEncoding]];

        NSLog(@"Tumblr Login:%@\nTumblr ", emailtext.text);

        [NSURLConnection connectionWithRequest:request delegate:self];
        [request release];

        yes=1;

        if(1) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Tumblr Post" message:@"Post successfully" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

            [alert show];
            [alert release];
        }
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Tumblr Post" message:@"Post not  successfully done" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

            [alert show];
            [alert release]; 
        }
    }
 }

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 0)
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    else
    {
        NSLog(@"cancel");
    }
}

关于iphone - 使用 iphone SDK 和 "/api/write "发布到 tumblr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389748/

相关文章:

ios - 下载数据异步ios

ios - 为什么网页 View 会崩溃

ios - 当 slider 动画时更新标签上的 slider 值

ios - 将 navigationController 嵌入到 tabBarController 中后,tabBarItem 区域在界面生成器中消失

iphone - 在iphone sdk中的数据库数组中添加值

iphone - 从 UINavigationController 弹出一个包含 UITabBarController 的 UIViewController

ios - 您可以拥有多少个 UIApplicationShortcutItem 有限制吗?

iphone - 如何判断用户是否已经滚动到 UITableView 的底部?

iphone - 如何在 UINavigationController 中添加中间按钮?

ios - 我可以在 UITextField 中有多个 leftOverlay 项目或同时有 2 个独立的文本吗?