1

This function makes a request to a SOAP API, and then uses the response to build a view. I would like to cache this response for offline usage.

func getUserFilesWithUserObj(userObj:User,completionhandler:@escaping (_ userFiles:NSArray, _ status: Bool) -> Void){
        let soapMessage: String = String(format: "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><getFilesByID xmlns=\"http://example.com/\"><_usrID>%@</_usrID><_utStatus>%@</_utStatus></getFilesByID></soap12:Body></soap12:Envelope>",userObj.userId!,"9")

        guard let urlRequest = self.createSoapRequestFromSoapMessage(soapmessage: soapMessage) else{
            return
        }
        var localPath = AppDelegate.getDownloadFilesFolderPath()
        localPath = localPath.stringByAppendingPathComponent(path: "user-files")

        if FileManager.default.fileExists(atPath: localPath){
            do {
                self.processUserFiles(files: try String(contentsOf: NSURL(fileURLWithPath: localPath) as URL, encoding: .utf8))
            } catch let error as NSError { print(error) }
        }
        else{

            SwiftLoader.show(animated: true)
            Alamofire.request(urlRequest).responseData { (response) in
                SwiftLoader.hide()
                if response.error == nil{
                    if response.data != nil{
                        let strData = String(data: response.data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
                        do {
                            try strData?.write(to: NSURL(string: localPath)! as URL, atomically: false, encoding: .utf8)
                        } catch let error as NSError { print(error) }

                        self.processUserFiles(files: strData!)

                        completionhandler(self.arrUserTreatment!, true)
                    }
                    completionhandler(NSArray(), false)
                }
                else{
                    completionhandler(NSArray(), false)
                }
            }
        }
    }

Here is the error message I am receiving in the Xcode terminal,

2019-08-19 21:51:57.131031-0400 AppName[412:28510] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported." UserInfo={NSURL=/var/mobile/Containers/Data/Application/AFFBB2E6-0EB9-4206-9AD5-EDB3AD22A23F/Documents/DownloadFiles/user-files}

How can I save the XML from this response to a file and reload it later?

oxr463
  • 1,573
  • 3
  • 14
  • 34
  • You can use anything like UserDefaults or file to save any response. Once you use this you can remove or release these objects.Save whole response as a string and when you want to use it again convert it. You can go with core data too, because instead of storing whole XML response you can save only those values which you want and it will also help you to use those values easily. – Chaaruu Jadhav Aug 20 '19 at 04:13
  • Could you provide examples or further references for these? – oxr463 Aug 20 '19 at 11:13

1 Answers1

0

Prepending file:// to the localPath fixed the error.

var localPath = AppDelegate.getDownloadFilesFolderPath()
localPath = "file://" + localPath.stringByAppendingPathComponent(path: "user-files")

Source: https://stackoverflow.com/a/39646856/8507637

oxr463
  • 1,573
  • 3
  • 14
  • 34