1

I'm currently developing on an iOS app, which catches some tweets from the streaming api. For that reason I take the username and password of an user to authenticate. In addition to this I want to give the user the opportunity to follow some people on twitter. I created an UIButton and now want to call a url or something like this to follow a specific user. Is this possible?

bart s
  • 5,068
  • 1
  • 34
  • 55
Lukas
  • 1,346
  • 7
  • 24
  • 49

4 Answers4

2

Simply do a post

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

POST Data:  user_id=1401881&follow=true

Reference

oiledCode
  • 8,589
  • 6
  • 43
  • 59
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;
    }
}

}
Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • Thank's, but not exactly what i was looking for. I did it by just posting the data to the above api url. But thank's anyway! – Lukas Feb 10 '13 at 19:38
2

If you are using iOS 6 to follow user on 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);
        }];
    }

    }
}];
}
Mohd Asim
  • 2,171
  • 1
  • 18
  • 15
1

I followed @Mohd Asim 's answer to implement the following Swift code, thanks for the answer. :D

Version: iOS 10, Swift 3

Twitter 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)
            })
    })
}
}

You are welcome;)

RainCast
  • 4,134
  • 7
  • 33
  • 47
  • Fantastic! This swift version is just what I was looking for. Would you happen to have a similar thing for Facebook? I've been looking all over and there's a lot of info on FB integration, sharing and posting and such. But all I'm after is a 'follow us' functionality. I can get the account access but I don't know what to put in the SLRequest for url and params. Can you help? Thx – Mike Nov 07 '16 at 23:21
  • Hi Mikey, sorry I've only had the twitter follow code. Try google it a bit I am sure you can find something. Good luck! – RainCast Nov 08 '16 at 02:09
  • Is it possible to do this with iOS 11, not that social account have been removed? Or can we just use the browser now? – User Aug 17 '18 at 16:53