Guest User

Untitled

a guest
Aug 7th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. let username = "xxxx"
  2. let password = "xyz"
  3. let loginData = String(format: "%@:%@", username, password).data(using: String.Encoding.utf8)!
  4. let base64LoginData = loginData.base64EncodedString()
  5.  
  6. // create the request
  7. let url = URL(string: "http://demo.xyz.com/dctm-rest/repositories/iol_ref2/objects/0900a1848039590d/content-media?format=crtext&modifier=&page=0")!
  8.  
  9. var request = URLRequest(url: url)
  10. request.httpMethod = "GET"
  11. request.setValue("Basic (base64LoginData)", forHTTPHeaderField: "Authorization")
  12. let session = URLSession.shared
  13.  
  14. let taskk = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
  15. if let tempLocalUrl = tempLocalUrl, error == nil {
  16. // Success
  17. if let statusCode = (response as? HTTPURLResponse)?.statusCode {
  18. print("Success: (statusCode)")
  19. }
  20.  
  21. do {
  22. let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
  23.  
  24. let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  25.  
  26. if paths.count > 0
  27. {
  28. documentsDirectory = paths.first!
  29. }
  30. var fileURL : URL = URL(fileURLWithPath: documentsUrl!.absoluteString+filename)
  31.  
  32. if(mimetype == "text/plain"){
  33. fileURL = URL(fileURLWithPath: documentsUrl!.absoluteString+filename+".txt")
  34. }else if(mimetype == "application/pdf"){
  35. fileURL = URL(fileURLWithPath: documentsUrl!.absoluteString+filename+".pdf")
  36. }
  37.  
  38.  
  39. print(fileURL)
  40. let dataFromURL = NSData(contentsOf: tempLocalUrl)
  41. dataFromURL?.write(to: fileURL, atomically: true)
  42.  
  43. print(dataFromURL)
  44. try FileManager.default.copyItem(at: tempLocalUrl, to: fileURL)
  45.  
  46.  
  47. OperationQueue.main.addOperation {
  48.  
  49. self.activityIndicator.stopAnimating()
  50. UIApplication.shared.endIgnoringInteractionEvents()
  51. let documentController = UIDocumentInteractionController.init(url: fileURL)
  52. documentController.delegate = self as? UIDocumentInteractionControllerDelegate
  53. documentController.presentPreview(animated: true)
  54.  
  55. }
  56.  
  57. } catch (let writeError) {
  58. print("error writing file : (writeError)")
  59. }
  60.  
  61. } else {
  62. print("Failure: %@", error?.localizedDescription);
  63. }
  64. }
  65. taskk.resume()
Add Comment
Please, Sign In to add comment