【问题标题】:Twitter - Follow ButtonTwitter - 关注按钮
【发布时间】:2013-01-25 16:20:47
【问题描述】:

我目前正在开发一个 iOS 应用程序,该应用程序从流 api 捕获一些推文。出于这个原因,我使用用户的用户名和密码进行身份验证。除此之外,我想让用户有机会在 Twitter 上关注一些人。我创建了一个 UIButton,现在想调用一个 url 或类似的东西来关注特定的用户。这可能吗?

【问题讨论】:

标签: ios twitter twitter-follow


【解决方案1】:

如果您使用 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);
        }];
    }

    }
}];
}

【讨论】:

  • 非常好的答案,正是我想要的……谢谢!
  • iOS 11 不再支持社交账户了?
【解决方案2】:

随便发个帖子

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

POST Data:  user_id=1401881&follow=true

Reference

【讨论】:

    【解决方案3】:
    -(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;
        }
    }
    
    }
    

    【讨论】:

    • 谢谢,但不是我想要的。我只是通过将数据发布到上面的 api url 来做到这一点。不过还是谢谢啦!
    【解决方案4】:

    我按照@Mohd Asim 的回答实现了以下 Swift 代码,感谢您的回答。 :D

    版本:iOS 10、Swift 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["user_id"] = "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)
                })
        })
    }
    }
    

    不客气;)

    【讨论】:

    • 太棒了!这个快速版本正是我想要的。你会碰巧对 Facebook 有类似的事情吗?我一直在寻找,有很多关于 FB 集成、共享和发布等的信息。但我所追求的只是“跟随我们”的功能。我可以获得帐户访问权限,但我不知道在 SLRequest 中为 url 和参数输入什么。你能帮我吗?谢谢
    • 嗨 Mikey,对不起,我只有 twitter 关注代码。试着用谷歌搜索一下,我相信你能找到一些东西。祝你好运!
    • 是否可以在 iOS 11 上执行此操作,而不是社交帐户已被删除?或者我们现在可以使用浏览器吗?
    猜你喜欢
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2012-06-08
    • 2011-09-07
    • 2016-02-14
    • 2020-02-01
    • 2012-09-02
    相关资源
    最近更新 更多