本文介绍了如何使用Xcode将文本共享到Tumblr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iOS应用程序,该应用程序具有Facebook,Twitter和Tumblr Sharing之类的共享功能.除了Tumblr,我已经完成了所有共享.我为此做了很多工作.我做了很多谷歌搜索,但是在Tumblr Sharing上什么也没找到.这是我目前用于通过Tumblr共享文本的代码:

I am working on an iOS application which has sharing functionality like Facebook, Twitter and Tumblr Sharing. I have done all sharing besides the Tumblr.I've worked a lot on this. I have done a lot of googling, but found nothing on Tumblr Sharing. Here is my code currently using for sharing text over Tumblr :

- (void)shareOvertumblr:(id)sender
{
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:
                          @"myEmailAddress", @"email",
                          @"password", @"password",
                          @"regular", @"type",
                          @"myTitle", @"title",
                          @"Hiiii How ruuu", @"body",
                          nil];
//here inPlace of these EmailAddress and Password using my correct emailAdress and Password
//set the body of the POST request to the multipart MIME encoded dictionary
[request setHTTPBody: [[NSString multipartMIMEStringWithDictionary: postData] dataUsingEncoding: NSUTF8StringEncoding]];
[NSURLConnection connectionWithRequest:request delegate:self];
}

  /*Here is The Category */
  @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:@"----_=_YourAppNameNoSpaces_%@_=_----",[[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

 /*Connection Delegate Methods*/
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
 [self.view setUserInteractionEnabled:YES];

 }
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
webData = [[NSMutableData alloc] initWithLength:0];
// webData is The NSMutable data
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

NSString* responseString = [[NSString alloc] initWithData: webData
                                                 encoding: NSUTF8StringEncoding];

NSLog(@"Response String %@",responseString);
// here i got Authentication failed as Response .....

}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];

 }

每当我运行此代码时,都会出现一些验证失败错误.我在各种线程上都看到了相同的问题.这是我尝试过的链接但不幸的是,到目前为止还没有运气.请帮忙.

Whenever i run this code getting some Authentication failed error.I have seen the same issue on various thread. Here is the link I have tried But unfortunately no luck so far. Please help.

推荐答案

您可以使用 ShareKit

Instead of your own code, you can use ShareKit

只需3行代码,它将为您的应用程序添加完整的共享功能.

It will add full sharing capabilities to your App with just 3 line of code.

ShareKit 是一个开放源代码框架,可以放入任何iPhone或iPad应用中,以立即添加完整的共享功能.

ShareKit is an open source framework that can be dropped into any iPhone or iPad app to instantly add full sharing capabilities.

您可以使用ShareKit在几行代码中通过 Tumblr 添加共享URL,图像和文本.

You can use ShareKit to add sharing URLs, images, and text with Tumblr in just a few lines of code.

共享文本

+(SHKItem *)text:(NSString *)text;

+ (SHKItem *)text:(NSString *)text;

NSString *someText = @"This is a blurb of text I highlighted from a document.";
SHKItem *item = [SHKItem text:someText];

您可以在此处

这篇关于如何使用Xcode将文本共享到Tumblr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:23