Skip to content

Instantly share code, notes, and snippets.

@nahuelDeveloper
Last active May 2, 2018 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahuelDeveloper/e225b9a0bff1ac5f8076e941d24d5ab9 to your computer and use it in GitHub Desktop.
Save nahuelDeveloper/e225b9a0bff1ac5f8076e941d24d5ab9 to your computer and use it in GitHub Desktop.
Share a gif on Twitter. Code adapted for Swift 3 and iOS 10
// MARK: - Twitter
/*
To upload a gif to Twitter, two different API calls are needed.
One to upload the gif. The other, to append a comment to the gif.
https://xcodenoobies.blogspot.com.ar/2016/01/how-to-using-slrequest-to-upload-image.html?showComment=1460489097793#c638358337669317406
*/
static func twitterShare(from viewController: FinishVideoViewController, path: String) {
let account = ACAccountStore()
let accountType = account.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)
// Request access to Twitter
account.requestAccessToAccounts(
with: accountType,
options: nil) { (granted, error) in
if granted == true {
guard let arrayOfAccounts = account.accounts(with: accountType) else { print("No array of accounts"); return }
if arrayOfAccounts.count > 0 {
guard let twitterAccount = arrayOfAccounts.first as? ACAccount else { print("No twitter account"); return }
let gifURL = URL(fileURLWithPath: path)
guard let gifData: Data = NSData(contentsOf: gifURL) as Data? else { print("No Gif Data"); return }
guard let postGifRequest = SocialHelper.getPostGifRequest(
twitterAccount: twitterAccount,
gifData: gifData) else { print("No post gif request"); return }
// Post gif to Twitter
viewController.showProgressHUD(with: SocialHelper.gifUploadingMessage)
postGifRequest.perform(handler: { (responseData, urlResponse, error) in
if let err = error {
print("Twitter error. Gif Request \(err)")
viewController.hideProgressHUD()
return
}
guard let responseData = responseData else { print("No response data"); return }
do {
let json = try JSONSerialization.jsonObject(
with: responseData,
options: JSONSerialization.ReadingOptions(rawValue: UInt(0)))
guard let jsonDict = json as? [String: Any] else {
print("Error parsing json response")
viewController.hideProgressHUD()
return }
guard let mediaID = jsonDict[SocialHelper.kTwitterMediaIdKey] as? String else { print("Error parsing mediaID"); return }
guard let postCommentRequest = getPostCommentRequest(twitterAccount: twitterAccount, mediaID: mediaID) else { return }
postCommentRequest.perform(handler: { (responseData, urlResponse, error) in
viewController.hideProgressHUD()
guard let _ = error else {
print("Upload successful")
DispatchQueue.main.async {
viewController.showAlertWithMessage(message: SocialHelper.gifUploadSuccessMessage)
}
return
}
print("Upload failed")
DispatchQueue.main.async {
viewController.showAlertWithMessage(message: SocialHelper.gifUploadFailedMessage)
}
})
} catch {
print("JSONSerialization error")
viewController.hideProgressHUD()
}
})
} else {
print("Array of accounts is empty")
}
}
}
}
static func getPostGifRequest(twitterAccount: ACAccount, gifData: Data) -> SLRequest? {
guard let gifRequestURL = URL(string: SocialHelper.twitterGifURL) else { print("No Request URL"); return nil }
let postRequest = SLRequest(
forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: gifRequestURL,
parameters: nil)
postRequest?.account = twitterAccount
postRequest?.addMultipartData(
gifData,
withName: "media",
type: "image/gif",
filename: "showapp.gif")
return postRequest
}
static func getPostCommentRequest(twitterAccount: ACAccount, mediaID: String) -> SLRequest? {
let commentRequestURL = URL(string: SocialHelper.twitterCommentURL)
let message = [
SocialHelper.kTwitterStatusKey: SocialHelper.twitterShareText,
SocialHelper.kTwitterMediaIdsKey: mediaID
]
let postRequest2 = SLRequest(
forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: commentRequestURL,
parameters: message)
postRequest2?.account = twitterAccount
return postRequest2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment