Guest User

Untitled

a guest
Dec 4th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3. login()
  4. }
  5.  
  6. func login()
  7. {
  8. // *********** Get stored hashkey **************
  9. let hashcode = getHashcode()
  10.  
  11. // ********** Check network connection *********
  12. let netConnection = Connection.isConnectedToNetwork()
  13. print("net connection: ", netConnection)
  14.  
  15. if netConnection == true
  16. {
  17. if hashcode != "00000"
  18. {
  19.  
  20. print("local key found", hashcode)
  21. // We dont have local key
  22. let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
  23. //webview.loadRequest(NSURLRequest(URL: NSURL(string: "about:blank")!))
  24. view.addSubview(webview)
  25. webview.loadPlugin(jsapi(), namespace: "jsapi")
  26.  
  27.  
  28. let url:NSURL = NSURL(string: serverLocation + onlineLoginApi)!
  29. let session = NSURLSession.sharedSession()
  30. let request = NSMutableURLRequest(URL: url)
  31.  
  32. request.HTTPMethod = "POST"
  33. request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
  34.  
  35. let paramString = "/?username=username&password=password"
  36. request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
  37.  
  38. let task = session.downloadTaskWithRequest(request) {
  39. (
  40. let location, let response, let error) in
  41.  
  42. guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
  43. print("error")
  44. return
  45. }
  46.  
  47. let urlContents = try! NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding)
  48.  
  49. guard let _:NSString = urlContents else {
  50. print("error")
  51. return
  52. }
  53.  
  54. print(urlContents)
  55.  
  56. }
  57.  
  58. task.resume()
  59.  
  60. // you must tell webview to load response
  61. webview.loadRequest(request)
  62.  
  63. }
  64. else{
  65.  
  66. print("local key found", hashcode)
  67. // ********* Found local key go to site pass key over ************
  68.  
  69. let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
  70. view.addSubview(webview)
  71. webview.loadPlugin(jsapi(), namespace: "jsapi")
  72.  
  73. let req = NSMutableURLRequest(URL: NSURL(string:serverLocation + onlineLoginApi + "?hashcode=(hashcode)")!)
  74. req.HTTPMethod = "POST"
  75. req.HTTPBody = "/?hashcode=(hashcode)".dataUsingEncoding(NSUTF8StringEncoding)
  76. NSURLSession.sharedSession().dataTaskWithRequest(req)
  77. { data, response, error in
  78. if error != nil
  79. {
  80. //Your HTTP request failed.
  81. print(error!.localizedDescription)
  82. } else {
  83. //Your HTTP request succeeded
  84. print(String(data: data!, encoding: NSUTF8StringEncoding))
  85. }
  86. }.resume()
  87. webview.loadRequest(req)
  88.  
  89. }
  90.  
  91. }
  92. else{
  93.  
  94. // No connection to internet
  95.  
  96. let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
  97. view.addSubview(webview)
  98. webview.loadPlugin(jsapi(), namespace: "jsapi")
  99.  
  100. let root = NSBundle.mainBundle().resourceURL!
  101. let url = root.URLByAppendingPathComponent("/www/error-no-connection.html")
  102. webview.loadFileURL(url, allowingReadAccessToURL: root)
  103. print("No internet connection")
  104.  
  105. }
  106. }
  107. class jsapi: NSObject {
  108.  
  109.  
  110. // Reconnect button on interface
  111. func retryConnection()
  112. {
  113. print("Reconnect clicked")
  114. dispatch_async(dispatch_get_main_queue())
  115. {
  116. let netConnections = Connection.isConnectedToNetwork()
  117.  
  118. if netConnections == true {
  119. let netalert = UIAlertView(title: "Internet on line", message: nil, delegate: nil, cancelButtonTitle: "OK")
  120. netalert.show()
  121.  
  122. let url = self.serverLocation + self.onlineLoginApi
  123. let hashcode = ViewController().getHashcode()
  124.  
  125. if(hashcode != "00000") {
  126. let url = url + "?hashcode=(hashcode)"
  127. print("url: ", url)
  128. }
  129.  
  130. ViewController().loadPagelive(url)
  131.  
  132. }
  133.  
  134.  
  135. else{
  136. let netalert = UIAlertView(title: "Internet off line", message: nil, delegate: nil, cancelButtonTitle: "OK")
  137. netalert.show()
  138. }
  139. }
  140. print("retryConnect end")
  141. }
  142. }
Add Comment
Please, Sign In to add comment