Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let username = "xxxx"
- let password = "xyz"
- let loginData = String(format: "%@:%@", username, password).data(using: String.Encoding.utf8)!
- let base64LoginData = loginData.base64EncodedString()
- // create the request
- let url = URL(string: "http://demo.xyz.com/dctm-rest/repositories/iol_ref2/objects/0900a1848039590d/content-media?format=crtext&modifier=&page=0")!
- var request = URLRequest(url: url)
- request.httpMethod = "GET"
- request.setValue("Basic (base64LoginData)", forHTTPHeaderField: "Authorization")
- let session = URLSession.shared
- let taskk = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
- if let tempLocalUrl = tempLocalUrl, error == nil {
- // Success
- if let statusCode = (response as? HTTPURLResponse)?.statusCode {
- print("Success: (statusCode)")
- }
- do {
- let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
- let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
- if paths.count > 0
- {
- documentsDirectory = paths.first!
- }
- var fileURL : URL = URL(fileURLWithPath: documentsUrl!.absoluteString+filename)
- if(mimetype == "text/plain"){
- fileURL = URL(fileURLWithPath: documentsUrl!.absoluteString+filename+".txt")
- }else if(mimetype == "application/pdf"){
- fileURL = URL(fileURLWithPath: documentsUrl!.absoluteString+filename+".pdf")
- }
- print(fileURL)
- let dataFromURL = NSData(contentsOf: tempLocalUrl)
- dataFromURL?.write(to: fileURL, atomically: true)
- print(dataFromURL)
- try FileManager.default.copyItem(at: tempLocalUrl, to: fileURL)
- OperationQueue.main.addOperation {
- self.activityIndicator.stopAnimating()
- UIApplication.shared.endIgnoringInteractionEvents()
- let documentController = UIDocumentInteractionController.init(url: fileURL)
- documentController.delegate = self as? UIDocumentInteractionControllerDelegate
- documentController.presentPreview(animated: true)
- }
- } catch (let writeError) {
- print("error writing file : (writeError)")
- }
- } else {
- print("Failure: %@", error?.localizedDescription);
- }
- }
- taskk.resume()
Add Comment
Please, Sign In to add comment