使用Fabric API在Twitter上分享视频,而不需要作曲家iOS

前端之家收集整理的这篇文章主要介绍了使用Fabric API在Twitter上分享视频,而不需要作曲家iOS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
通过REST API for Twitter的视频上传在一月中可用,但不支持Fabric框架:
link

解决方法

根据 documentation需要通过命令进行3个呼叫:INIT,APPEND和FINALIZE.
  1. -(void) shareOnTwitterWithVideo:(NSDictionary*) params{
  2. NSString *text = params[@"text"];
  3. NSData* dataVideo = params[@"video"];
  4. NSString *lengthVideo = [NSString stringWithFormat:@"%d",[params[@"length"] intValue]];
  5. NSString* url = @"https://upload.twitter.com/1.1/media/upload.json";
  6.  
  7. __block NSString *mediaID;
  8.  
  9. if([[Twitter sharedInstance] session]){
  10.  
  11. TWTRAPIClient *client = [[Twitter sharedInstance] APIClient];
  12. NSError *error;
  13. // First call with command INIT
  14. NSDictionary *message = @{ @"status":text,@"command":@"INIT",@"media_type":@"video/mp4",@"total_bytes":lengthVideo};
  15. NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];
  16.  
  17. [client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse,NSData *responseData,NSError *error){
  18.  
  19. if(!error){
  20. NSError *jsonError;
  21. NSDictionary *json = [NSJSONSerialization
  22. JSONObjectWithData:responseData
  23. options:0
  24. error:&jsonError];
  25.  
  26. mediaID = [json objectForKey:@"media_id_string"];
  27. client = [[Twitter sharedInstance] APIClient];
  28. NSError *error;
  29. NSString *videoString = [dataVideo base64EncodedStringWithOptions:0];
  30. // Second call with command APPEND
  31. message = @{@"command" : @"APPEND",@"media_id" : mediaID,@"segment_index" : @"0",@"media" : videoString};
  32.  
  33. NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];
  34.  
  35. [client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse,NSError *error){
  36.  
  37. if(!error){
  38. client = [[Twitter sharedInstance] APIClient];
  39. NSError *error;
  40. // Third call with command FINALIZE
  41. message = @{@"command" : @"FINALIZE",@"media_id" : mediaID};
  42.  
  43. NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];
  44.  
  45. [client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse,NSError *error){
  46.  
  47. if(!error){
  48. client = [[Twitter sharedInstance] APIClient];
  49. NSError *error;
  50. // publish video with status
  51. NSString *url = @"https://api.twitter.com/1.1/statuses/update.json";
  52. NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:text,@"status",@"true",@"wrap_links",mediaID,@"media_ids",nil];
  53. NSURLRequest *preparedRequest = [client URLRequestWithMethod:@"POST" URL:url parameters:message error:&error];
  54.  
  55. [client sendTwitterRequest:preparedRequest completion:^(NSURLResponse *urlResponse,NSError *error){
  56. if(!error){
  57. NSError *jsonError;
  58. NSDictionary *json = [NSJSONSerialization
  59. JSONObjectWithData:responseData
  60. options:0
  61. error:&jsonError];
  62. NSLog(@"%@",json);
  63. }else{
  64. NSLog(@"Error: %@",error);
  65. }
  66. }];
  67. }else{
  68. NSLog(@"Error command FINALIZE: %@",error);
  69. }
  70. }];
  71.  
  72. }else{
  73. NSLog(@"Error command APPEND: %@",error);
  74. }
  75. }];
  76.  
  77. }else{
  78. NSLog(@"Error command INIT: %@",error);
  79. }
  80.  
  81. }];
  82. }
  83. }

猜你在找的iOS相关文章