如何在没有本地作曲家的情况下使用Fabric框架向Twitter发布推文? iOS版

时间:2015-02-03 04:15:28

标签: ios twitter twitter-fabric

我将Fabric Framework集成到登录并在iOS平面上发布推文。但我无法通过自定义textview发布推文。它总是显示Twitter Composer。我怎样才能发布我的目的?谢谢!

这是我的代码:

    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:@""];
        }
    }];

2 个答案:

答案 0 :(得分:2)

试试这个:

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

}];

答案 1 :(得分:1)

试试这个:

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

您必须先获取该用户的userId。如果您正在使用手动按钮注册,请使用:

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