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")
}