gpt4 book ai didi

ios5 - iOS 的 twitter 框架支持自动分享吗?

转载 作者:行者123 更新时间:2023-12-04 05:35:15 24 4
gpt4 key购买 nike

我有一个名为 twitter 的按钮,当我通常单击此按钮时,会显示一个用于发送推文的 View ,即 TWTweetComposeViewController .我有两种发推文的方式,即自动分享和正常分享。正常分享工作得很好,但我怀疑如何在自动分享中做同样的事情,这意味着当我点击推特按钮而不是显示时会自动推特一些默认消息TWTweetComposeViewController查看并单击完成按钮。

最佳答案

最后我在 ios5 中搜索了很多与 Twitter 相关的问题,并在 twitter Developer Document 中阅读并尝试了这个答案。我认为这是进行自动共享的解决方案之一。

ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
// Did user allow us access?
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

// Sanity check
if ([arrayOfAccounts count] > 0)
{
// Keep it simple, use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

NSURL *url =
[NSURL URLWithString:
@"https://upload.twitter.com/1/statuses/update_with_media.json"];

// Create a POST request for the target endpoint
TWRequest *request =
[[TWRequest alloc] initWithURL:url
parameters:nil
requestMethod:TWRequestMethodPOST];

// self.accounts is an array of all available accounts;
// we use the first one for simplicity
[request setAccount:acct];

// The "larry.png" is an image that we have locally
UIImage *image = (UIImage *)[dictionarydata valueForKey:@"image"];
NSLog(@"%@",image);
if (image!=nil) {

// Obtain NSData from the UIImage
NSData *imageData = UIImagePNGRepresentation(image);


// Add the data of the image with the
// correct parameter name, "media[]"
[request addMultiPartData:imageData
withName:@"media[]"
type:@"multipart/form-data"];

// NB: Our status must be passed as part of the multipart form data
NSString *status = [NSString stringWithFormat:@"%@ %@ %@",[dictionaryData valueForKey:@"firstname"],[dictionaryData valueForKey:@"lastname"],[dictionarydata valueForKey:@"title"]];

// Add the data of the status as parameter "status"
[request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding]
withName:@"status"
type:@"multipart/form-data"];
// Perform the request.
// Note that -[performRequestWithHandler] may be called on any thread,
// so you should explicitly dispatch any UI operations to the main thread
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *dict =
(NSDictionary *)[NSJSONSerialization
JSONObjectWithData:responseData options:0 error:nil];

// Log the result
NSLog(@"%@", dict);

dispatch_async(dispatch_get_main_queue(), ^{
// perform an action that updates the UI...
});
}];
}else{
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"];
NSDictionary *p = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%@ %@ %@",[dictionaryData valueForKey:@"firstname"],[dictionaryData valueForKey:@"lastname"],[dictionarydata valueForKey:@"title"]], @"status",
nil
];
TWRequest *postRequest = [[TWRequest alloc]
initWithURL: url
parameters: p
requestMethod: TWRequestMethodPOST
];

// Post the request
[postRequest setAccount:acct];

// Block handler to manage the response
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
}];
}
}
}
}];

关于ios5 - iOS 的 twitter 框架支持自动分享吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12037571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com