2017-02-07 41 views
2

我正在尝试使用UIWebView播放视频并显示其来自Firebase的标题。我有2个视图控制器,第一个videoVC拥有视频标签的collectionView,当你按下标题时,它应该带你到第二个视图控制器“showVideosVC”。标题可以成功查看,但我遇到的问题是,我得到空白的UIWebView。我不知道我做错了,或者为什么视频没有在这里比赛是我收集的View类使用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

我想你会不得不做一些调试

1断点

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

并检查网址&尝试在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") 
} 


} 
相关问题