I have written code for retrieving data from file or download that file and show in your phone. But I don't understand why it is not displaying data. When I opened that file it shows blank and but it is printing some hexadecimal code in a console.
I am very new to this IOS development, kindly please help me this. I want to retrieve data from the server through a link, and display it on the mobile. Thanks in advance you can help with some alternate way.
Below is my code
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()