Access contents of SKDownload after processing in-app purchase using SKPaymentQueue

177 Views Asked by At

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!

0

There are 0 best solutions below