New to Telerik UI for Xamarin? Download free 30-day trial

DataSource for Xamarin.iOS: Accessing remote data

With TLKDataSource it is easy to utilize data coming from remote sources, for example web services. To do this you should call the LoadDataFromURL method like demonstrated below:

var chart = new TKChart (this.View.Bounds);
chart.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
this.View.Add (chart);

string url = "";
dataSource.LoadDataFromURL (url, TKDataSourceDataFormat.JSON, "dayList", (NSError err) => {
    if (err != null) {
        Console.WriteLine("Can't connect with the server!");

    dataSource.Settings.Chart.CreateSeries((TKDataSourceGroup group) => {
        TKChartSeries series = null;
        if (group.ValueKey == "clouds") {
            series = new TKChartColumnSeries();
            series.YAxis = new TKChartNumericAxis(NSObject.FromObject(0), NSObject.FromObject(100));
            series.YAxis.Title = "clouds";
            series.YAxis.Style.TitleStyle.RotationAngle = (float)Math.PI/2.0f;
        else {
            series = new TKChartLineSeries();
            series.YAxis = new TKChartNumericAxis(NSObject.FromObject(-10), NSObject.FromObject(30));
            if (group.ValueKey == "temp.min") {
                series.YAxis.Position = TKChartAxisPosition.Right;
                series.YAxis.Title = "temperature";
                series.YAxis.Style.TitleStyle.RotationAngle = (float)Math.PI/2.0f;
        return series;

    dataSource.Map((NSObject item) => {
        double interval = ((NSNumber)item.ValueForKey(new NSString("dateTime"))).DoubleValue;
        NSDate date = NSDate.FromTimeIntervalSince1970(interval);
        item.SetValueForKey(date, new NSString("dateTime"));
        return item;

    NSObject[] items = this.dataSource.Items;
    NSMutableArray newItems = new NSMutableArray();
    newItems.Add(new TKDataSourceGroup(items, "clouds", "dateTime"));
    newItems.Add(new TKDataSourceGroup(items, "temp.min", "dateTime"));
    newItems.Add(new TKDataSourceGroup(items, "temp.max", "dateTime"));
    dataSource.ItemSource = newItems;

    chart.DataSource = dataSource;

    var formatter = new NSDateFormatter();
    formatter.DateFormat = "dd";
    TKChartDateTimeAxis xAxis = (TKChartDateTimeAxis)chart.XAxis;
    xAxis.MajorTickInterval = 1;
    xAxis.PlotMode = TKChartAxisPlotMode.BetweenTicks;
    xAxis.LabelFormatter = formatter;
    xAxis.Title = "date";
    xAxis.MinorTickIntervalUnit = TKChartDateTimeAxisIntervalUnit.Days;

The code above downloads a JSON formatted data from the specified web service, sets the ValueKey property and presents the result in TKChart.


In some cases the data coming from a remote source is protected. In such scenarios you should inherit a class from TLKDataSource and override the appropriate NSURLConnectionDelegate method. TLKDataSource supports all authentication modes supported by NSURLConnection. Further information about authentication modes is available in Apple documentation.

The following code demonstrates the usage of basic authentication with TLKDataSource:

public class DataSourceDocsAuth : TLKDataSource
    public DataSourceDocsAuth ()
    public class MyDataSource: TLKDataSource
        [Export ("connection:didReceiveAuthenticationChallenge:")]
        public void ReceivedAuthenticationChallenge (NSUrlConnection connection, NSUrlAuthenticationChallenge challenge) 
            if (challenge.PreviousFailureCount == 0) {
                var credential = new NSUrlCredential ("USER", "PASSWORD", NSUrlCredentialPersistence.ForSession);
                challenge.Sender.UseCredential (credential, challenge);
            else {
                Console.WriteLine("previous authentication failure");
In this article