I前不久發佈了這個問題,但終於實現了這個功能。
TWTRComposer仍然不支持添加除圖像以外的任何內容,因此我使用了建議的媒體/上傳REST API。我可以發佈GIF和視頻。啁啾或者媒體之前,我創建一個自定義UIAlertView中,讓別人作曲鳴叫:
然後,當他們挖掘鳴叫按鈕,GIF被啾啾,如果它足夠小;否則,視頻將被推送。
得到的GIF鳴叫:https://twitter.com/spinturntable/status/730609962817294336
生成的視頻鳴叫:https://twitter.com/spinturntable/status/730609829128081408
這裏是我是如何實現這個功能(很多的幫助,從這個帖子https://stackoverflow.com/a/31259870/1720985)。
-(IBAction)tweet:(id)sender{
// check if the person has twitter
if([[Twitter sharedInstance] session]){
// first, show a pop up window for someone to customize their tweet message, limited to 140 characters
UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:@"Compose Tweet"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
tweetAlert.tag = TAG_TWEET;
tweetTextView = [UITextView new];
[tweetTextView setBackgroundColor:[UIColor clearColor]];
CGRect frame = tweetTextView.frame;
frame.size.height = 500;
tweetTextView.frame = frame;
[tweetTextView setFont:[UIFont systemFontOfSize:15]];
tweetTextView.textContainerInset = UIEdgeInsetsMake(0, 10, 10, 10);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitTextView:) name:@"UITextViewTextDidChangeNotification" object:tweetTextView];
[tweetTextView setText:[NSString stringWithFormat:@"%@ %@", [[NSUserDefaults standardUserDefaults] valueForKey:@"tweetText"], self.setShareURL]];
[tweetAlert setValue:tweetTextView forKey:@"accessoryView"];
[tweetAlert addButtonWithTitle:@"Tweet"];
[tweetAlert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"Please log in with your Twitter account to tweet!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
然後檢測UIAlertView中分享Tweet(我加UIAlertViewDelegate):
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(alertView.tag == TAG_TWEET){
if (buttonIndex == 1) {
UIAlertView *tweetStartAlert = [[UIAlertView alloc] initWithTitle:nil
message:@"Tweeting..."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[tweetStartAlert show];
// get client
__block TWTRAPIClient *client = [[Twitter sharedInstance] APIClient];
__block NSString *mediaID;
NSString *text = [tweetTextView text]; // get tweet text
NSLog(@"text: %@", text);
NSData *mediaData;
NSString *mediaLength;
NSString *mediaType;
NSString* url = @"https://upload.twitter.com/1.1/media/upload.json";
// if this is a single spin set, tweet the gif
if([self.setSpins count] ==1){
NSLog(@"tweeting GIF with url %@", self.gifURL);
mediaData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.gifURL]];
mediaLength = [NSString stringWithFormat:@"%lu", mediaData.length];
mediaType = @"image/gif";
}else if([self.setSpins count] > 1){
// multi-spin set - tweet the video
NSLog(@"tweeting video with url %@", self.videoURL);
mediaData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.videoURL]];
mediaLength = [NSString stringWithFormat:@"%lu", mediaData.length];
mediaType = @"video/mp4";
}
NSError *error;
// First call with command INIT
__block NSDictionary *message = @{ @"status":text,
@"command":@"INIT",
@"media_type":mediaType,
@"total_bytes":mediaLength};
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];
mediaID = [json objectForKey:@"media_id_string"];
NSError *error;
NSString *mediaString = [mediaData base64EncodedStringWithOptions:0];
// Second call with command APPEND
message = @{@"command" : @"APPEND",
@"media_id" : mediaID,
@"segment_index" : @"0",
@"media" : mediaString};
NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];
[client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse, NSData *responseData, NSError *error){
if(!error){
client = [[Twitter sharedInstance] APIClient];
NSError *error;
// Third call with command FINALIZE
message = @{@"command" : @"FINALIZE",
@"media_id" : mediaID};
NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];
[client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse, NSData *responseData, NSError *error){
if(!error){
client = [[Twitter sharedInstance] APIClient];
NSError *error;
// publish video with status
NSLog(@"publish video!");
NSString *url = @"https://api.twitter.com/1.1/statuses/update.json";
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:text,@"status",@"true",@"wrap_links",mediaID, @"media_ids", 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);
[tweetStartAlert dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *tweetFinishedAlert = [[UIAlertView alloc] initWithTitle:nil
message:@"Tweeted!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[tweetFinishedAlert show];
double delayInSeconds = 1.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[tweetFinishedAlert dismissWithClickedButtonIndex:0 animated:YES];
});
[self logShare:@"twitter"];
}else{
NSLog(@"Error: %@", error);
}
}];
}else{
NSLog(@"Error command FINALIZE: %@", error);
}
}];
}else{
NSLog(@"Error command APPEND: %@", error);
}
}];
}else{
NSLog(@"Error command INIT: %@", error);
}
}];
}
}
}
一些額外的東西:
用於組成的tweet消息創建初始UIAlertView中我重點UITextView在撰寫鳴叫提醒時出現:
- (void)didPresentAlertView:(UIAlertView *)alertView {
if(alertView.tag == TAG_TWEET){
NSLog(@"tweetAlertView appeared");
[tweetTextView becomeFirstResponder];
}
}
下面是如何檢查的UITextView是否有小於140個字符:
- (void)limitTextView:(NSNotification *)note {
int limit = 140;
if ([[tweetTextView text] length] > limit) {
[tweetTextView setText:[[tweetTextView text] substringToIndex:limit]];
}
}
希望爲我花了很長時間才拼湊起來的一切,這是對別人有用。
感謝您的幫助 - 你碰巧有一個如何做到這一點的例子? – scientiffic
@scientiffic以下是使用REST API發佈視頻的示例。 http://stackoverflow.com/questions/31259869/share-video-on-twitter-with-fabric-api-without-composer-ios/ – John81
@ john81感謝您的鏈接!我猜你需要爲人們創建一個自定義窗口來輸入他們的推文文本? – scientiffic