Objective C Programming Extra Notes

hammerhappysinnSoftware and s/w Development

Nov 9, 2013 (3 years and 7 months ago)

86 views

Objective C Programming

Extra Notes

Copyright © 2011 Four Directions Limited

All rights reserved





Structure Of Xcode Template

- When create a project using
Sample project, say, Navigation
Based Project. We will see following
hierarchy of folders in the
Navigator View of Xcode.



- test-info.plist can be used to setup
meta-data of the application.



- main.m is where the application
will look at when the started.



- Frameworks contain all library
that we will use when programming





Info.Plist

- Localization native development region: The language specified by this
value is used as the default language if a resource cannot be located
for the user’s preferred region or language.



- CFBundleDisplayName (String - iOS, Mac OS X) specifies the display
name of the bundle.

Info.Plist

- Icon File: identifies the file containing the icon for the bundle



- Bundle Identifier: identifies the type of the bundle.



- Bundle Name: The name of application which will show on iphone home page.



- Bundle Display Name: Localizable version of Bundle Name. Will overwrite
bundle name if specified.



- Main nib file base name: The main xib which will be automatically loaded when
an application is launched.



- Bundle version: The version display on App Store. Need to increase this value
when submit update of application. The first integer represents major revisions to
the application, such as revisions that implement new features or major changes.
The second integer denotes revisions that implement less prominent features. The
third integer represents maintenance releases.





main.m

int
main(
int
argc,
char
*argv[])!
{


NSAutoreleasePool
*pool = [[
NSAutoreleasePool

alloc
]
init
];!

int
retVal =
UIApplicationMain
(argc, argv,
nil
,
nil
);!
[pool
release
];!

return
retVal;!
}



- int main(int argv, char**argv) is the C standard application entry point - that is
to say your program will always start in main.



- From IOS Documentation, int UIApplicationMain (int argc, char *argv[],
NSString
*principalClassName,
NSString
*delegateClassName) means following.

This function instantiates the application object from the principal class and and
instantiates the delegate (if any) from the given class and sets the delegate for
the application. It also sets up the main event loop, including the application’s
run loop, and begins processing events. If the application’s
Info.plist
file
specifies a main nib file to be loaded, by including the
NSMainNibFile
key and a
valid nib file name for the value, this function loads that nib fil
e.






import and class

- When Class A import Class B, Class A will know existence of Class B. It will be able to
create object of class B and call related functions.



- Recursive import may result compile error. Like following one.

import “ClassA.h”

@interface
ClassB:
NSObject
{!
ClassA a;

}

end



import “ClassB.h”

@interface
ClassA:
NSObject
{!

ClassB
b;!
}

end



- To solve recursive import, class take the place to let ClassB know there is a
Definition of ClassA.

@class

ClassA
;!
@interface
ClassB:
NSObject
{!

ClassA
a;!
}

end



import “ClassB.h”

@interface
ClassA:
NSObject
{!

ClassB
b;!
}

end

Class Function

- The function
– (
void
)addObject:(
id
)anObject
of NSMutableArray is an
instance functions. Which we need to call it using an object
(Instance) of NSMutableArray.



NSMutableArray
* array  [[
NSMutableArray

alloc
]
init
];

[array
addObject
:
"String"
];



- The function

 (
id
)array
of NSMutableArray is an class function.
Which we can use class to call it.

NSMutableArray
* array  [
NSMutableArray
array];



- Actually, implementation of
 (id)array
in NSMutableArray is as
following.

 (
id
)array{

"
return
[[[
self

alloc
]
init
]
autorelease
];!
}

!
!
Struct

- Used to store variable. We call it plain old data. Following are
two struct we will use in Objective C.

typedef

struct
{!

CGFloat
x;!

CGFloat
y;!
}CGPoint;!
!
typedef

struct
{!

CLLocationDegrees
latitude;!

CLLocationDegrees
longitude;!
} CLLocationCoordinate2D;!


- We can create an struct, using it and change the data inside by
following code.


CGPoint
pointA;!
pointA.
x
=
10
;!
pointB.
y
=
20
;!
Singleton Pattern

- Singleton is a design pattern restricting the instantiation of a class
to one object.

static

LoadingView
* loadingView =
nil
;!
!
@implementation
LoadingView!
!
+ (
LoadingView
*) sharedLoadingView{!
"
@synchronized
(
loadingView
){!
""
if
(
loadingView
==
nil
){!
"""
loadingView
= [[
LoadingView

alloc
]
initWithFrame
:
[
UIScreen

mainScreen
].
bounds
];!
""
}!
"
}!
"
return

loadingView
;!
}!
!
!
- Singleton can be access globally using following code once you
have import the header.


[[
LoadingView

sharedLoadingView
]
attachToView
:
window
];!
How to load a view from xib

-
We can create an ViewController from xib and extract view out
of it. After that, we can add the view to other other UIView and
release the ViewController.

!
"
UIViewController
* arOverlayViewController = [[
UIViewController

alloc
]
initWithNibName
:
@"AROverlayViewController"

bundle
:
nil
];!
"
arOverlayView
= (
AROverlayView
*)arOverlayViewController.
view
;!

arViewController
.
cameraOverlayView
=
arOverlayViewController.
view
;!
[arOverlayViewController
release
];!
!
"
!
AR Browser

Copyright © 2011 Four Directions Limited

All rights reserved





Introduction

12

Augmented reality (AR) is a term for a live direct or an indirect view of a physical, real-world
environment whose elements are  augmented by computer-generated sensory input, such as sound or
graphics.

A facial recognition system is a computer application for automatically identifying
or verifying a person from a digital image or a video frame from a video source.













Facial Recognition



Gesture recognition is a topic in computer science and language technology with the goal of
interpreting human gestures via mathematical algorithm



Gesture Recognition

Image recognition recognize the image with certain patten and output
an matrix represent the orientation of image.

Image Recognition

Technology In AR Browser
- Camera: Camera support overlay is needed in order to draw graphic (overlay) to interact with
Real World.



- GPS: GPS input to get the location information about user.



- Compass: Compass to get the direction user facing.

Class Used

Copyright © 2011 Four Directions Limited

All rights reserved





UIDevice

- The
UIDevice
class provides a singleton instance representing the
current device. e.g.
[
UIDevice

currentDevice
].!


- From this instance you can obtain information about the device such
as unique ID, assigned name, device model, and operating-system name
and version.



- To check system version of current device. We will use following
functions.
!

if
([[
UIDevice

currentDevice
].
systemVersion

floatValue
] >=
3.1
){!

// Do something...!
}
else
{!

// Do something!
}!
UIImagePickerController

- The
UIImagePickerController
class manages customizable, system-supplied
user interfaces for taking pictures and movies on supported devices, and for
choosing saved images and movies for use in your app.



- The role and appearance of an image picker controller depend on the
source type you assign to it before you present it.

UIImagePickerController
* arViewController = [[
UIImagePickerController

alloc
]
init
];!
arViewController.
sourceType
=
UIImagePickerControllerSourceTypeCamera
;
"
!
"
!
-
You can customize an image picker controller to manage user interactions
yourself. To do this, provide an overlay view containing the controls you
want to display.

"
UIViewController
* arOverlayViewController = [[
UIViewController

alloc
]
initWithNibName
:
@"AROverlayViewController"

bundle
:
nil
];!
"
arOverlayView
= (
AROverlayView
*)arOverlayViewController.
view
;!

arViewController
.
cameraOverlayView
=
arOverlayViewController.
view
;!




CGAffineTransform

- The
CGAffineTransform
data structure represents a matrix used for affine
transformations.



-
UIView contain a property called transform which is CGAffineTransform. We
can set this property to transform UIView.



- To rotate a view.


UIView
* view = [[
UIView

alloc
]
initWithFrame
:
CGRectMake
(
0
,
0
,
400
,
400
)];!
"
CGAffineTransform
transform =
CGAffineTransformRotate
(view.
transform
, -
0.5
*
M_PI
); !
view.
transform
= transform;!
!
!
- CGAffineTransform can also perform translate, scale.





CLLocationManager

-The
CLLocationManager
class defines the interface for configuring the delivery
of location- and heading-related events to your application.



- To configure and use a
CLLocationManager
object to deliver events:

1. Always check to see whether the desired services are available before starting
any services and abandon the operation if they are not.

2. Create an instance of the
CLLocationManager
class.

3. Assign a custom object to the
delegate
property. This object must conform to
the
CLLocationManagerDelegate
protocol.

4. Configure any additional properties relevant to the desired service.

5. Call the appropriate start method to begin the delivery of events.

locationManager
= [[
CLLocationManager

alloc
]
init
];!
locationManager
.
headingFilter
=
0.2f
;!
locationManager
.
distanceFilter
=
5.0f
;!
locationManager
.
delegate
=
self
;!
[
locationManager

startUpdatingLocation
];!
[
locationManager

startUpdatingHeading
];!




CLLocation and CLLocationCoordinate2D

- A
CLLocation
object represents the location data generated by a
CLLocationManager
object.



- In iOS, this class also reports information about latitude, longitude, altitude,the
instantaneous speed and heading in which the device is moving.


- Typically, you use a
CLLocationManager
object to create instances of this class
based on the last known location of the user’s device.



- CLLocation got a coordinate property which with class type
CLLocationCoordinate2D which contain longitude and latitude.

MKMapView

-
An
MKMapView
object provides an embeddable map interface, similar to the one
provided by the Maps application.



- You can center the map on a given coordinate, specify the size of the area you
want to display, and annotate the map with custom information.


MKCoordinateRegion
viewRegion =
MKCoordinateRegionMakeWithDistance
(
userLocation
.
coordinate
,
0.5
*
METERS_PER_MILE
,
0.5
*
METERS_PER_MILE
);!

MKCoordinateRegion
adjustedRegion = [
mapView

regionThatFits
:viewRegion]; !
[
mapView

setRegion
:adjustedRegion
animated
:
true
];!
!
- Annotation is an class use to show the information about a specific place. An
annotation object is any object that conforms to the
MKAnnotation
protocol.


MKPointAnnotation
* annotation = [[
MKPointAnnotation

alloc
]
init
];!

CLLocationCoordinate2D
coordinate =
{locationInfo.
latitude
,locationInfo.
longitude
};!
annotation.
title
= locationInfo.
title
;!
annotation.
coordinate
= coordinate;!
[
mapView

addAnnotation
:annotation];!






MKMapView



-
MKPointAnnotation
is the only
class apple define. If you want to make
your own custom Annotation, the
Annotation Class need to conform
MKAnnotation Protocol



- In iOS 4.0 and later, you can use
overlays to display content over a wide
portion of the map. An overlay is any
object that conforms to the MKOverlay
protocol.



NSUserDefault

- The
NSUserDefaults
class provides convenience methods for storing and
accessing common types such as floats, doubles, integers, Booleans, URLs and
property list object. Property object including
NSData
,
NSString
,
NSNumber
,
NSDate
,
NSArray
, or
NSDictionary
.



- The
synchronize
method, which is automatically invoked at periodic intervals,
keeps the in-memory cache in sync with a user’s defaults database.



- Normally, we use
- (void)setObject:(id)
value
forKey:(
NSString
*)
defaultName to
store objects and use
- (id)objectForKey:(
NSString
*)
defaultName to retrieve
objects.



- We better call synchronize once we have store set the object we need to store to
NSUserDefault instead of waiting the Class to automatically call after a period of
time.

selector

- We use selector() will return an SEL variable which store the declaration of
certain functions.


[
NSTimer

scheduledTimerWithTimeInterval
:
0.1f

target
:
self

selector
:
@selector
(openAR_)
userInfo
:
nil

repeats
:
NO
]!
!
!
- In above example, NSTimer can call openAR_ function which is belong to self when an
interval(0.1s) passed.



- If the function got parameter, we can simply add ”:“.


[
NSTimer

scheduledTimerWithTimeInterval
:
0.1f

target
:
self

selector
:
@selector
(openAR_:)
userInfo
:
nil

repeats
:
NO
]!
!