2013-02-10 134 views
1

我目前正在開發iOS應用程序,該應用程序從流API獲取一些推文。出於這個原因,我把用戶的用戶名和密碼進行驗證。除此之外,我想讓用戶有機會在Twitter上關注一些人。我創建了一個UIButton,現在想調用一個url或類似的東西來跟隨特定的用戶。這可能嗎?Twitter - 關注按鈕

+0

看到的https: //github.com/chrismaddern/Follow-Me-On-Twitter-iOS-Button – 2013-02-10 11:44:49

+0

或查看http ://stackoverflow.com/questions/10379201/how-to-add-twitter-follow-button-in-my-iphone-app – 2013-02-10 11:46:01

回答

2

如果您使用的是iOS 6跟蹤用戶在Twitter上:

-(void)followMe 
{ 
ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(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]; 

     NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init]; 
     [tempDict setValue:@"twitter_name" forKey:@"screen_name"]; 
     [tempDict setValue:@"true" forKey:@"follow"]; 
     NSLog(@"*******tempDict %@*******",tempDict); 

     //requestForServiceType 

     SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"] parameters:tempDict]; 
     [postRequest setAccount:twitterAccount]; 
     [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
      NSString *output = [NSString stringWithFormat:@"HTTP response status: %i Error %d", [urlResponse statusCode],error.code]; 
      NSLog(@"%@error %@", output,error.description); 
     }]; 
    } 

    } 
}]; 
} 
+0

非常好的答案,正是我正在尋找...謝謝! – RainCast 2016-10-25 04:51:44

2

簡單地做一個帖子

https://api.twitter.com/1.1/friendships/create.json 

POST Data: user_id=1401881&follow=true 

Reference

2
-(void)twitterButton 
{ 
NSString *twitterAccount= @"yourAccountName"; 
NSArray *urls = [NSArray arrayWithObjects: 
       @"twitter://user?screen_name={handle}", // Twitter 
       @"tweetbot:///user_profile/{handle}", // TweetBot 
       @"echofon:///user_timeline?{handle}", // Echofon 
       @"twit:///user?screen_name={handle}", // Twittelator Pro 
       @"x-seesmic://twitter_profile?twitter_screen_name={handle}", // Seesmic 
       @"x-birdfeed://user?screen_name={handle}", // Birdfeed 
       @"tweetings:///user?screen_name={handle}", // Tweetings 
       @"simplytweet:?link=http://twitter.com/{handle}", // SimplyTweet 
       @"icebird://user?screen_name={handle}", // IceBird 
       @"fluttr://user/{handle}", // Fluttr 
       @"http://twitter.com/{handle}", 
       nil]; 

UIApplication *application = [UIApplication sharedApplication]; 

for (NSString *candidate in urls) { 
    NSURL *url = [NSURL URLWithString:[candidate stringByReplacingOccurrencesOfString:@"{handle}" withString:twitterAccount]]; 
    if ([application canOpenURL:url]) 
    { 
    UIWebView* Twitterweb =[[UIWebView alloc] initWithFrame:CGRectMake(.....)]; 
     Twitterweb.delegate=nil; 
     Twitterweb.hidden=NO; 
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
     [Twitterweb loadRequest:requestObj]; 
     [self.view addSubview:Twitterweb]; 
     return; 
    } 
} 

} 
+0

謝謝,但不是我正在尋找。我只是將數據發佈到上面的api url中。但是,無論如何,謝謝! – 2013-02-10 19:38:53

+0

歡迎,很高興幫助 – Mutawe 2013-02-11 06:08:45

0

我跟着@Mohd阿西姆的回答執行下面的Swift代碼,謝謝你的回答。 :d

版本:iOS的10,夫特3

微博API:1.1

https://dev.twitter.com/rest/reference/post/friendships/create

class SocialHelper { 

static func FollowAppTwitter() { 

    let accountStore = ACAccountStore() 
    let twitterType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter) 

    accountStore.requestAccessToAccounts(with: twitterType, options: nil, 
     completion: { (isGranted, error) in 
      guard let userAccounts = accountStore.accounts(with: twitterType), 
       userAccounts.count > 0 else { return } 
      guard let firstActiveTwitterAccount = userAccounts[0] as? ACAccount else { return } 

      // post params 
      var params = [AnyHashable: Any]() //NSMutableDictionary() 
      params["screen_name"] = "pixelandme" 
      params["follow"] = "true" 

      // post request 
      guard let request = SLRequest(forServiceType: SLServiceTypeTwitter, 
            requestMethod: SLRequestMethod.POST, 
            url: URL(string: "https://api.twitter.com/1.1/friendships/create.json"), 
            parameters: params) else { return } 
      request.account = firstActiveTwitterAccount 

      // execute request 
      request.perform(handler: { (data, response, error) in 
       print(response?.statusCode) 
       print(error?.localizedDescription) 
      }) 
    }) 
} 
} 

歡迎您)

+0

太棒了!這個快速版本就是我正在尋找的。你會碰巧對Facebook有類似的事情嗎?我一直在尋找,並且有很多關於FB整合,分享和發佈等的信息。但我所追求的只是「跟隨我們」的功能。我可以獲得帳戶訪問權限,但我不知道要爲網址和參數輸入SLRequest。你能幫我嗎? Thx – Mikey 2016-11-07 23:21:00

+0

嗨Mikey,對不起,我只有twitter後續代碼。嘗試谷歌一點,我相信你可以找到一些東西。祝你好運! – RainCast 2016-11-08 02:09:53