1

I've been trying to access a downloaded SKDownload zip file after a successful in-app purchase as such:

func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {
  downloads.forEach ({ (download) -> Void in
     switch download.state {
        ...
        case .finished:
           self.processDownload(download: download)
           ...
           break

        ...
    })
}

Here's the function that processes the downloaded SKDownload file:

func procesessDownload(download: SKDownload) {
    guard let hostedContentPath = download.contentURL else {
        return
    }

    do {
        // THIS LINE OF CODE THROWS WITH THE ERROR POSTED BELOW
        let files = try FileManager.default.contentsOfDirectory(atPath: hostedContentPath.relativePath)

    } catch {
        //catch error
    }
}

When I inspect the download.contentURL it is there:

file:///private/var/mobile/Containers/Data/Application/D755EC3C-2BA6-43A8-BB21-938B38EBFCB7/Library/Caches/EB11461D-71C8-42C7-9C99-E6F8B81161EE.zip/
  - _url : file:///private/var/mobile/Containers/Data/Application/D755EC3C-2BA6-43A8-BB21-938B38EBFCB7/Library/Caches/EB11461D-71C8-42C7-9C99-E6F8B81161EE.zip/

But upon trying to access the file using FileManager using this line of code above:

let files = try FileManager.default.contentsOfDirectory(atPath: hostedContentPath.relativePath)

I keep getting the following error stating that the file doesn't exist:

Error Domain=NSCocoaErrorDomain Code=260 "The file “EB11461D-71C8-42C7-9C99-E6F8B81161EE.zip” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/D755EC3C-2BA6-43A8-BB21-938B38EBFCB7/Library/Caches/EB11461D-71C8-42C7-9C99-E6F8B81161EE.zip, NSUnderlyingError=0x28320d500 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

  • I tried adding / at the end to treat the zip file as a directory and many other things to no avail.

  • I even tried to access the directory containing that file (up to the zip file):

/private/var/mobile/Containers/Data/Application/D755EC3C-2BA6-43A8-BB21-938B38EBFCB7/Library/Caches/

The error says that "Caches" directory not found.

  • I've also tried adding "Contents" to the path like one of the example codes I found by a random person.

I've been banging my head for more than a day straight and I couldn't find any documentation on the matter. Any help would be greatly appreciated, thanks!

KBog
  • 3,800
  • 1
  • 25
  • 31
  • "upon trying to access the file using FileManager" How!? – El Tomato Jun 29 '20 at 00:55
  • I've updated my question to reflect that part just now. Basically that line of code `let files = try FileManager.default.contentsOfDirectory(atPath: hostedContentPath.relativePath)` is throwing with the error I posted above. – KBog Jun 29 '20 at 02:39
  • That's not how you access the Library folder for your app. – El Tomato Jun 29 '20 at 02:42
  • I'm not looking to access the Library folder, I just want to access the downloaded file from the in-app purchase transaction. i.e.: `SKDownload.contentURL`. – KBog Jun 29 '20 at 02:43
  • Your file is found in Library > Caches, no!? – El Tomato Jun 29 '20 at 02:47
  • Nope, nothing there. If I get the path of the Caches directory as such: `FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first` the path I get is different from the one that Apple used to download my in-app purchase hosted content (apple one ends up in /private/var/...). – KBog Jun 30 '20 at 03:15
  • I see. Sorry about my ignorance. I have never had a downloadable content as IAP. Have you read an article at the following URL? https://kitefaster.com/2017/06/14/use-content-hosting-app-purchase-content/ – El Tomato Jun 30 '20 at 03:23
  • No worries, thanks for your replies! Yes I did follow everything I found online, nothing worked for me :( – KBog Jun 30 '20 at 18:34

0 Answers0