Praktikum Entwicklung von Mediensystemen mit Wintersemester 2013/2014 Christian Weiß, Dr. Alexander De Luca
Today Organization Introduction to ios programming Hello World Assignment 1 2
Organization 6 ECTS Bachelor: Vertiefendes Thema Master: Gruppenpraktikum Wednesday 12-14, Geschwister-Scholl-Platz 1 M207 Check your emails (cip / campus) http://www.medien.ifi.lmu.de/lehre/ws1314/pem/ 3
Roadmap October, November: weekly lectures and individual assignments November, December, January: app development in teams, 4 milestone presentations January: final presentation and closing meetings for each team 4
ios Mobile operating system by Apple for iphone, ipad and ipod Touch Based on Unix, derived from OS X Latest release: ios 7 (September 2013) High market share, high user engagement, high willingness to pay for apps. Overall smartphone / tablet market is huge and still growing, and many PEM skills also apply to Android development. 5
Layers of ios Cocoa Touch Multi-touch, Web View, Map Kit, Camera, Image Picker... Media Core Audio, PDF, Core Animation, Quartz 2D, OpenGL... Core Services Core Location, Preferences, Address Book, Preferences... Core OS File System, Kernel, Power Management, Security... 6
User input GUI controls: buttons, sliders, switches etc. Multi-touch gestures: tap, pinch, rotate, swipe, pan, long press Accelerometer: shaking, rotating 7
ios Development 8
Development Environment XCode 9
XCode Source editor: code completion, syntax highlighting, contextsensitive information Interface builder: UI elements library and inspector, split editor to connect UI with code, Storyboards Compiler: C, C++, Objective-C ios Simulator: run and test apps on a Mac More: refactoring, version control, debugging, analysis (https://developer.apple.com/technologies/tools/) 10
XCode Build and run Device and simulator selection Source editor Show/hide sidebars File navigator Utilities sidebar (API info) 11
Contents of an XCode project Source code files (.h and.m) User interface files (.storyboard and.xib) Libraries (.framework) Resources, e.g. images (.png) App configuration file (Info.plist) 12
Objective-C Language for programming ios and Mac apps, also used by Apple to create much of OS X, ios, APIs Strict superset of C, adds syntax for classes, methods, etc. Object-orientated Short introduction: https://developer.apple.com/library/mac/referencelibrary/gettingstarted/ Learning_Objective-C_A_Primer/index.html Detailed introduction: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ ProgrammingWithObjectiveC/Introduction/Introduction.html 13
Elements of Objective-C Java MyClass.java Methods and method calls Attributes, setters, getters Objective-C Header.h Implementation.m Methods and messages Properties, instance variables * Constructor Interface Garbage Collection Initializer Protocol Automatic Reference Counting (ARC) * * * * Different terminology, but for us very similar to writing Java code 14
Methods Definition (in.h): - (void) dosomething; - (void) dosomethingwitha: (NSString *) a andb: (NSString *) b; Implementation (in.m): - (void) dosomething { // do something } - (void) dosomethingwitha: (NSString *) a andb: (NSString *) b { // do something with a and b } Method call ( message ) (in.m): [self dosomething]; NSString* a = @"a"; NSString* b = @"b"; [self dosomethingwitha:a andb:b]; 15
Properties Auto-creation instance variable, getter and setter The getter has the name of the property ( myproperty ) The name of the setter is get + property name ( getmyproperty ) Definition (in.h): @property(strong, nonatomic) NSString *name; Using getters (in.m): NSString *labeltext = self.name; labeltext = [self name]; Using setters (in.m): [self setname:@"max"]; self.name = @"Max"; Using the instance variable (in.m): _name = @"Max"; strong/weak: refers to ownership. Always use strong except for properties that point to a parent. nonatomic/atomic: use nonatomic to avoid multi-threading issues. self.name: this syntax does NOT access the variable itself. It s a getter/setter, just like the other syntax. _name: Use this instance variable in custom setters/getters and in init-methods only. In any other case, use the getter/setter. 16
Instance Variables ( ivars ) Like private/protected attributes in Java Definition (in.h): NSString* _name; Use (in.m): _name = @"Max"; labeltext = _name; You don t have to use the underscore ( _ ), but it s good practice. Otherwise you accidentally mix up ivars and properties (see next slide). Most of the time it is better to use properties instead 17
Object Initialization Object: MyClass *myobject = [[MyClass alloc] init]; Object with parameter: MyClass *myobject = [[MyClass alloc] initwithparameter: parameter]; String: NSString *hello = @"Hello"; NSString *helloworld = [NSString stringwithformat:@"%@ World", hello]; Array: NSArray *colors = @[@"Green", @"Red", @"Yellow"]; NSMutableArray *mutablecolors = [@[@"Green", @"Red", @"Yellow"] mutablecopy]; If your app doesn t work properly, make sure your objects aren t nil. THERE ARE NO NULL POINTER EXCEPTIONS - Less crashes, more confusion. 18
Objective-C - Example Student.h Student.m #import <Foundation/Foundation.h> #import "Student.h" @interface Student : NSObject @implementation Student @end @end 19
Objective-C - Example Student.h Student.m #import <Foundation/Foundation.h> #import "Student.h" @interface student() @end @interface Student : NSObject @implementation Student @end @end 20
Objective-C - Example Student.h Student.m #import <Foundation/Foundation.h> #import "Student.h" @interface student() @end @interface Student : NSObject @implementation Student @property (strong, nonatomic) NSString *fullname; @property (nonatomic) NSUInteger number; @end @end 21
Objective-C - Example Student.h Student.m #import <Foundation/Foundation.h> #import "Student.h" @interface student() @end @interface Student : NSObject @property (strong, nonatomic) NSString *fullname; @property (nonatomic) NSUInteger number; @implementation Student - (void) setfullname:(nsstring *)fullname { NSLog(@"%@", fullname); _fullname = fullname; } @end @end 22
Hello World New XCode Project: Single View Application In the storyboard, drag a text label and a switch onto the screen 23
Hello World Open the assistant editor and ctrl-drag the text label into ViewController.h. Enter a name and click Connect. You now have access to the UI element in your code. Again, ctrl-drag the switch into the code. This time, select Action instead of Outlet. Change the type from id to UISwitch. Enter a name and click Connect. You now have a listener method that is called by the OS when the user changes the value of our switch. 24
Hello World Close the assistant editor and go to ViewController.m. Complete the IBAction method: - (IBAction)switchChanged:(UISwitch *)sender { NSLog(@"switch changed"); if (sender.on) { self.mylabel.text = @"HelloWorld"; } else { self.mylabel.text = @""; } } Open the debug area and run the code. 25
UIViewController One of the most important classes in ios programming You have to subclass UIViewController when creating a new screen Provides methods for managing the view hierarchy throughout its life cycle and for reacting to events (also great for debugging), e.g. viewdidload: viewwillappear: viewdidappear: viewwilldisappear: viewdiddisappear: - (void)willrotatetointerfaceorientation:(uiinterfaceorientation)tointerfaceorientation duration:(nstimeinterval)duration; For more see http://developer.apple.com/library/ios/#documentation/uikit/reference/ UIViewController_Class/Reference/Reference.html 26
App Delegate Every app must have an App Delegate. Provides methods for managing the app throughout its life cycle (also great for debugging), e.g. - application:didfinishlaunchingwithoptions: applicationdidbecomeactive: applicationdidenterbackground: applicationwillenterforeground: applicationwillterminate: For more see: http://developer.apple.com/library/ios/#documentation/uikit/reference/ UIApplicationDelegate_Protocol/Reference/Reference.html There are lots of protocols (often named Delegate), e.g. for managing the keyboard, table views, date pickers. 27
Resources Stanford CS 193P iphone Application Development: https://itunes.apple.com/us/course/coding-together-developing/ id593208016 Official documentation: https://developer.apple.com/library/ios Tutorials: http://www.raywenderlich.com/tutorials Solutions to specific problems: Google + Stackoverflow Book: ios Programming: The Big Nerd Ranch Guide by Joe Conway and Aaron Hillegass Developer videos: https://developer.apple.com/videos/ 28
Assignment 1 Individual assignment Get to know XCode and Objective-C Due next Wednesday 10:00, upload to Uniworx Questions? 29