【问题标题】:share videoURL on instagram app iOS在 Instagram 应用 iOS 上分享 videoURL
【发布时间】:2017-05-17 04:59:25
【问题描述】:
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://library?AssetPath=file:%2F%2F%2Fhttp:%2Fmobmp4.org%2Ffiles%2Fdata%2F2480%2FTutak%252520Tutak%252520Tutiya%252520Title%252520Song%252520-%252520Remix%252520-%25252 %252520-%252520Mp4.mp4&InstagramCaption=Some%20Preloaded%20Caption

我成功地打开了 URL,但我找不到我想在 instagram 上分享的视频。

【问题讨论】:

  • 减少代码缩进,但使缩进统一,并将响应格式化为报价
  • 现在我成功在 Instagram 上分享视频了。
  • 感谢您让我们知道,但如果您添加一个解释您是如何做到的答案会很好?否则我们知道你成功与否对我们没有任何好处,对吧? :\
  • 你能告诉我们或在这里分享代码,以便我们也可以看到它是如何完成的吗?

标签: ios swift instagram sharing


【解决方案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 ")
    }

  }

【讨论】:

  • 您必须在 LSApplicationQueriesSchemes 数组下的 Info.plist 中添加 -> 第 0 项:instagram。
  • 我添加了,但没有帮助。
【解决方案2】:

首先,您需要视频中的 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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多