使用UIWebView从服务器播放视频无法正常工作

时间:2017-02-07 10:58:16

标签: ios swift uiwebview uicollectionview

我正在尝试播放视频,并使用UIWebView从firebase显示它的标题。我有2个视图控制器,第一个视频VC具有视频标签的集合视图,当你按下标题时它应该带你到第二个视图控制器“showVideosVC”。标题可以成功查看,但我遇到的问题是我得到空白的UIWebView。我不知道我做错了什么或为什么视频不在这里播放是我的收藏查看课

class videosVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    var posts = [Post]()
    let db : DBHelperVideos = DBHelperVideos()
    var arrayVideos = [videoModel]()
    var selectedIndexPath : IndexPath = IndexPath()
    var post: Post!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.arrayVideos.removeAll()
        self.arrayVideos.append(contentsOf: self.db.fetchAll())
        self.collectionView.reloadData()
        DataService.ds.REF_POSTS9.observe(.value, with: { (snapshot) in

            if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {

                for snap in snapshot {
                    print ("SNAP: \(snap)”)
                    if let postDict = snap.value as? Dictionary<String, AnyObject>{
                        let key = snap.key
                        let post = Post(postKey: key , postData: postDict)
                        self.posts.append(post)
                          self.db.insertBook(id: postDict["id"] as! String,bookName: postDict["video_name"] as! String, bookPath: "", bookURL: postDict["video_path"] as! String, caption: "")        
                    }
                }
            } else {

            }
            self.arrayVideos.removeAll()
            self.arrayVideos.append(contentsOf: self.db.fetchAll())
            self.collectionView.reloadData()

        })

        collectionView.delegate = self
        collectionView.dataSource = self

    }





    func getFilePath(name : String) -> String {

        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let filePath = documentsPath+"/"+name
        return filePath
    }




    @IBAction func backbtn(_ sender: Any) {
        if let navController = self.navigationController {
            navController.popToRootViewController(animated: true)
        }
    }



    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return arrayVideos.count
    }




    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let book = arrayVideos[indexPath.item]
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as? collectionViewCellVideos {

           // cell.initWithBook(video: video)

            cell.configureCell(video: video)

            return cell

        }else {
            return collectionViewCellVideos()
        }
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectedIndexPath = indexPath
        self.performSegue(withIdentifier: "showVideo”, sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {

        if segue.identifier == "showImage"
        {
            let vc = segue.destination as! showVideosVC
            vc.video = self.arrayVideos[self.selectedIndexPath.row]

        }
    }  
} 

这是应该播放视频的第二节课

class showVideosVC: UIViewController , UIWebViewDelegate {

    var video : videoModel?

    @IBOutlet weak var webView: UIWebView!


    override func viewDidLoad() {
        super.viewDidLoad()

         let url = NSURL(string: (book?.bookPath)!)
        let url_request = NSURLRequest(url: url as! URL,
                                       cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad,
                                       timeoutInterval: 20.0)

        self.webView.loadRequest(url_request as URLRequest)


    }

}

1 个答案:

答案 0 :(得分:1)

我认为你必须做一些调试

1-断点

let url_request = NSURLRequest(url: url as! URL,cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad,timeoutInterval: 20.0)

并检查网址&amp;尝试在Safari中打开它。

2-将第二课改为并检查是否会加载错误

class showVideosVC: UIViewController , UIWebViewDelegate {

var video : videoModel?

@IBOutlet weak var webView: UIWebView!


override func viewDidLoad() {
    super.viewDidLoad()

    // Added
    webView.delegate = self

    let url = NSURL(string: (book?.bookPath)!)
    let url_request = NSURLRequest(url: url as! URL,
                                   cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad,
                                   timeoutInterval: 20.0)

    self.webView.loadRequest(url_request as URLRequest)


}
// Added
func webViewDidFinishLoad(_ webView: UIWebView) {
    print("Loaded")
}
// Added
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
    print("Error")
}


}