Here is my code
struct ContentView: View {
@State private var presentError : Bool = false
@State private var error: Error?
@State private var map: Map = {
let map = Map(basemapStyle: .arcGISTopographic)
map.initialViewpoint = Viewpoint(
latitude: 34.723638,
longitude: -92.500932,
scale: 72_000
)
return map
}()
var body: some View {
MapView(map: map)
.toolbar(content: {
ToolbarItem(placement: .bottomBar,content: {
Text("Load Shapefile")
.task {
do{
try await LoadShapefile()
}catch{
print(error)
self.error = error
}
}
})
})
.onAppear{
ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = ChallengeHandler()
}
.onDisappear{
ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = nil
}
.alert(isPresented: $presentError){
Alert(title: Text("Something went wrong"), message: Text(error!.localizedDescription))
}
}
func LoadShapefile() async throws{
print("shapefile ",URL.shapefilePath.absoluteString)
// Instantiates shapefile feature table with the path to the .shp file.
let shapefileTable = ShapefileFeatureTable(fileURL: .shapefilePath)
try await shapefileTable.load() // Throwing error
// Creates a feature layer for the shapefile feature table.
let shapefileLayer = FeatureLayer(featureTable: shapefileTable)
// Adds the layer to the map.
map.addOperationalLayer(shapefileLayer)
}
private struct ChallengeHandler: ArcGISAuthenticationChallengeHandler {
func handleArcGISAuthenticationChallenge(
_ challenge: ArcGISAuthenticationChallenge
) async throws -> ArcGISAuthenticationChallenge.Disposition {
// NOTE: Never hardcode login information in a production application.
// This is done solely for the sake of the sample.
return .continueWithCredential(
try await TokenCredential.credential(for: challenge, username: "myusername", password: "my password")
)
}
}
}
private extension URL{
static var shapefilePath : URL { Bundle.main.url(forResource: "test", withExtension: "shp")! }
}
I have also added files under the Copy Bundle Resource in Build Phase.
I tried reading another text file using Bundle. It worked fine. Just the shape file is giving an error.