iOS 上傳視頻(分享)到Twitter
Twitter SDK 升級到最新版的3.2.1
就我們目前的升級說一下改動吧
首先,pList文件里面加參數[圖片上傳失敗...(image-9f8006-1509629415859)]
注意: twitterkit-{yourConsumerKey},這個大括號是不需要的... 最初我傻逼的加上了,就會造成登錄crashTwitter的啟動從之前的
[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,希望誰試出來了,給我說下。
就這么多~
希望可以幫助大家。