Dzung Le
Dzung Le

Reputation: 51

How can I post tweet to Twitter with Fabric framework without native composer? iOS

I integrate Fabric Framework to login and post tweet on iOS flat-form. But I cannot post tweet by my customize textview. It always display Twitter Composer. How can I post with my purpose? Thanks!

This is my code:

    TWTRComposer *composer = [[TWTRComposer alloc] init];

    [composer setText:textInput.text];
 // [composer setImage:[UIImage imageNamed:@"fabric"]];

    [composer showWithCompletion:^(TWTRComposerResult result) {
        if (result == TWTRComposerResultDone) {
            [Util showMessage:@"Post Tweet successfully!" withTitle:@""];
        }
        else {
            [Util showMessage:@"Unable to post Tweet" withTitle:@""];
        }
    }];

Upvotes: 4

Views: 1283

Answers (2)

Alvin John
Alvin John

Reputation: 413

Try this:

func tweet(userId: String) {
        let client = TWTRAPIClient(userID: userId)

        let error: NSErrorPointer = NSErrorPointer()
        let url: String = "https://api.twitter.com/1.1/statuses/update.json"
        let message: [NSObject : AnyObject] = [
            "status" : "Sample Tweet Tweet!"
        ]

        let preparedRequest: NSURLRequest = client.URLRequestWithMethod("POST", URL: url, parameters: message, error: error)

        client.sendTwitterRequest(preparedRequest) { (response, data, jsonError) -> Void in
            do {
                let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject]
                NSLog("%@", json)
                print("Tweet post!")
            } catch {
                print("json error: \(error)")
            }
        }
    }

You must first get the userId of the user. If you're using the manual button registration use this:

func tweet(userId: String) {
        let client = TWTRAPIClient(userID: userId)

        let error: NSErrorPointer = NSErrorPointer()
        let url: String = "https://api.twitter.com/1.1/statuses/update.json"
        let message: [NSObject : AnyObject] = [
            "status" : "Sample Tweet Tweet!"
        ]

        let preparedRequest: NSURLRequest = client.URLRequestWithMethod("POST", URL: url, parameters: message, error: error)

        client.sendTwitterRequest(preparedRequest) { (response, data, jsonError) -> Void in
            do {
                let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject]
                NSLog("%@", json)
                print("Tweet post!")
            } catch {
                print("json error: \(error)")
            }
        }
    }

Upvotes: 1

fechidal89
fechidal89

Reputation: 723

Try this:

TWTRAPIClient *client = [[Twitter sharedInstance] APIClient];
NSError *error;

NSString *url = @"https://api.twitter.com/1.1/statuses/update.json";
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:text,@"status", nil];

NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];

[client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse, NSData *responseData, NSError *error){

if(!error){
    NSError *jsonError;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonError];
    NSLog(@"%@", json);
}else{
     NSLog(@"Error: %@", error);
}

}];

Upvotes: 2

Related Questions