Search code examples
xamarinxamarin.iosapple-watch

Xamarin Apple Watch Extension - Consuming simple JSON Web Service


I've built a simple Xamarin.forms app which connects to a web service and gets one piece of data and displays it.

I wanted to do the same on an apple watch (targetting a series 2 apple watch) but I just don't understand how it works.

I've added the watch project and the watch extension, created the interface... Got a "refresh" button which changes the value of the label to a hard coded value but I want to get the data from a simple web service on app startup on wake and when button is activated (similar to my xamarin.forms iphone app)

I've tried using the classes that I use on the forms app to get the data but I can't reference them. I've tried using httpclient and that doesn't work.

After googling it appears I am suppose to use a HandleWatchKitExtensionRequest delegate in the phone app to get the data in a dictionary and then display the data. I haven't been able to find any really simple examples of this, but I'm confused because since apple watch version 3 the watch has been autonomous with cell access and therefore it doesn't have the host app necessarily.

So what I'm asking, is can the watch actually consume a web service alone using a xamarin phone app and if so, is there any simple examples because I can't find any (that aren't from 2015).

Sorry if this is vague, or I sound like a newbie (I definitely am a newbie when it comes to xamarin but any pointers or help would be appreciated)


Solution

  • According to https://developer.apple.com/documentation/foundation/nsurlsession?language=objc NSURLSession is available since watchOS 2.0. It is used to download the content from the web and is extensively documented just don't search for watchOS specifically as it is the same on all Apple OS.

    But as HttpClient uses NSURLSession it is also available, so, maybe you need to add the reference to System.Net.Http.