本质上,我想要的是,一旦用户允许访问他们的Twitter帐户,应用程序就可以在UITableView
中发布用户选择的任何内容。理想情况下,我想在iOS5中使用Twitter框架,但我遇到的主要问题是推特的模式视图控制器。这是可选的吗?没有它可以发推特吗?如果没有,你建议我怎么做?
谢谢!
如果没有它,推特肯定是可能的,以下是iOS 5应用程序的生产版本。如果用户还没有注册帐户,它甚至会将其带到首选项的必要部分。
- (void)postToTwitter
{
// 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];
// 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];
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
TWRequest *postRequest = nil;
postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:[self stringToPost] forKey:@"status"] requestMethod:TWRequestMethodPOST];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
if ([urlResponse statusCode] == 200) {
Alert(0, nil, @"Tweet Successful", @"Ok", nil);
}else {
Alert(0, nil, @"Tweet failed", @"Ok", nil);
}
});
}];
});
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]];
}
}
}];
}
这将是一个使用SLRequest而不是TWRequest的更新版本,TWRequest在iOS 6中已被弃用。请注意,这需要将社会和账户框架添加到您的项目中。。。
- (void) postToTwitterInBackground {
// 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];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
SLRequest *postRequest = nil;
// Post Text
NSDictionary *message = @{@"status": @"Tweeting from my iOS app!"};
// URL
NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"];
// Request
postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:message];
// Set Account
postRequest.account = twitterAccount;
// Post
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Twitter HTTP response: %i", [urlResponse statusCode]);
}];
}
}
}];
}
更新:Fabric by Twitter中的TwitterKit非常方便,如果用户试图在你的应用程序中发推文时,你打算从你的推特应用程序中发布,那么这可能是一个不错的选择。
(是的,这种方法将允许你在没有任何对话框或确认的情况下发布到推特上)。
TwitterKit将处理权限部分,并使用TWTRAPIClient通过Twitter rest API执行推文。
//Needs to performed once in order to get permissions from the user to post via your twitter app.
[[Twitter sharedInstance]logInWithCompletion:^(TWTRSession *session, NSError *error) {
//Session details can be obtained here
//Get an instance of the TWTRAPIClient from the Twitter shared instance. (This is created using the credentials which was used to initialize twitter, the first time)
TWTRAPIClient *client = [[Twitter sharedInstance]APIClient];
//Build the request that you want to launch using the API and the text to be tweeted.
NSURLRequest *tweetRequest = [client URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:[NSDictionary dictionaryWithObjectsAndKeys:@"TEXT TO BE TWEETED", @"status", nil] error:&error];
//Perform this whenever you need to perform the tweet (REST API call)
[client sendTwitterRequest:tweetRequest completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//Check for the response and update UI according if necessary.
}];
}];
希望这能有所帮助。
由于多次更改,接受的答案不再有效。这一款适用于iOS 10、Swift 3和Twitter API 1.1版。
**更新**
这个答案已经更新,因为上一个答案依赖于一个不推荐使用的Twitter端点。
import Social
import Accounts
func postToTwitter() {
let accountStore = ACAccountStore()
let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)
accountStore.requestAccessToAccounts(with: accountType, options: nil) { (granted, error) in
if granted, let accounts = accountStore.accounts(with: accountType) {
// This will default to the first account if they have more than one
if let account = accounts.first as? ACAccount {
let requestURL = URL(string: "https://api.twitter.com/1.1/statuses/update.json")
let parameters = ["status" : "Tweet tweet"]
guard let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, url: requestURL, parameters: parameters) else { return }
request.account = account
request.perform(handler: { (data, response, error) in
// Check to see if tweet was successful
})
} else {
// User does not have an available Twitter account
}
}
}
}
这是正在使用的API。