There’s dozens of questions related to yours on SO.
———-
In **Swift 4+** and **Xcode 9**
Method 1:
using `UIDocumentInteractionController`
1- Add the `UIDocumentInteractionControllerDelegate`,`WKNavigationDelegate`
let documentInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
documentInteractionController.delegate = self
documentInteractionController.presentPreview(animated: true)
And the delegate:
//MARK: UIDocumentInteractionController delegates
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
**OR**
Method 2: Using `UIWebView`
let webview = WKWebView(frame: UIScreen.main.bounds)
view.addSubview(webview)
webview.navigationDelegate = self
webview.load(URLRequest(url: URL(fileURLWithPath: path)))//URL(string: "http://") for web URL
C: Jack
***SWIFT 4+***
*If has to open file from local cache/Documentdiectory which has file path*
**Method 1: using UIDocumentInteractionController**
class ViewController: UIViewController,UIDocumentInteractionControllerDelegate,WKNavigationDelegate {
let documentInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
documentInteractionController.delegate = self
documentInteractionController.presentPreview(animated: true)
}
//MARK: UIDocumentInteractionController delegates
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
**Method 2: using WebView**
let webview = WKWebView(frame: UIScreen.main.bounds)
view.addSubview(webview)
webview.navigationDelegate = self
webview.load(URLRequest(url: URL(fileURLWithPath: path)))//URL(string: "http://") for web URL