PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 14982 Views | Rate 70.01
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 18372 Views | Rate 59.07
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4862 Views | Rate 55.89
NMEA GPS Library for AVR Microcontrollers
Programmatically Injecting Events on Android - Part 2 | 5100 Views | Rate 45.13
Programmatically Injecting Events on Android - Part 2
Building a robot – Part 2 | 4666 Views | Rate 44.44
Building a robot – Part 2
Simple Switched power Supplies | 16261 Views | Rate 42.02
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 11410 Views | Rate 36.45
Capacitor Discharge Microspot Welder / Cutter
Atmega8 and Nokia 5110 LCD  | 1571 Views | Rate 34.91
Atmega8 and Nokia 5110 LCD

 
  

Developing for Apple iOS


By Radu Motisan Posted on July 16th, 2012 , 3475 Views (Rate 11.17)



  




Introduction

Years after the successful launch of Apple's mobile platform , I accidentally got intersected with this technology. Here are some of my first impressions, and a short tutorial to get you started.

You will need:
- a Mac mini, running Mac OS . I'm currently using a 2011 model, that can boot both Win7 and MacOS. You can see it here.
- a iOS developer account. It is not free, and it costs 99USD/year. You can get one here.

Logging in to the developer account, will give you access to the development tools. Currently available is Xcode 4.3.2 . It is a big download, close to 2GB, but it as a package with everything you need: IDE, SDK and Compiler. This version of Xcode brings the iOS SDK 5.1.
- a test device, unless you are planning to use the Simulator. The test device has many advantages, like speed, and full suite of features working in the real environment like Camera, Bluetooth , Phone, etc , and on top of all, it shows how your software performs on the target it was intended for. It is an optional recommended expense.

Installing the tools

Setting up the tools is straight forward. You just double click the Xcode .dmg file, downloaded from your Developer Account, on Apple's website, than drag the Xcode app to the Applications folder. Unlike the Windows world, this process is considered "Installation" on Mac OS.

Select "Create a new Xcode project", and start by using the built in wizard for your first application. Compile it and have it run on the Simulator.
To run it on an actual device, you'll need to configure your certificates and a provisioning file for your mobile device. There are many good tutorials covering that topic, if you need help.

Hello world application

I want to keep it as simple as possible, so I select "Single View application".

Pressing NEXT asks for some additional details, like Name, Package (company info) etc. Here is what I've used:

Click NEXT, choose where to save your project, and click Create. Your project template is ready, and you can even run it already!

Application architecture

Coming from the Windows Mobile / Android world, the first step was to establish a few logical links to my previous knowledge.
Looking at the left side of Xcode interface, also called Navigator, you can see the AppDelegate.m and .h and ViewController.m , .h and .xib . The AppDelegate is the entry point for this project, but for this simple Hello World app there is not much to do there. The other three files are responsible for creating your application's "Single view".
The .h is a Header file for declarations, the .m is the implementation file and the .xib is an visual interface builder file. You can use the IDE to add various controls such as buttons , lists or textfields.

Listviews in iOS

ListViews are an often used data organizer or display control. I will show you how to add a list view to the Hello World project we created above.

Among the available controls, accessible by pressing the CUBE symbol in the right part of Xcode's interface (left picture), there is a predefined Table View control. We can use that to create a list view: drag and drop it to the center of the screen, where you see the virtual iOS device. The magnetic placer will adjust the size automatically!





To populate this list, we'll need to write some code!
step 1:
Open HWViewController.h . You see the following:

  1.  
  2. #import <UIKit/UIKit.h>
  3.  
  4. @interface HWViewController : UIViewController
  5.  
  6. @end
  7.  

Change it to:

  1.  
  2. #import <UIKit/UIKit.h>
  3.  
  4. @interface HWViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
  5. // listview attached content array
  6. NSMutableArray *arrayItems;
  7. }
  8. @property (nonatomic, retain) NSMutableArray *arrayItems;
  9. @property (retain, nonatomic) IBOutlet UITableView *myTableView;
  10. @end
  11.  

step 2:
Open HWViewController.m . Under

  1. @implementation HWViewControlle

add the following

  1.  
  2. @synthesize arrayItems, myTableView;
  3.  
  4. // build one listview cell
  5. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  6. static NSString *CellIdentifier = @"Cell";
  7. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  8. if (cell == nil) {
  9. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  10. }
  11.  
  12.  
  13. cell.textLabel.text = [arrayItems objectAtIndex:indexPath.row];
  14.  
  15. return cell;
  16. }
  17.  
  18. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  19. return [arrayItems count];
  20. }
  21.  
  22. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  23. NSString *item = (NSString *)[arrayItems objectAtIndex:indexPath.row];
  24. UIAlertView *Alert = [[UIAlertView alloc]
  25. initWithTitle:@
  26. "Hello!" message:item delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  27. [Alert show];
  28. }
  29.  
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33. // Do any additional setup after loading the view, typically from a nib.
  34. NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"Radu Motisan",@"Pocketmagic.net", nil];
  35. self.arrayItems = array;
  36. }
  37.  

step 3:
Open HWViewController_iPhone.xib, and click to select the list view. To the right, in the Utilities panel, press the "Connections Inspector" button (last arrow symbol). Under outlets, you'll see "dataSource" and "delegate".

Hold down the Control key, click on the circle at right of "dataSource" , and drag the line over "File's Owner". Do the same for "delegate":

step 4:
Compile and RUN the program. You'll get a list view with two lines, clicking any item shows a message box, with the item content:

Get the sample code here:

HelloWorld






  

More on PocketMagic:

Repairing a Victoreen CDV-700 6B Dosimeter  | 170 Views | Rate 24.29
Repairing a Victoreen CDV-700 6B Dosimeter
How to set the AVR Fusebits | 1742 Views | Rate 23.86
How to set the AVR Fusebits
Developing for Blackberry 10 | 94 Views | Rate 23.5
Developing for Blackberry 10
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 2086 Views | Rate 22.92
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164
Dual H-Bridge for controlling two motors | 1236 Views | Rate 21.68
Dual H-Bridge for controlling two motors
USBAsp -  AVR USB Programmer  | 8162 Views | Rate 21.15
USBAsp - AVR USB Programmer

One Response to “Developing for Apple iOS”

  1. 1
    Bluetooth and iOS – Use Bluetooth in your iPhone apps « PocketMagic:

    [...]   « Developing for Apple iOS [...]

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string HH0uZu to the field below: