How to read data from file placed in server through url link in ios swift (4)?

两盒软妹~` 提交于 2019-12-11 08:47:46

问题


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()

来源:https://stackoverflow.com/questions/51723230/how-to-read-data-from-file-placed-in-server-through-url-link-in-ios-swift-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!