感謝@CReaTuS,我想澄清一點點,請注意,在iOS6的情況下,我們在iOS5中製作SLRequest,我們必須使用TWRequest執行請求。見如下─
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if ([accountStore respondsToSelector:@selector(requestAccessToAccountsWithType: options: completion:)])
{
[accountStore requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
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];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:@"Twitter_Name" forKey:@"screen_name"];
[tempDict setValue:@"true" forKey:@"follow"];
//Code specific to iOS6 or later
SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"] parameters:tempDict];
// To unfollow hit URL-https://api.twitter.com/1.1/friendships/destroy.json
[followRequest setAccount:twitterAccount];
[followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
NSLog(@"%@", output);
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request failed
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show success
});
}
}];
}
}
});
}];
}
else if ([accountStore respondsToSelector:@selector(requestAccessToAccountsWithType: withCompletionHandler:)])
{
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
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];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:@"Twitter_Name" forKey:@"screen_name"];
[tempDict setValue:@"true" forKey:@"follow"];
//Code specific to iOS5
TWRequest *followRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/friendships/create.json"]
parameters:tempDict
requestMethod:TWRequestMethodPOST];
[followRequest setAccount:twitterAccount];
[followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
NSLog(@"%@", output);
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request failed
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show success
});
}
}];
}
}
});
}];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request completely failed
});
}
編碼愉快:)
我有同樣的問題
,並如何ü設法擺脫它..!請幫忙..! – JgdGuy 2013-08-22 12:40:00
這是一個很好的WWDC 2012演示一下:http://adcdownload.apple.com/wwdc_2012/wwdc_2012_session_pdfs/session_306__integrating_with_facebook_twitter_and_sina_weibo.pdf – iwasrobbed 2013-09-18 02:36:08