iOS 上传视频(分享)到Twitter(其实就是SDK升级啦)
iOS 上传视频(分享)到Twitter
Twitter SDK 升级到最新版的3.2.1
就我们目前的升级说一下改动吧
-
首先,pList文件里面加参数[图片上传失败...(image-9f8006-1509629415859)]
注意: twitterkit-{yourConsumerKey},这个大括号是不需要的... 最初我傻逼的加上了,就会造成登录crash -
Twitter的启动从之前的
[Fabric with:@[[Twitter class]]];
改为了
[Twitter sharedInstance] startWithConsumerKey:consumerSecret:];
当然,使用Fabric的用户还是之前一样的启动方式
- 再其次就是上传视频的一些接口的修改方式下面会提到
首先是步骤:
eg:
- https://developer.twitter.com/en/docs/media/upload-media/uploading-media/chunked-media-upload
- https://github.com/twitterdev/large-video-upload-python/blob/master/async-upload.py
-
上传图片一类的
- Initialize the upload using the INIT command
- Upload each chunk of bytes using the APPEND command
- Complete the upload using the FINALIZE command
-
上传大的文件比如视频
- INIT media upload.
- APPEND chunked data.
- FINALIZE media uploaded.
- Check STATUS of video processing.
图片一类的没有这个需求,所以没有研究,只研究了视频上传
具体实现
Twitter文档:https://developer.twitter.com/en/docs/media/upload-media/api-reference
iOS 系统小于11
首先上传视频需要账号,iOS系统小于11,设置里面有Twitter,FB之类的账号,所以可以通过系统的ACAccountStore获取。
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [accountStore accountsWithAccountType:accountType];
// Check if the users has setup at least one Twitter account
if (accounts.count > 0){
}
}];
其次,账号获取到了,接下来就是按照四个步骤拼接Request去上传。
主要代码放在了Git : https://gist.github.com/DarrenDuXuan/72077e98a611f29505d8beb1b2fdb3e0
iOS 系统大于11
iOS 系统大于11之后,苹果干掉了设置里面的账号,虽然 ACAccountStore , SLComposeViewController之类的还在,但是不能用了。所以Twitter官方也说是用他们的SDK,Twitter Kit。
步骤还是之前的步骤,但是11不支持的方法要按照官方文档里面的修改掉。
网址 : https://dev.twitter.com/twitterkit/ios/migrate-social-framework
所以现在获取Account的方法也要从之前的系统方法改为SDK里面的。
NSString *userID = [[Twitter sharedInstance].sessionStore session].userID;
TWTRAPIClient *client = [[TWTRAPIClient alloc] initWithUserID:userID];
以及11之前的request从之前的SLRequest改为了NSURLRequest
NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:twitterPostURLStr parameters:postParams error:&error];
发送请求也换成了TWTRAPIClient里面的方法sendTwitterRequest
主要代码放在了Git : https://gist.github.com/DarrenDuXuan/e7659cff41f3f6bee25edc471125b8d5
至此,上传视频到Twitter基本完成了,代码都放在git 基本看了官方文档对照代码看看就能明白,文章最开始也给了官方的文档链接,还有一个是官方用Python写的。
当然,Twitter官方也有分享用的专门的类,TWTRComposerViewController,他有两个initWithInitialText方法,一个需要videoURL,一个需要Data。第一个是assets-library,项目没法用,第二个传Data会Crash,希望谁试出来了,给我说下。
就这么多~
希望可以帮助大家。