mapbox / MapboxStatic.swift

Static map snapshots with overlays in Swift or Objective-C on iOS, macOS, tvOS, and watchOS
https://www.mapbox.com/api-documentation/?language=Swift#static
Other
189 stars 31 forks source link

Network error on WatchOS Device #121

Open dimix opened 1 year ago

dimix commented 1 year ago

On Apple Watch Device I've Network errors and the image is not loaded:

2023-05-05 12:02:52.574064+0200 Pin Vision Watch App[423:14134] Error getting network data status Error Domain=NSPOSIXErrorDomain Code=19 "Operation not supported by device"
2023-05-05 12:02:52.574407+0200 Pin Vision Watch App[423:14134] Task <C1C21836-E556-4D68-B493-977A1D301FB5>.<0> HTTP load failed, 0/0 bytes (error code: -1009 [1:50])
2023-05-05 12:02:52.574955+0200 Pin Vision Watch App[423:14131] NSURLConnection finished with error - code -1009

In simulator the image is loaded correctly without errors.

How can I load map images directly in Device? Thanks!

dimix commented 1 year ago

The code used is:

let snapshot = Snapshot(options: options, accessToken: Self.token)
let image = snapshot.image
dimix commented 1 year ago

I found the issue.

The code is wrong on WatchOS:

Snapshot.swift line 174

@objc open var image: Image? {
        if let data = try? Data(contentsOf: url) {
            return Image(data: data)
        } else {
            return nil
        }
    }

Loading directly from Data is not allowed. You need to use URLSession instead.

jatin19121990 commented 8 months ago

Extend Class Snapshot and implement new method using URLSession like:

extension Snapshot {
    func getImage() async throws -> UIImage? {
        let (data, _) = try await URLSession.shared.data(from: url)
        return UIImage(data: data)
    }
}