Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.78 KB | None | 0 0
  1. DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async(execute: {
  2.            
  3.             let taskID = self.beginBackgroundUpdateTask()
  4.             print("background taks started:")
  5.             print(taskID)
  6.            
  7.             if let userusername: String = Keychain(service: KCservice, accessGroup: KCaccessGroup).accessibility(.afterFirstUnlock)["userusername"] {
  8.                 if userusername != ""{
  9.                     if let userpassword: String = Keychain(service: KCservice, accessGroup: KCaccessGroup).accessibility(.afterFirstUnlock)["userpassword"] {
  10.                         if userpassword != ""{
  11.                            
  12.                             let loginString = String(format: "%@:%@", userusername, userpassword)
  13.                             let loginData = loginString.data(using: String.Encoding.utf8)!
  14.                             let base64LoginString = loginData.base64EncodedString()
  15.                            
  16.                             let url = URL(string: "\(polyserver)oauth/v2/user/tokens")
  17.                             let postParams: [String : Any] = [:]
  18.                            
  19.                             var request = URLRequest(url: url!)
  20.                             request.httpMethod = "DELETE"
  21.                             request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  22.                             request.setValue("basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
  23.                             request.setValue("no-cache", forHTTPHeaderField: "Cache-Control")
  24.                             do
  25.                             {
  26.                                 request.httpBody = try JSONSerialization.data(withJSONObject: postParams, options: JSONSerialization.WritingOptions())
  27.                             }
  28.                             catch
  29.                             {
  30.                                 print("Caught an error: \(error)")
  31.                             }
  32.                             let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
  33.                                 guard error == nil && data != nil else {                                                          // check for fundamental networking error
  34.                                     print("error=\(String(describing: error))")
  35.                                     self.endBackgroundUpdateTask(taskID)
  36.                                     return
  37.                                 }
  38.                                
  39.                                 if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
  40.                                     print("statusCode should be 200, but is \(httpStatus.statusCode)")
  41.                                     print("response = \(String(describing: response))")
  42.                                     self.endBackgroundUpdateTask(taskID)
  43.                                     return
  44.                                 }
  45.                                
  46.                                 let responseString = String(data: data!, encoding: String.Encoding.utf8)
  47.                                 print("responseString = \(String(describing: responseString))")
  48.                                 self.endBackgroundUpdateTask(taskID)
  49.                                
  50.  
  51.                             }
  52.                             task.resume()
  53.                            
  54.                            
  55.                            
  56.                         }else{self.endBackgroundUpdateTask(taskID)}
  57.                     }else{self.endBackgroundUpdateTask(taskID)}
  58.                 }else{self.endBackgroundUpdateTask(taskID)}
  59.             }else{self.endBackgroundUpdateTask(taskID)}
  60.            
  61.            
  62.         })
  63.        
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement