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)
}
}