Book Image

iOS Development using MonoTouch Cookbook

By : Dimitris Tavlikos
Book Image

iOS Development using MonoTouch Cookbook

By: Dimitris Tavlikos

Overview of this book

<p>MonoTouch brings the amazing revenue opportunities of Apple’s billion dollar app store to C# and .NET developers. <br /><br />This cookbook leaves no stone unturned, providing you with practical recipes covering user interfaces, data management, multimedia , web services, and localization, right through to application deployment on the app store.<br /><br />Whatever the area of MonoTouch iOS development you need to know about, you will find a recipe for it in this cookbook. Minimum theory and maximum practical action defines this book. It is jam packed with recipes for interacting with the device hardware, like the GPS, compass and the accelerometer. Recipes for those all important real world issues such as designing the UI with the integrated designer introduced with Xcode 4. It is the essential cookbook for C# and .NET developers wanting to be part of the exciting and lucrative world of iOS development.</p>
Table of Contents (22 chapters)
iOS Development Using MonoTouch Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating files


In this recipe, we will learn how to create files on the filesystem of iOS devices.

Getting ready

Create a new iPhone Single View Application project in MonoDevelop. Name it FileCreationApp . Open the FileCreationAppViewController.xib file, and add a UILabel and a UIButton on its view.

How to do it...

Enter the following code in the FileCreationAppViewController class:

public override void ViewDidLoad(){
  string filePath = Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.Personal), "MyFile.txt");
  using (StreamWriter sw = new StreamWriter (filePath)){
    sw.WriteLine ("Some text in file!");
  }
  this.btnShow.TouchUpInside += delegate {
    using (StreamReader sr = new StreamReader (filePath)){
      this.labelMessage.Text = sr.ReadToEnd ();
    }
  }
};

How it works...

As one can see from this code, we can use standard classes from the System.IO namespace, just like in desktop applications. The first thing we do is to set a path for the file we will save. We...