iOS follow button

ios
fabric
twitterkit

#1

Hi,

I need to implement follow button in my iOS app, is there a way to do it?

I found a way for websites but I need for iOS app.

Any suggestions?

Thanks.


#2

Hey @mariamAbdulrahm,

There isn’t a built in way into Twitter Kit, but you can construct the request using the REST API: https://dev.twitter.com/rest/reference/post/friendships/create


#3

Hey @bonnell,

Thanks for your help. This is my code:

        let client = TWTRAPIClient()
        var clientError : NSError?
        
        let statusesShowEndpoint = "https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true"

        let request = client.URLRequestWithMethod("POST", URL: statusesShowEndpoint, parameters: nil, error: &clientError)
        
        client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
            
            if connectionError != nil {
                print("Error: \(connectionError)")
            }
            
            guard let myData = data else {
                
                print("error nil")
                return
            }
            
            let json = JSON(data: myData)
            print(son)
        }

I got this error:

Error: Optional(Error Domain=TwitterAPIErrorDomain Code=220 "Request failed: forbidden (403)" UserInfo={NSLocalizedFailureReason=Twitter API error : Your credentials do not allow access to this resource. (code 220), TWTRNetworkingStatusCode=403, NSErrorFailingURLKey=https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true, NSLocalizedDescription=Request failed: forbidden (403)})

Any suggestions?


#4

Your TWTRAPIClient will need to be authenticated at some point - have you implemented Sign in with Twitter in your app as well?


#5

@andypiper Hey, Thanks for your reply. Yes I implemented it as follow:

let logInButton = TWTRLogInButton { (session, error) in
            if let unwrappedSession = session {
                
                self.userName.setValue(unwrappedSession.userName, forKey: "userName")
                self.userName.synchronize()
                let alert = UIAlertController(title: "Logged In",
                    message: "User \(unwrappedSession.userName) has logged in",
                    preferredStyle: UIAlertControllerStyle.Alert
                )
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
                self.presentViewController(alert, animated: true, completion: {
                    
                    self.performSegueWithIdentifier("toMainPage", sender: nil)
                })
            } else {
                NSLog("Login error: %@", error!.localizedDescription);
            }
        }