iOS发送带有结构的推文而不撰写推文

时间:2015-02-11 14:04:24

标签: ios objective-c xcode twitter twitter-fabric

我想发送推文,但我不想使用撰写推文。

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

[composer setText:@"just setting up my Fabric"];
[composer setImage:[UIImage imageNamed:@"fabric"]];

[composer showWithCompletion:^(TWTRComposerResult result) {
    if (result == TWTRComposerResultCancelled) {
        NSLog(@"Tweet composition cancelled");
    }
    else {
        NSLog(@"Sending Tweet!");
    }
}];

https://dev.twitter.com/twitter-kit/ios/compose此方法显示一些弹出屏幕,我想发送推文即时如何使用fabric api执行此操作?这种类型有可能吗?

此代码部分不适用于fabric

 ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
     {
         if(granted)
         {
             NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

             if ([accountsArray count] > 0)
             {
                 ACAccount *twitterAccount = [accountsArray objectAtIndex:0];

                 TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:[NSDictionary dictionaryWithObject:self.textViewOutlet.text forKey:@"status"] requestMethod:TWRequestMethodPOST];

                 [postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/png"];
                 [postRequest setAccount:twitterAccount];

                 [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                  {
                      //show status after done
                      NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                      NSLog(@"Twiter post status : %@", output);
                  }];
             }
         }
     }];

2 个答案:

答案 0 :(得分:3)

试试这个:

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

抱歉,我使用Swift编写了这段代码。

答案 1 :(得分:2)

https://github.com/nst/STTwitter

我使用这个API,比面料更容易:)

[_twitter postStatusUpdate:@"tweet text" inReplyToStatusID:nil latitude:nil longitude:nil placeID:nil displayCoordinates:nil trimUser:nil successBlock:nil errorBlock:nil];

非常容易发送没有作曲家的推文可能是这个帮助某人...