Category Archives: iOS

Xamarin.iOS- Storing Data

Xamarin.iOS can use the same System.IO classes to work with files and directories in iOS that you would use in any .NET application. However, despite the familiar classes and methods, iOS implements some restrictions on the files that can be created or accessed and also provides special features for certain directories. This article outlines these restrictions and features, and demonstrates how file access works in a Xamarin.iOS application.

Locations of Persistent Data

iOS provides several options for saving application data based on the specific needs of the app. Some apps require private storage while others need to share data between apps.

There are five different places to store data:

  • Shared Preferences – Stores data in key/value pairs
  • Internal Storage – Stores private data in the memory of the device
  • External Storage – Stores data, which can be available to other apps on shared external storage
  • SQLite Databases – Stores structured data in a private database
  • Network Connection – Stores data on a Web server

iOS imposes some restrictions on what an application can do with the file system to preserve the security of an application’s data, and to protect users from malignant apps. These restrictions are part of the Application Sandbox – a set of rules that limits an application’s access to files, preferences, network resources, hardware, etc. An application is limited to reading and writing files within its home directory (installed location); it cannot access another application’s files.

iOS also has some file system-specific features: certain directories require special treatment with respect to backups and upgrades, and applications can also share files via iTunes

This video will walk you through creating an app that stores information to a text file on the device. We will store some information and then retrieve it for display using  the .NET System.IO classes for file system operations on iOS.

Xamarin.iOS – Adding Audio

Adding audio to your applications can take on many forms. This could be a sound-effect for a game, audio that is played on demand, or in the form of a podcast or audiobook.

The iOS framework includes support for playing variety of common media types, so that you can easily integrate audio, video and images into your applications. You can play audio or video from media files stored in your application’s resources (raw resources), from standalone files in the filesystem, or from a data stream arriving over a network connection.

Setting Your App Up for Audio

The AVAudioPlayer is used to playback audio data from either memory or a file. Apple recommends using this class to play audio in your app unless you are doing network streaming or require low latency audio I/O.

You can use the AVAudioPlayer to do the following:

  • Play sounds of any duration with optional looping.
  • Play multiple sounds at the same time with optional synchronization.
  • Control volume, playback rate and stereo positioning for each sounds playing.
  • Support features such as fast forward or rewind.
  • Obtain playback level metering data.

AVAudioPlayer supports sounds in any audio format provided by iOS, tvOS and OS X such as .aif, .wav or .mp3.

Starting and Stopping Audio

This video will show you how to use the AVAudioPlayer class to start and stop MP3 audio in your Xamarin iOS app.

Here is the link to the AudioManager class that is used in this video – https://dl.dropboxusercontent.com/u/13327672/295%20files/AudioManager.cs

Xamarin.iOS – Creating Lists and Opening the Browser

The Tables UITableViewController

Displaying a list is one of the most common design patterns used in mobile applications. Tables are one of the single most important controls in iOS applications. It’s hard to find an application that doesn’t use tables in some form. Tables in iOS are highly customizable and wrap a lot of functionality that we get for free.

aa1cTrKw7PEdW3

The source for the data displayed in the TableView can come from objects such as arrays or Lists. You can also bind to a data source such as a database table.

In order to determine what should happen when a row is clicked, you can use the RowSelected event.

A UITableView can have a ‘grouped’ or ‘plain’ style, and consists of the following parts:

Ci17gCS_PHIUfe

There is a lot of functionality to make your TableViews really pop in your application.

  • Swipe to delete individual cells.
  • Entering Edit mode to reveal delete buttons on each row
  • Entering Edit mode to reveal re-ordering handles.
  • Inserting new cells (with animation).

PipC_AnZwUmL-5

The primary classes used to display table views are shown here

In this tutorial, we will be using UITableViewSource – Xamarin.iOS-exclusive abstract class that provides all the methods required to display a table, including row count, returning a cell view for each row, handling row selection and many other optional features. Youmust subclass this to get a UITableView working.

Opening the Browser

When we want to perform actions using another app on the device, we can do a SharedApplication call. This is part of the UIApplication class, which holds all of the central administrative properties for any application running on iOS (more here: https://developer.xamarin.com/api/type/UIKit.UIApplication/)

Here is the video walk-through for this lesson that shows how to setup a TableView and launch the browser window.