func shareVideoToInstagram() {
let strURL = "http://mobmp4.org/files/data/2480/Tutak%20Tutak%20Tutiya%20Title%20Song%20-%20Remix%20-%20Drunx%20-%20Mp4.mp4"
let caption = "Some Preloaded Caption"
let captionStr = caption.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! as String
let videoURL = URL(fileURLWithPath: strURL, isDirectory: false)
let library = ALAssetsLibrary()
library.writeVideoAtPath(toSavedPhotosAlbum: videoURL) { (newURL, error) in
if let instagramURL = NSURL(string: "instagram://library?AssetPath=\(videoURL.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!)&InstagramCaption=\(captionStr)") {
print(instagramURL)
if UIApplication.shared.canOpenURL(instagramURL as URL) {
UIApplication.shared.openURL(instagramURL as URL)
}
} else {
print("NO")
}
}
}
我得到了这样的instagramURL:
的Instagram://库AssetPath =文件:%2F%2F%2Fhttp:%2Fmobmp4.org%2Ffiles%2Fdata%2F2480%2FTutak%252520Tutak%252520Tutiya%252520Title%252520Song%252520-%252520Remix%252520-%252520Drunx %252520-%252520Mp4.mp4&安培; InstagramCaption =有些%20Preloaded%20Caption
我成功openURL但是找不到我要在Instagram上分享的视频。
答案 0 :(得分:1)
func shareVideoToInstagram() {
let videoFilePath = "http://mobmp4.org/files/data/2480/Tutak%20Tutak%20Tutiya%20Title%20Song%20-%20Remix%20-%20Drunx%20-%20Mp4.mp4"
let instagramURL = NSURL(string: "instagram://app")
if (UIApplication.shared.canOpenURL(instagramURL! as URL)) {
let url = URL(string: ("instagram://library?AssetPath="+videoFilePath))
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler:nil)
}
} else {
print(" Instagram isn't installed ")
}
}
答案 1 :(得分:0)
首先,你需要一个来自视频的 NSData:
guard
let videoPath = "http://mobmp4.org/files/data/2480/Tutak%20Tutak%20Tutiya%20Title%20Song%20-%20Remix%20-%20Drunx%20-%20Mp4.mp4",
let videoUrl = URL(string: videoPath),
let videoData = NSData(contentsOf: videoUrl)
else {
return
}
postVideoToInstagramFeed(videoData: videoData)
并与函数分享:
func postVideoToInstagramFeed(videoData: NSData) {
getLibraryPermissionIfNecessary { granted in
guard granted else { return }
}
PHPhotoLibrary.shared().performChanges({
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
let filePath = "\(documentsPath)/\(Date().description).mp4"
videoData.write(toFile: filePath, atomically: true)
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: filePath))
},
completionHandler: { success, error in
if success {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
if let lastAsset = fetchResult.firstObject {
let localIdentifier = lastAsset.localIdentifier
let urlFeed = "instagram://library?LocalIdentifier=" + localIdentifier
guard
let url = URL(string: urlFeed)
else {
print("Could not open url")
return
}
DispatchQueue.main.async {
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
print("Sucess")
})
}
else {
UIApplication.shared.openURL(url)
print("Sucess")
}
}
else {
print("Instagram not found")
}
}
}
}
else if let error = error {
print(error.localizedDescription)
}
else {
print("Could not save the video")
}
})
}
func getLibraryPermissionIfNecessary(completionHandler: @escaping (Bool) -> Void) {
guard PHPhotoLibrary.authorizationStatus() != .authorized else {
completionHandler(true)
return
}
PHPhotoLibrary.requestAuthorization { status in
completionHandler(status == .authorized)
}
}