【问题标题】:Uploading video on youtube from iOS app?从 iOS 应用程序上传 youtube 上的视频?
【发布时间】:2018-09-30 18:24:07
【问题描述】:

我想从我自己的应用程序在 Youtube 上上传视频,并在杀死应用程序甚至在后台模式下恢复上传。我试过这个链接,但这是一个 MacApp 而不是 iOS 应用程序。此外,当我运行它时,它在 pod 文件中显示 NSURLSession 在 10.9 或更新版本中可用。所以它似乎对我不起作用。

https://github.com/google/google-api-objectivec-client-for-rest/tree/master/Examples

任何人都可以指导我如何在一个更好并且可能是一个简单的示例中实现这一点。

编辑: 我已经尝试了下面的代码,但我遇到了错误。请帮助我解决问题。

func postVideoToYT(videoData: Data, token: String,title:String,innoId:Int,videoTags:String,callback: @escaping (Bool) -> Void) {

        do {

            let headers = ["Authorization": "Bearer \(token)"]

            upload(multipartFormData: { multipartFormData in

                multipartFormData.append("{'snippet':{'title' : '\(title)','description': 'abc'}}".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "snippet", mimeType: "application/json")

                multipartFormData.append("{'status' : {'privacyStatus':'private'}}".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "status",mimeType: "application/json")

                multipartFormData.append(videoData, withName: "video", fileName: "video.mov", mimeType: "application/octet-stream")

            }, usingThreshold: 1, to: URL(string: "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet&status")!, method: .post, headers: headers, encodingCompletion: { encodingResult in

                switch encodingResult {

                case .success(let upload, _, _):



                    upload.responseJSON { response in

                        print("Post video to url --->\(response)")

                        if let json = response.result.value as? [String : Any] {

                            let videoId = json["id"] as? String ?? ""

                           /* self.delayWithSeconds(1, completion: {

                                self.addVideo(innoId: innoId, videoKey:videoId, shortDesc: title, tagIds: videoTags)

                            })*/

                        }



                        callback(true)

                    }



                    upload.uploadProgress(closure: { (progress) in

                        print(progress)

                       // self.progressView.progress = Float(progress.fractionCompleted)

                        //self.progressLabel.text = "\(( Int(progress.fractionCompleted * 100)))%"

                    })



                    break

                case .failure(_):

                    callback(false)

                    break

                }

            })

        }



        catch {

            print("Catch error")

        }

    }

【问题讨论】:

  • 上传视频:stackoverflow.com/questions/35053431/…你可以参考这个链接
  • 我们是否只需要使用rest api。我们不能使用上面链接中提到的类吗?
  • “上传”方法包含什么?

标签: ios swift xcode youtube-data-api


【解决方案1】:

我在 iOS10 和 iOS12 上测试了你的功能,它正在运行。 我怀疑问题来自错误的令牌(标题部分中的授权)。 请注意,每次您进行 Google 登录时,令牌都会发生变化

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2017-04-13
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 2015-09-15
    • 2015-01-08
    • 2011-04-01
    相关资源
    最近更新 更多