UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Works on simulator but not on real watch

Forums > watchOS

I'm writing my first watchOS app so I am sure my problems are self-inflicted!

I am trying to fetch from a url and then JSON decode and display the results.

The app runs correctly on the watchOS simulator but not on my real series 5.

It launches correctly and displays a list of target files to select from. The list is hard-coded in an array so no network activity at this stage.

Tapping an item is supposed to customise the url string, fetch the data and then decode it and display on a detail view.

Works perfectly on the simulator.

On the real device it displays the list correctly. After tapping an item it displays the detail view but all the fields which should be populated from the JSON decode are blank.

I've set the ATS for the target domain so I don't think that is a problem.

My watch has a data plan so I'm assuming that enables data download?

Is there anything different either when trying to fetch data using urlsession.shared.data on the watch or when trying to JSON decode the received data that might cause the problem I'm having?

At this point I don't know if my problem is in the fetch or the decode - can anyone give me a clue how to check the fetch worked or failed because that would then narrow the scope a bit.

My thanks in advance for any assistance - sincerely appreciated!


Are there any fields which are Int? There is a difference between Watch and MacOS/iOS handling Int. Although, your watch is a 64bit device the default Int on watch is 32bit.

So you need to declare your Int on watch not just as Int but as Int64. Learned it myself the hard way.


Many thanks for that!

I do have Int in my JSON and they can be 14+ digits in length so that is probably an issue which was lining up to bite me. I've changed them all to Int64 as you recommend.

I stayed up half the night trying to either crack or narrow the problem down and found:

1) Other people have reported a similar problem - working in sim but not on device

2) My watch was successfully retrieving the file - I checked data.count and got reasonable values

3) I simplified my JSON - taking out an array and reducing the file to only about 5 - 6 strings

4) The JSON decode looks to be failing with "The data couldn't be read because it isn't in the correct format."

I believe there is more specific error information available but I don't know how to access it - the examples I have seen all simply use print(error) to get details but as I'm running on a real device I don't see how to get the output from a print statement.



In this thread https://www.hackingwithswift.com/forums/swiftui/decoding-api-again-how-do-i-decode-this-json/21115/ is an extension for Bundle from NigelGee.

With this extension you get a more detailed error description.


Again, thank you for your continued help - appreciated!

For some reason which I don't yet know, things have started working!

My watch app is now downloading and then decoding the JSON.

To reach this stage I stripped the JSON down to only about 5 string values. I will next start adding back the array which contains the numeric items and see how things go.

My initial efforts were using Xcode 15B5 but I've dropped back to 14.3.1 and created the app again as a new project. Perhaps some of my problems were due to trying 15 - I'm going to stick with 14 until such time as 15 is finally released.


I have finally discovered the source of the problem - posting to hopefully assist anyone else who may hit the same issue.

My watch was still paired to my previous iPhone which no longer had a sim installed. My watch app would work if my phone had a WiFi connection. Tested and verified this both at home and at other locations.

So - problem was not related to Xcode 15B5 or indeed any other vague cause.


Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.