Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func viewDidLoad() {
- super.viewDidLoad()
- login()
- }
- func login()
- {
- // *********** Get stored hashkey **************
- let hashcode = getHashcode()
- // ********** Check network connection *********
- let netConnection = Connection.isConnectedToNetwork()
- print("net connection: ", netConnection)
- if netConnection == true
- {
- if hashcode != "00000"
- {
- print("local key found", hashcode)
- // We dont have local key
- let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
- //webview.loadRequest(NSURLRequest(URL: NSURL(string: "about:blank")!))
- view.addSubview(webview)
- webview.loadPlugin(jsapi(), namespace: "jsapi")
- let url:NSURL = NSURL(string: serverLocation + onlineLoginApi)!
- let session = NSURLSession.sharedSession()
- let request = NSMutableURLRequest(URL: url)
- request.HTTPMethod = "POST"
- request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
- let paramString = "/?username=username&password=password"
- request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
- let task = session.downloadTaskWithRequest(request) {
- (
- let location, let response, let error) in
- guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
- print("error")
- return
- }
- let urlContents = try! NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding)
- guard let _:NSString = urlContents else {
- print("error")
- return
- }
- print(urlContents)
- }
- task.resume()
- // you must tell webview to load response
- webview.loadRequest(request)
- }
- else{
- print("local key found", hashcode)
- // ********* Found local key go to site pass key over ************
- let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
- view.addSubview(webview)
- webview.loadPlugin(jsapi(), namespace: "jsapi")
- let req = NSMutableURLRequest(URL: NSURL(string:serverLocation + onlineLoginApi + "?hashcode=(hashcode)")!)
- req.HTTPMethod = "POST"
- req.HTTPBody = "/?hashcode=(hashcode)".dataUsingEncoding(NSUTF8StringEncoding)
- NSURLSession.sharedSession().dataTaskWithRequest(req)
- { data, response, error in
- if error != nil
- {
- //Your HTTP request failed.
- print(error!.localizedDescription)
- } else {
- //Your HTTP request succeeded
- print(String(data: data!, encoding: NSUTF8StringEncoding))
- }
- }.resume()
- webview.loadRequest(req)
- }
- }
- else{
- // No connection to internet
- let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
- view.addSubview(webview)
- webview.loadPlugin(jsapi(), namespace: "jsapi")
- let root = NSBundle.mainBundle().resourceURL!
- let url = root.URLByAppendingPathComponent("/www/error-no-connection.html")
- webview.loadFileURL(url, allowingReadAccessToURL: root)
- print("No internet connection")
- }
- }
- class jsapi: NSObject {
- // Reconnect button on interface
- func retryConnection()
- {
- print("Reconnect clicked")
- dispatch_async(dispatch_get_main_queue())
- {
- let netConnections = Connection.isConnectedToNetwork()
- if netConnections == true {
- let netalert = UIAlertView(title: "Internet on line", message: nil, delegate: nil, cancelButtonTitle: "OK")
- netalert.show()
- let url = self.serverLocation + self.onlineLoginApi
- let hashcode = ViewController().getHashcode()
- if(hashcode != "00000") {
- let url = url + "?hashcode=(hashcode)"
- print("url: ", url)
- }
- ViewController().loadPagelive(url)
- }
- else{
- let netalert = UIAlertView(title: "Internet off line", message: nil, delegate: nil, cancelButtonTitle: "OK")
- netalert.show()
- }
- }
- print("retryConnect end")
- }
- }
Add Comment
Please, Sign In to add comment