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

Communicating with native APIs


In this recipe, we will discuss using native iOS APIs to connect and consume REST services.

Getting ready

For this recipe, we will work on the ForecastApp we created in the previous recipe, Consuming REST services. Open the project in Xamarin Studio.

How to do it…

Perform the following steps:

  1. Comment out the code in the ViewDidLoad method and add the following code:

    this.btnForecast.TouchUpInside += (sender, e) => {
      NSUrlRequest request = new NSUrlRequest(new NSUrl("http://api.ometfn.net/0.1/forecast/eu12/46.5,6.32/now.json"));
      NSUrlConnection connection = new NSUrlConnection(request, new ConnectionDelegate((response) => {
        JsonValue jsonObj = JsonValue.Parse(response);
        JsonArray tempArray = (JsonArray)jsonObj["temp"];
        double temp = (double)tempArray[0];
        JsonArray windSpeedArray = (JsonArray)jsonObj["wind_10m_ground_speed"];
        double windSpeed = (double)windSpeedArray[0];
        this.lblOutput.Text = string.Format("Temperature: { 0}\ nWind...