tom tom - 1 year ago 72
iOS Question

playing videos from server using UIWebView is not working

i am trying to play videos and show it's title from firebase using UIWebView. I have 2 view controllers the first one videoVC has collectionView of labels of the videos, when you press on the title it should take you to the second view controller "showVideosVC". the titles can be viewed successfully but the problem i am having is that i get blank UIWebView. i dont know what i am doing wrong or why the videos are not playing here is my collection View class

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]

}
}
}


and this is the second class that should play the videos

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)


}

}

Answer Source

I Think you will have to do some debugging

1- Breakpoint at

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

and check the url & try to open it in Safari.

2- Change second class to and check if it will load with error

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


}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download