You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.4 KiB

6 years ago
  1. //
  2. // WebLinksViewController.swift
  3. //
  4. //
  5. // Created by shishir sapkota
  6. //
  7. import Foundation
  8. import UIKit
  9. import Alamofire
  10. class WebLinksViewController: UIViewController {
  11. // MARK: Properties
  12. // MARK: Outlets
  13. // @IBOutlet weak var webView: UIWebView!
  14. // @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
  15. var navTitle: String = ""
  16. var url: String?
  17. var titleString: String = ""
  18. // MARK: VC's Life cycle
  19. var webView: UIWebView?
  20. var activityIndicator: UIActivityIndicatorView?
  21. override func viewDidLoad() {
  22. UIApplication.shared.statusBarStyle = .lightContent
  23. super.viewDidLoad()
  24. let barButton = UIBarButtonItem.init(title: "Close", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.cancel))
  25. self.navigationItem.rightBarButtonItem = barButton
  26. webView = UIWebView(frame: self.view.frame)
  27. webView?.delegate = self
  28. activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
  29. activityIndicator?.center = self.view.center
  30. self.view.addSubview(webView!)
  31. self.view.addSubview(activityIndicator!)
  32. }
  33. private func setupWebView() {
  34. self.webView?.delegate = self
  35. }
  36. // MARK: IBActions
  37. override func viewWillAppear(_ animated: Bool) {
  38. self.navigationItem.title = titleString
  39. self.openURLInWebView()
  40. }
  41. @objc func cancel() {
  42. self.dismiss(animated: true, completion: nil)
  43. }
  44. func openURLInWebView() {
  45. if NetworkReachabilityManager()?.isReachable == true {
  46. if let url = URL.init(string: self.url ?? "") {
  47. let request = URLRequest(url: url)
  48. webView?.loadRequest(request)
  49. }
  50. }else {
  51. self.alert(message: "Please check your internet connection", title: "")
  52. }
  53. }
  54. }
  55. // MARK: UIWebViewDelegate
  56. extension WebLinksViewController: UIWebViewDelegate {
  57. func webViewDidStartLoad(_ webView: UIWebView) {
  58. self.activityIndicator?.startAnimating()
  59. }
  60. func webViewDidFinishLoad(_ webView: UIWebView) {
  61. self.activityIndicator?.stopAnimating()
  62. }
  63. func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
  64. self.activityIndicator?.stopAnimating()
  65. self.alert(message: error.localizedDescription)
  66. }
  67. }