0

I have 2 apps that are trying to share the same TPK offline base map. I have App Groups enabled and have the TPK file stored in the shared container but it does not seem to work when referencing the file with AGSLocalTiledLayer.

The error message I see says "Cache at path '(null)' not found"

Does anyone know if this approach is possible?

1
  • I have run in to the same problem. Backing up a step, I have the same problem when specifying a full path even to my own Documents directory. So AGSLocalTiledLayer:name: works, which looks for the file in the app bundle or app's Documents directory, but AGSLocalTiledLayer:path: does NOT work when I point it to the same file in the Document's directory.
    – JohnC
    Commented Nov 11, 2016 at 17:09

1 Answer 1

1

I got this working. In my case, the problem was passing a proper path name without "file://" at the beginning. Using url.absolutePath does this, so you need to strip it off when converting URL's. Here is some sample code:

// Get a URL to the shared file location
func getSharedFilePath(appGroup:String)->URL? {

    if let directoryPath = FileManager().containerURL(forSecurityApplicationGroupIdentifier: appGroup) {
        return directoryPath.appendingPathComponent(sharedFilename)
    } else {
        return nil
    }
}

public func writeToSharedFile(version:Int)->String {

    guard let fileURL = getSharedFilePath(appGroup:applicationGroup) else {
        return("Error getting shared file path")
    }

    guard !FileManager.default.fileExists(atPath: fileURL.path) else {
        return("File already exists")
    }

    // get path to file in app bundle
    guard let tpkFile = Bundle.main.url(forResource: appBundleFilename, withExtension: "tpk") else {
        return("Error finding file in app bundle")
    }

    // read file from app bundle to data variable
    var data:Data? = nil
    do {
        data = try Data(contentsOf: tpkFile)
    } catch {
        print("Error saving file")
    }

    // write data variable to file in file system
    do {
        try data?.write(to: fileURL)
        print("Success: shared file saved: \(fileURL)")
    } catch {
        print("Error saving shared file")
    }

    return("ok")
}

public func readFromSharedFile()->String {

    guard let url = getSharedFilePath(appGroup:applicationGroup) else {
        return("Error getting shared file path")
    }

    // read file from file system to data variable
    var fullPath = url.absoluteString
    // strip off "file://"
    fullPath = fullPath.substring(from: fullPath.index(fullPath.startIndex, offsetBy: 7))
    let basemap = AGSLocalTiledLayer(path: fullPath)    //old syntax

    if let error = basemap?.error {
        print("Read shared file error: " + error.localizedDescription)
    } else {
        print("Read shared file loaded OK")
    }

    return("data file read successfully")
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.