SlideShare uma empresa Scribd logo
1 de 31
How to code for accelerometer
          and Core Location?
                    Desert Code Camp
                 Phoenix, Arizona 2010
Design Pattern
             1.
Model(delegate) + Controller + View




                         View                            Singleton
                                Include
                                XIB files




            Model                           Controller

Delegates




                                                                     3
Objective-C
          2.
Objective-C

  Is a simple computer language designed to enable sophisticated OO programming.


  Extends the standard ANSI C language by providing syntax for defining classes,
  methods, and properties, as well as other constructs that promote dynamic
  extension of classes.



  Based mostly on Smalltalk (class syntax and design), one of the first object-oriented
  programming languages.



  Includes the traditional object-oriented concepts, such as encapsulation,
  inheritance, and polymorphism.



                                                                                           5
Files


     Extension                           Source Type
.h               Header files. Header files contain class, type, function, and
                 constant declarations.

.m               Source files. This is the typical extension used for source
                 files and can contain both Objective-C and C code.

.mm              Source files. A source file with this extension can contain C+
                 + code in addition to Objective-C and C code.

                 This extension should be used only if you actually refer to C+
                 + classes or features from your Objective-C code.




                                                                                  6
#import

  To include header files in your source code, you can use the standard #include, but….
  Objective-C provides a better way #import. it makes sure that the same file is never
  included more than once.


 	
  #import	
  “MyAppDelegate.h”	
  
 	
  #import	
  “MyViewController.h”	
  

 	
  #import	
  <UIKit/UIKit.h>	
  




                                                                                           7
Class

  The specification of a class in Objective-C requires two distinct pieces: the
  interface (.h files) and the implementation (.m files).

  The interface portion contains the class declaration and defines the instance
  variables and methods associated with the class.
     @interface	
  
 	
  …	
  
 	
  @end	
  



  The implementation portion contains the actual code for the methods of the
  class.
 	
  @implementation	
  
 	
  …	
  
 	
  @end	
  



                                                                                   8
Class
                                             Class name

@interface	
  MyClass	
  :	
  NSObject	
                Parent class
{	
  
           	
  int	
  count;	
  
           	
  id	
  data;	
                                        Instance variables
           	
  NSString*	
  name;	
  
}	
  

-­‐	
  (id)initWithString:(NSString	
  *)aName;	
  
                                                                                methods
+	
  (MyClass	
  *)createMyClassWithString:	
  (NSString	
  *)	
  aName;	
  

@end	
  




                                                                                         9
Class
                                                                    Class name

@implementation	
  MyClass	
  

-­‐	
  (id)initWithString:(NSString	
  *)	
  aName	
  
{	
  
	
  	
  	
  	
  if	
  (self	
  =	
  [super	
  init])	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  count	
  =	
  0;	
  
	
  	
  	
  	
  	
  	
  	
  	
  data	
  =	
  nil;	
                                         methods
	
  	
  	
  	
  	
  	
  	
  	
  name	
  =	
  [aName	
  copy];	
  
	
  	
  	
  	
  	
  	
  	
  	
  return	
  self;	
  
	
  	
  	
  	
  }	
  
}	
  

+	
  (MyClass	
  *)createMyClassWithString:	
  (NSString	
  *)	
  aName	
  
{	
  
	
  	
  	
  	
  return	
  [[[self	
  alloc]	
  initWithString:aName]	
  autorelease];	
  
}	
  

@end	
  


                                                                                                 10
Methods

  A class in Objective-C can declare two types of methods:
  Instance method is a method whose execution is scoped to a particular instance of the
  class. In other words, before you call an instance method, you must first create an
  instance of the class.

  Class methods, by comparison, do not require you to create an instance.



 Method type identifier               One or more signature keywords


 	
  -­‐(void)insertObject:(id)anObject	
  atIndex:(NSUInteger)index;	
  




           Return type                          Parameters with (type) and name
                                                                                           11
Methods

So	
  the	
  declaration	
  of	
  the	
  method	
  insertObject	
  would	
  be:	
  

-­‐(void)insertObject:(id)anObject	
  atIndex:(NSUInteger)index	
  



     Method type identifier, is (-) to instance methods, (+) to class methods.




And	
  the	
  line	
  to	
  call	
  the	
  method	
  would	
  be:	
  

[myArray	
  insertObject:anObj	
  atIndex:0];	
  



                                                                                            12
Properties

  They are simply a shorthand for defining methods (getters and setters) that
  access existing instance variables.



  Properties do not create new instance variables in your class declaration.


  Reduce the amount of redundant code you have to write. Because most
  accessor methods are implemented in similar ways



  You specify the behavior you want using the property declaration and then
  synthesize actual getter and setter methods based on that declaration at
  compile time.

                                                                                 13
Properties

In the interface we have:
{	
  
BOOL	
  flag;	
  
NSString*	
  myObject;	
  
UIView*	
  rootView;	
  
}	
  

@property	
  BOOL	
  flag;	
  
@property	
  (copy)	
  NSString*	
  myObject;	
  //	
  Copy	
  the	
  object	
  during	
  assignement	
  
@property	
  (readonly)	
  UIView*	
  rootView;	
  //	
  Create	
  only	
  a	
  getter	
  method.	
  
  	
         	
              	
          	
               	
  	
  

…	
  

And in the implementation side we have:
@syntetize	
  flag;	
  
@syntetize	
  myObject;	
  
@syntetize	
  rootView;	
  

…	
  
myObject.flag	
  =	
  YES;	
  
CGRect	
  	
  	
  viewFrame	
  =	
  myObject.rootView.frame;	
  



                                                                                                                     14
Properties

Writability

  Readwrite. You can read/write it. This is the default value.
  Readonly. You can only read it.

Setter semantics (mutually exclusive)

  Assign. Specifies that the setter uses simple assignment. This is the default value.
  Retain. Specifies that a pointer should be retained.
  Copy. Specifies that a copy of the object should be used for assignment.

Atomicity (multithreading)

  Nonatomic. Specifies that accessor methods are not atomic.
  The default value is atomic but there is no need to specify it.


                                                                                          15
Protocols and Delegates

  Protocols are not classes themselves. They simply define an interface that other objects
  are responsible for implementing


  A protocol declares methods that can be implemented by any class.


  In iPhone OS, protocols are used frequently to implement delegate objects. A delegate
  object is an object that acts on behalf of, or in coordination with, another object.


  The declaration of a protocol looks similar to that of a class interface, with the exceptions
  that protocols do not have a parent class and they do not define instance variables.


  In the case of many delegate protocols, adopting a protocol is simply a matter of
  implementing the methods defined by that protocol. There are some protocols that
  require you to state explicitly that you support the protocol, and protocols can specify
  both required and optional methods.
                                                                                               16
Example: Fraction

Fraction.h	
                                               Fraction.m	
  

#import	
  <Foundation/NSObject.h>	
  	
                   #import	
  "Fraction.h"	
  	
  
                                                           #import	
  <stdio.h>	
  	
  	
  
@interface	
  Fraction:	
  NSObject	
  {	
  	
  	
  	
  
	
  	
  	
  int	
  numerator;	
  	
  	
  	
  	
  	
  
                                                           @implementation	
  Fraction	
  
	
  	
  	
  int	
  denominator;	
  	
  
	
  }	
  	
  	
  
                                                           @synthesize	
  numerator;	
  
//Properties	
  instead	
  of	
  getters	
  and	
  	
      @synthesize	
  denominator;	
  
//setters	
  
@property	
  (nonatomic)	
  int	
  numerator;	
            //	
  Output	
  Print	
  
@property	
  (nonatomic)	
  int	
  denominator;	
          -­‐(void)	
  print	
  {	
  	
  	
  	
  	
  	
  
                                                           printf("%i/%i",	
  numerator,denominator);	
  	
  
                                                           }	
  	
  	
  
//Output	
  print	
                                        @end	
  	
  
-­‐(void)	
  print;	
  	
  

@end	
  	
  


                                                                                                                17
Example: Fraction

main.m


#import <stdio.h>
#import "Fraction.h"

int main( int argc, const char *argv[] ) {
    Fraction *frac = [[Fraction alloc] init];
    frac.numerator = 1;
    frac.denominator=3;

    printf( "The fraction is: " );
    [frac print];
    printf( "n" );

    [frac release]
    return 0;
}




                                                                18
Strings

  The NSString class provides an object wrapper.
  Supports storing arbitrary-length strings, support for Unicode, printf-style
  formatting utilities, and more.

  Shorthand notation for creating NSString objects from constant values.
  Precede a normal, double-quoted string with the @ symbol.


  NSString*	
  	
  myString	
  =	
  @”Hello	
  Worldn";	
  


  NSString*	
  	
  anotherString	
  =	
  	
  
   	
     	
  	
  	
  	
  	
  	
  [NSString	
  stringWithFormat:@"%d	
  %s",	
  1,	
  @"String”];	
  




                                                                                                         19
Let us code:
Autorotate and Accelerometer
                           3
Accelerometer


Measure of Gravity acceleration:

0g

1g

2.3g




                              21
Reading the Accelerometer

  UIAccelerometer object in UIKit allow you to access to the raw accelerometer
     data directly. This object reports the current accelerometer values.

  To get an instance of this class, call the sharedAccelerometer method of
     UIAccelerometer class.

    The updateInterval property define the reporting interval in seconds.


-­‐(void)viewDidLoad	
  {	
  
	
  	
  UIAccelerometer	
  *accelerometer	
  =	
  [UIAccelerometer	
  sharedAccelerometer];	
  
	
  	
  accelerometer.delegate	
  =	
  self;	
  
	
  	
  accelerometer.updateInterval	
  =	
  	
  1.0/60;	
  
	
  	
  [super	
  viewDidLoad];	
  
}	
  



                                                                                              22
Reading the Accelerometer

  A delegate (UIAccelerometerDelegate) will receive acceleration events.


@interface	
  FooViewController:	
  UIViewController	
  <UIAccelerometerDelegate>	
  



  Use   accelerometer:didAccelerate: method to process accelerometer data.


-­‐(void)accelerometer:(UIAccelerometer	
  *)accelerometer	
  didAccelerate:
                	
  (UIAcceleration	
  *)acceleration	
  {	
  	
  	
  	
  	
  
	
  	
  NSString	
  *s	
  =	
  [[NSString	
  alloc]	
  initWithFormat:@"%f,	
  %f,	
  %f",	
   	
     	
  
                	
  acceleration.x,	
  acceleration.y,	
  acceleration.z];	
  
    	
  accLabel.text	
  =	
  s;	
  
	
  	
  [s	
  release];	
  
}
                                                                                                        23
AutoRotate

The UIViewController class provides the infrastructure needed to rotate your interface and
  adjust the position of views automatically in response to orientation changes.


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation interfaceOrientation {
   return YES;
   //return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


interfaceOrientation values:

  UIInterfaceOrientationPortrait
  UIInterfaceOrientationPortraitUpsideDown
  UIInterfaceOrientationLandscapeLeft
  UIInterfaceOrientationLandscapeRight

                                                                                             24
AutoRotate adjustments




                     25
Core Location
            4
Core Location


The Core Location framework monitors signals coming from cell phone towers
and Wi-Fi hotspots and uses them to triangulate the user's current position.




                                                                               27
Getting the User's Current Location

  Create an instance of CLLocationManager class.
It	
  is	
  necessary	
  to	
  include	
  the	
  CoreLocation.framework	
  

#import	
  <CoreLocation/CoreLocation.h>	
  

@interface	
  FooViewController:	
  UIViewController<CLLocationManagerDelegate>	
  	
  



  To begin receiving notifications, assign a delegate and call the
   startUpdatingLocation method.


-­‐(void)viewDidLoad	
  {	
  
	
  	
  CLLocationManager	
  *locationManager=	
  [[CLLocationManager	
  alloc]	
  init];	
  
	
  	
  [locationManager	
  startUpdatingLocation];locationManager.delegate	
  =	
  self;	
  
	
  	
  locationManager.distanceFilter	
  =	
  kCLDistanceFilterNone;	
  	
  	
  	
  
	
  	
  locationManager.desiredAccuracy	
  =	
  kCLLocationAccuracyBest;	
  
}	
  

                                                                                                28
Using the Core Location

  We need implement this:

-­‐  (void)locationManager:(CLLocationManager	
  *)manager	
  didUpdateToLocation:
  (CLLocation	
  *)newLocation	
  fromLocation:(CLLocation	
  *)oldLocation	
  {	
  

	
  	
  NSString	
  *latitudeString	
  =	
  [[NSString	
  alloc]	
  initWithFormat:@"%g°",	
  
             	
         	
       	
            	
  newLocation.coordinate.latitude];	
  

	
  	
  latitudeLabel.text	
  =	
  latitudeString;	
  
	
  	
  [latitudeString	
  release];	
  
    	
  NSString	
  *longitudeString	
  =	
  [[NSString	
  alloc]	
  initWithFormat:@"%g°",	
  	
  
    	
       	
         	
  newLocation.coordinate.longitude];	
  

	
  	
  longitudeLabel.text	
  =	
  longitudeString;	
  
	
  	
  [longitudeString	
  release];	
  
}	
  


                                                                                                      29
Exercises and
  References
            5
iPhone Dev Center




http://developer.apple.com/iphone




                                           31

Mais conteúdo relacionado

Mais procurados

Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
Aspect oriented programming_with_spring
Aspect oriented programming_with_springAspect oriented programming_with_spring
Aspect oriented programming_with_springGuo Albert
 
Java session05
Java session05Java session05
Java session05Niit Care
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsSubhransu Behera
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming Raghunath A
 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2NAILBITER
 
Getting started with the JNI
Getting started with the JNIGetting started with the JNI
Getting started with the JNIKirill Kounik
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in PythonSujith Kumar
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONLalitkumar_98
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - CJussi Pohjolainen
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
Java Programming For Android
Java Programming For AndroidJava Programming For Android
Java Programming For AndroidTechiNerd
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in PythonSujith Kumar
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 

Mais procurados (20)

Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Aspect oriented programming_with_spring
Aspect oriented programming_with_springAspect oriented programming_with_spring
Aspect oriented programming_with_spring
 
Java session05
Java session05Java session05
Java session05
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
 
Objective c
Objective cObjective c
Objective c
 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2
 
Getting started with the JNI
Getting started with the JNIGetting started with the JNI
Getting started with the JNI
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Java Programming For Android
Java Programming For AndroidJava Programming For Android
Java Programming For Android
 
OOP C++
OOP C++OOP C++
OOP C++
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
About Python
About PythonAbout Python
About Python
 

Destaque

Dynamic Object Flow Analysis (PhD Defense)
Dynamic Object Flow Analysis (PhD Defense)Dynamic Object Flow Analysis (PhD Defense)
Dynamic Object Flow Analysis (PhD Defense)lienhard
 
The San Diego LGBT Community Center
The San Diego LGBT Community CenterThe San Diego LGBT Community Center
The San Diego LGBT Community CenterSociologistTina
 
Wolko1- Afiches de cine
Wolko1- Afiches de cineWolko1- Afiches de cine
Wolko1- Afiches de cineguest0b0bd35
 
Tracking Objects To Detect Feature Dependencies
Tracking Objects To Detect Feature DependenciesTracking Objects To Detect Feature Dependencies
Tracking Objects To Detect Feature Dependencieslienhard
 
Financial planning introduction fall 2010
Financial planning introduction fall 2010Financial planning introduction fall 2010
Financial planning introduction fall 2010dphil002
 
簡介創用CC授權
簡介創用CC授權簡介創用CC授權
簡介創用CC授權Chou Emily
 
Urban Cottage + IceMilk Aprons
Urban Cottage + IceMilk ApronsUrban Cottage + IceMilk Aprons
Urban Cottage + IceMilk ApronsIceMilk Aprons
 
Week5-Group-J
Week5-Group-JWeek5-Group-J
Week5-Group-Js1160114
 
Amazon resource for bioinformatics
Amazon resource for bioinformaticsAmazon resource for bioinformatics
Amazon resource for bioinformaticsBrad Chapman
 
漫谈php和java
漫谈php和java漫谈php和java
漫谈php和javasulong
 
Phenomenal Oct 8, 2009
Phenomenal Oct 8, 2009Phenomenal Oct 8, 2009
Phenomenal Oct 8, 2009etalcomendras
 
Paul Harris Fellow Clubs En
Paul Harris Fellow Clubs EnPaul Harris Fellow Clubs En
Paul Harris Fellow Clubs Enetalcomendras
 

Destaque (20)

Barya Perception
Barya PerceptionBarya Perception
Barya Perception
 
201506 CSE340 Lecture 10
201506 CSE340 Lecture 10201506 CSE340 Lecture 10
201506 CSE340 Lecture 10
 
Thehub bocconi law
Thehub   bocconi lawThehub   bocconi law
Thehub bocconi law
 
Dynamic Object Flow Analysis (PhD Defense)
Dynamic Object Flow Analysis (PhD Defense)Dynamic Object Flow Analysis (PhD Defense)
Dynamic Object Flow Analysis (PhD Defense)
 
The San Diego LGBT Community Center
The San Diego LGBT Community CenterThe San Diego LGBT Community Center
The San Diego LGBT Community Center
 
Wolko1- Afiches de cine
Wolko1- Afiches de cineWolko1- Afiches de cine
Wolko1- Afiches de cine
 
Tracking Objects To Detect Feature Dependencies
Tracking Objects To Detect Feature DependenciesTracking Objects To Detect Feature Dependencies
Tracking Objects To Detect Feature Dependencies
 
Financial planning introduction fall 2010
Financial planning introduction fall 2010Financial planning introduction fall 2010
Financial planning introduction fall 2010
 
簡介創用CC授權
簡介創用CC授權簡介創用CC授權
簡介創用CC授權
 
Urban Cottage + IceMilk Aprons
Urban Cottage + IceMilk ApronsUrban Cottage + IceMilk Aprons
Urban Cottage + IceMilk Aprons
 
Week5-Group-J
Week5-Group-JWeek5-Group-J
Week5-Group-J
 
Amazon resource for bioinformatics
Amazon resource for bioinformaticsAmazon resource for bioinformatics
Amazon resource for bioinformatics
 
200905 - Sociable machines
200905 - Sociable machines200905 - Sociable machines
200905 - Sociable machines
 
漫谈php和java
漫谈php和java漫谈php和java
漫谈php和java
 
Phenomenal Oct 8, 2009
Phenomenal Oct 8, 2009Phenomenal Oct 8, 2009
Phenomenal Oct 8, 2009
 
Uip Romain
Uip RomainUip Romain
Uip Romain
 
201101 mLearning
201101 mLearning201101 mLearning
201101 mLearning
 
Valvuloplastie
ValvuloplastieValvuloplastie
Valvuloplastie
 
Paul Harris Fellow Clubs En
Paul Harris Fellow Clubs EnPaul Harris Fellow Clubs En
Paul Harris Fellow Clubs En
 
New Venture Presentatie
New Venture PresentatieNew Venture Presentatie
New Venture Presentatie
 

Semelhante a 201005 accelerometer and core Location

Semelhante a 201005 accelerometer and core Location (20)

Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
iOS Application Development
iOS Application DevelopmentiOS Application Development
iOS Application Development
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Intro to iOS Development • Made by Many
Intro to iOS Development • Made by ManyIntro to iOS Development • Made by Many
Intro to iOS Development • Made by Many
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
My c++
My c++My c++
My c++
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
 
iOS development introduction
iOS development introduction iOS development introduction
iOS development introduction
 
C#ppt
C#pptC#ppt
C#ppt
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Unit 5.ppt
Unit 5.pptUnit 5.ppt
Unit 5.ppt
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
01 objective-c session 1
01  objective-c session 101  objective-c session 1
01 objective-c session 1
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Lecture 13, 14 & 15 c# cmd let programming and scripting
Lecture 13, 14 & 15   c# cmd let programming and scriptingLecture 13, 14 & 15   c# cmd let programming and scripting
Lecture 13, 14 & 15 c# cmd let programming and scripting
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
C++
C++C++
C++
 

Mais de Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Último

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 

Último (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

201005 accelerometer and core Location

  • 1. How to code for accelerometer and Core Location? Desert Code Camp Phoenix, Arizona 2010
  • 3. Model(delegate) + Controller + View View Singleton Include XIB files Model Controller Delegates 3
  • 5. Objective-C   Is a simple computer language designed to enable sophisticated OO programming.   Extends the standard ANSI C language by providing syntax for defining classes, methods, and properties, as well as other constructs that promote dynamic extension of classes.   Based mostly on Smalltalk (class syntax and design), one of the first object-oriented programming languages.   Includes the traditional object-oriented concepts, such as encapsulation, inheritance, and polymorphism. 5
  • 6. Files Extension Source Type .h Header files. Header files contain class, type, function, and constant declarations. .m Source files. This is the typical extension used for source files and can contain both Objective-C and C code. .mm Source files. A source file with this extension can contain C+ + code in addition to Objective-C and C code. This extension should be used only if you actually refer to C+ + classes or features from your Objective-C code. 6
  • 7. #import   To include header files in your source code, you can use the standard #include, but….   Objective-C provides a better way #import. it makes sure that the same file is never included more than once.  #import  “MyAppDelegate.h”    #import  “MyViewController.h”    #import  <UIKit/UIKit.h>   7
  • 8. Class   The specification of a class in Objective-C requires two distinct pieces: the interface (.h files) and the implementation (.m files).   The interface portion contains the class declaration and defines the instance variables and methods associated with the class. @interface    …    @end     The implementation portion contains the actual code for the methods of the class.  @implementation    …    @end   8
  • 9. Class Class name @interface  MyClass  :  NSObject   Parent class {    int  count;    id  data;   Instance variables  NSString*  name;   }   -­‐  (id)initWithString:(NSString  *)aName;   methods +  (MyClass  *)createMyClassWithString:  (NSString  *)  aName;   @end   9
  • 10. Class Class name @implementation  MyClass   -­‐  (id)initWithString:(NSString  *)  aName   {          if  (self  =  [super  init])  {                  count  =  0;                  data  =  nil;   methods                name  =  [aName  copy];                  return  self;          }   }   +  (MyClass  *)createMyClassWithString:  (NSString  *)  aName   {          return  [[[self  alloc]  initWithString:aName]  autorelease];   }   @end   10
  • 11. Methods   A class in Objective-C can declare two types of methods:   Instance method is a method whose execution is scoped to a particular instance of the class. In other words, before you call an instance method, you must first create an instance of the class.   Class methods, by comparison, do not require you to create an instance. Method type identifier One or more signature keywords  -­‐(void)insertObject:(id)anObject  atIndex:(NSUInteger)index;   Return type Parameters with (type) and name 11
  • 12. Methods So  the  declaration  of  the  method  insertObject  would  be:   -­‐(void)insertObject:(id)anObject  atIndex:(NSUInteger)index   Method type identifier, is (-) to instance methods, (+) to class methods. And  the  line  to  call  the  method  would  be:   [myArray  insertObject:anObj  atIndex:0];   12
  • 13. Properties   They are simply a shorthand for defining methods (getters and setters) that access existing instance variables.   Properties do not create new instance variables in your class declaration.   Reduce the amount of redundant code you have to write. Because most accessor methods are implemented in similar ways   You specify the behavior you want using the property declaration and then synthesize actual getter and setter methods based on that declaration at compile time. 13
  • 14. Properties In the interface we have: {   BOOL  flag;   NSString*  myObject;   UIView*  rootView;   }   @property  BOOL  flag;   @property  (copy)  NSString*  myObject;  //  Copy  the  object  during  assignement   @property  (readonly)  UIView*  rootView;  //  Create  only  a  getter  method.               …   And in the implementation side we have: @syntetize  flag;   @syntetize  myObject;   @syntetize  rootView;   …   myObject.flag  =  YES;   CGRect      viewFrame  =  myObject.rootView.frame;   14
  • 15. Properties Writability   Readwrite. You can read/write it. This is the default value.   Readonly. You can only read it. Setter semantics (mutually exclusive)   Assign. Specifies that the setter uses simple assignment. This is the default value.   Retain. Specifies that a pointer should be retained.   Copy. Specifies that a copy of the object should be used for assignment. Atomicity (multithreading)   Nonatomic. Specifies that accessor methods are not atomic.   The default value is atomic but there is no need to specify it. 15
  • 16. Protocols and Delegates   Protocols are not classes themselves. They simply define an interface that other objects are responsible for implementing   A protocol declares methods that can be implemented by any class.   In iPhone OS, protocols are used frequently to implement delegate objects. A delegate object is an object that acts on behalf of, or in coordination with, another object.   The declaration of a protocol looks similar to that of a class interface, with the exceptions that protocols do not have a parent class and they do not define instance variables.   In the case of many delegate protocols, adopting a protocol is simply a matter of implementing the methods defined by that protocol. There are some protocols that require you to state explicitly that you support the protocol, and protocols can specify both required and optional methods. 16
  • 17. Example: Fraction Fraction.h   Fraction.m   #import  <Foundation/NSObject.h>     #import  "Fraction.h"     #import  <stdio.h>       @interface  Fraction:  NSObject  {              int  numerator;             @implementation  Fraction        int  denominator;      }       @synthesize  numerator;   //Properties  instead  of  getters  and     @synthesize  denominator;   //setters   @property  (nonatomic)  int  numerator;   //  Output  Print   @property  (nonatomic)  int  denominator;   -­‐(void)  print  {             printf("%i/%i",  numerator,denominator);     }       //Output  print   @end     -­‐(void)  print;     @end     17
  • 18. Example: Fraction main.m #import <stdio.h> #import "Fraction.h" int main( int argc, const char *argv[] ) { Fraction *frac = [[Fraction alloc] init]; frac.numerator = 1; frac.denominator=3; printf( "The fraction is: " ); [frac print]; printf( "n" ); [frac release] return 0; } 18
  • 19. Strings   The NSString class provides an object wrapper.   Supports storing arbitrary-length strings, support for Unicode, printf-style formatting utilities, and more.   Shorthand notation for creating NSString objects from constant values. Precede a normal, double-quoted string with the @ symbol. NSString*    myString  =  @”Hello  Worldn";   NSString*    anotherString  =                  [NSString  stringWithFormat:@"%d  %s",  1,  @"String”];   19
  • 20. Let us code: Autorotate and Accelerometer 3
  • 21. Accelerometer Measure of Gravity acceleration: 0g 1g 2.3g 21
  • 22. Reading the Accelerometer   UIAccelerometer object in UIKit allow you to access to the raw accelerometer data directly. This object reports the current accelerometer values.   To get an instance of this class, call the sharedAccelerometer method of UIAccelerometer class.   The updateInterval property define the reporting interval in seconds. -­‐(void)viewDidLoad  {      UIAccelerometer  *accelerometer  =  [UIAccelerometer  sharedAccelerometer];      accelerometer.delegate  =  self;      accelerometer.updateInterval  =    1.0/60;      [super  viewDidLoad];   }   22
  • 23. Reading the Accelerometer   A delegate (UIAccelerometerDelegate) will receive acceleration events. @interface  FooViewController:  UIViewController  <UIAccelerometerDelegate>     Use accelerometer:didAccelerate: method to process accelerometer data. -­‐(void)accelerometer:(UIAccelerometer  *)accelerometer  didAccelerate:  (UIAcceleration  *)acceleration  {              NSString  *s  =  [[NSString  alloc]  initWithFormat:@"%f,  %f,  %f",        acceleration.x,  acceleration.y,  acceleration.z];    accLabel.text  =  s;      [s  release];   } 23
  • 24. AutoRotate The UIViewController class provides the infrastructure needed to rotate your interface and adjust the position of views automatically in response to orientation changes. -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation interfaceOrientation { return YES; //return (interfaceOrientation == UIInterfaceOrientationPortrait); } interfaceOrientation values:   UIInterfaceOrientationPortrait   UIInterfaceOrientationPortraitUpsideDown   UIInterfaceOrientationLandscapeLeft   UIInterfaceOrientationLandscapeRight 24
  • 27. Core Location The Core Location framework monitors signals coming from cell phone towers and Wi-Fi hotspots and uses them to triangulate the user's current position. 27
  • 28. Getting the User's Current Location   Create an instance of CLLocationManager class. It  is  necessary  to  include  the  CoreLocation.framework   #import  <CoreLocation/CoreLocation.h>   @interface  FooViewController:  UIViewController<CLLocationManagerDelegate>       To begin receiving notifications, assign a delegate and call the startUpdatingLocation method. -­‐(void)viewDidLoad  {      CLLocationManager  *locationManager=  [[CLLocationManager  alloc]  init];      [locationManager  startUpdatingLocation];locationManager.delegate  =  self;      locationManager.distanceFilter  =  kCLDistanceFilterNone;            locationManager.desiredAccuracy  =  kCLLocationAccuracyBest;   }   28
  • 29. Using the Core Location   We need implement this: -­‐  (void)locationManager:(CLLocationManager  *)manager  didUpdateToLocation: (CLLocation  *)newLocation  fromLocation:(CLLocation  *)oldLocation  {      NSString  *latitudeString  =  [[NSString  alloc]  initWithFormat:@"%g°",          newLocation.coordinate.latitude];      latitudeLabel.text  =  latitudeString;      [latitudeString  release];    NSString  *longitudeString  =  [[NSString  alloc]  initWithFormat:@"%g°",          newLocation.coordinate.longitude];      longitudeLabel.text  =  longitudeString;      [longitudeString  release];   }   29
  • 30. Exercises and References 5