1

如何从 iphone SDK for tumblr 进行身份验证然后将文本发布到 tumblr 请帮助!

4

2 回答 2

2

* * @charanjit 我已经写了一个代码,你可以通过它在不倒翁上发布文本试试这个代码:-

@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");
    }
}
于 2012-07-09T07:15:52.637 回答
0

试试这个功能发帖到tumblr 希望这对你有帮助:)

- (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];
        }
    }
}
于 2012-07-09T06:17:11.683 回答