Iphone 使用帐户框架在iOS 5 Twitter中转发、回复和收藏
我已经在我的应用程序中集成了iOS5Twitter。我能够通过集成Twitter和Accounts框架的tweetcomposeviewcontroller发送推文Iphone 使用帐户框架在iOS 5 Twitter中转发、回复和收藏,iphone,objective-c,ios,ios5,twitter,Iphone,Objective C,Ios,Ios5,Twitter,我已经在我的应用程序中集成了iOS5Twitter。我能够通过集成Twitter和Accounts框架的tweetcomposeviewcontroller发送推文 现在,我想借助Accounts framework在iOS 5中转发、回复和收藏。使用TWRequest类而不是twtweetcomoseviewcontroller访问推文、转发、回复和收藏。有关更多信息,请使用这些链接。和使用以下代码进行转发 - (void)_retweetMessage:(TwitterMessage *)m
现在,我想借助Accounts framework在iOS 5中转发、回复和收藏。使用
TWRequest
类而不是twtweetcomoseviewcontroller
访问推文、转发、回复和收藏。有关更多信息,请使用这些链接。和使用以下代码进行转发
- (void)_retweetMessage:(TwitterMessage *)message
{
NSString *retweetString = [NSString stringWithFormat:@"http://api.twitter.com/1/statuses/retweet/%@.json", message.identifier];
NSURL *retweetURL = [NSURL URLWithString:retweetString];
TWRequest *request = [[TWRequest alloc] initWithURL:retweetURL parameters:nil requestMethod:TWRequestMethodPOST];
request.account = _usedAccount;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData)
{
NSError *parseError = nil;
id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&parseError];
if (!json)
{
NSLog(@"Parse Error: %@", parseError);
}
else
{
NSLog(@"%@", json);
}
}
else
{
NSLog(@"Request Error: %@", [error localizedDescription]);
}
}];
}
祝你好运。朋友詹尼斯的回答帮助我找到解决方案,答案是: 1)用于转发和收藏 这是一个用于发送请求的类方法,您必须为retweet和Favorite传递不同的URL 转发的URL:
收藏夹的URL:
2)回复代码
+ (void)makeRequestForReplyWithSelectedFeed:(NSString *)status inReply2:(NSString *)updateID{
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *trimmedText = status;
if ([trimmedText length] > 140.0) {
trimmedText = [trimmedText substringToIndex:140];
}
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:trimmedText forKey:@"status"];
if (updateID > 0) {
[params setObject:[NSString stringWithFormat:@"%@", updateID] forKey:@"in_reply_to_status_id"];
}
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
// Create a request, which in this example, posts a tweet to the user's timeline.
// This example uses version 1 of the Twitter API.
// This may need to be changed to whichever version is currently appropriate.
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:params requestMethod:TWRequestMethodPOST];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init];
[twitter5 performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
[twitter5 release]; }];
}
}
}];
}
方法调用:
[IOS5 Twitter makeRequestForReplyWithSelectedFeed:[NSString stringWithFormat:@“%@”,tweetMessage.text]在reply2:[NSString stringWithFormat:@“%@”,selectedFeed.id_str]]代码>
我已经创建了一个类iOS5Twitter并实现了一个类方法,如果你想在你的控制器中实现它,请用self
替换delegate
,谢谢,但是我必须为retweet、reply和favorite传递哪些url和参数?是的,谢谢它对retweet有效,现在我正在尝试favorite和reply,我将挑战性地接受你的答案,我已经投票了,但我正在尝试favorite和reply,这就是我等待的原因。:)事实上,你很容易用这个url修改现有功能,只需做一些小的适当更改。浏览文档,我坚持要你将你的方法添加到我的答案中,以便其他人可以根据你的意愿轻松获得帮助。。我得到code=34;message=“对不起,该页面不存在”代码>在传递url时。你能帮我做这个吗?@ShahPaneri这可能是图书馆的一个变化问题。我想现在twitter已经改变了它的库,所以看看这个,它可能会帮助你。我发现了这个问题。我没有对用户进行身份验证,因此它给了我错误。B.T.W.谢谢你的回答。:)
+ (void)makeRequestForReplyWithSelectedFeed:(NSString *)status inReply2:(NSString *)updateID{
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *trimmedText = status;
if ([trimmedText length] > 140.0) {
trimmedText = [trimmedText substringToIndex:140];
}
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:trimmedText forKey:@"status"];
if (updateID > 0) {
[params setObject:[NSString stringWithFormat:@"%@", updateID] forKey:@"in_reply_to_status_id"];
}
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
// Create a request, which in this example, posts a tweet to the user's timeline.
// This example uses version 1 of the Twitter API.
// This may need to be changed to whichever version is currently appropriate.
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:params requestMethod:TWRequestMethodPOST];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init];
[twitter5 performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
[twitter5 release]; }];
}
}
}];
}