Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Consuming REST services


In this recipe, we will discuss how to properly use and consume REST services with Xamarin.iOS.

Getting ready

Create a new Single View Application in Xamarin Studio and name it ForecastApp. In this recipe, we will use the Open Meteo Foundation REST API. The use of this API is subject to the terms of use stated in this page: http://openmeteofoundation.org/terms-of-use.

How to do it...

Perform the following steps to complete this recipe:

  1. Add a label and a button on ForecastAppViewController. Make sure that the label's Lines property is set to at least three lines.

  2. Add the following code in the controller's ViewDidLoad method:

    this.btnForecast.TouchUpInside += async (sender, e) => {
    
      HttpClient client = new HttpClient();
      string jsonResponse = await client.GetStringAsync("http://api.ometfn.net/0.1/forecast/eu12/46.5,6.32/now.json");
      JsonValue jsonObj = JsonValue.Parse(jsonResponse);
      JsonArray tempArray = (JsonArray)jsonObj["temp"];
      double temp = (double)tempArray...