在iOS应用上从youtube上传视频?

时间:2018-04-20 05:45:48

标签: ios swift xcode youtube-data-api

我想从我自己的应用程序&上传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")

        }

    }

enter image description here

1 个答案:

答案 0 :(得分:0)

我在iOS10和iOS12上测试了您的功能,并且可以正常工作。 我怀疑问题来自错误的令牌(标题部分中的授权)。 请注意,每次您登录Google登录后令牌都会更改