Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

IOS Overview App Development Life Cycle

History, IOS version, IOS Architecture Hello World App, Structure of project,
SDK Overview. Deploying to an Emulator, Debug app.

Objective C IOS User Interface


Objective-c introduction, Classes and Layout/Story board, Using resource in app,
Objects, Pointers, Properties and Methods, Component.
Object Properties and ARC.
/ IOS Overview
/ IOS Architecture

Architecture
/ IOS SDK
Tools shipped with SDK:
Xcode
Professional text editor
Debugger
GCC Compiler
Interface Builder
For creating user interface
Instruments
For optimizing applications
Dash Code
For creating web applications for safari
iPhone simulator
Demo
/ App Development Life Cycle
Demo App Development Life Cycle
/ Objective - C
• Objective-c introduction
• Classes and Objects
• Pointers
• Properties and Methods
• Object Properties and ARC
• NSString, NSArray, NSMutableArray, NSDictionary, NSMutableDictionary…
/ Objective-c introduction
• Objective-C is an object oriented language
• Objective-C was invented by two men, Brad Cox and Tom Love at their company Stepstone
• In 1988 Steve Jobs acquires Objective-C license for NeXT and Used Objective-C to build the NeXTSTEP Operating System
• In 1995 NeXT gets full rights to Objective-C from Stepstone
/ Classes
Have both definition file and implementation file: Classname.h and Classname.m

File Classname.m
File Classname.h
#import “ClassName.h”
#import <Cocoa/Cocoa.h> @implementation ClassName
@interface ClassName : NSObject{

int age; -(void)setAge:(int)number


NSString name; { age = number; }
} -(void)setName:(NSString)n
{ name = n; }
// methods declared -(int)getAge
-(void)setAge:(int)number; { return age; }
-(void)setName:(NSString)n; -(NSString)getName
-(int)getAge; { return name; }
-(NSString)getName;

@end @end
/ Object
ClassName *object = [[ClassName alloc] init];

NSString* myString = [[NSString alloc] init];

Persion* model = [[Persion alloc] init];


/ Address and Pointers
• To get address of a variable i: &I

• Pointer:
• int *addressofi = &I;
• SimpleClass *model= [[SimpleClass alloc] init];
/ Automatic Reference Counting (ARC)
/ Properties
• Strong pointer là một con trỏ, trỏ đến một đối tượng và sở hữu (own) đối tượng đó.
(tham chiếu mạnh)
#import <Cocoa/Cocoa.h>
@interface ClassName : NSObject • Weak pointer là một con trỏ, trỏ đến một đối tượng nhưng không sở hữu (own) đối
tượng đó.” (tham chiếu yếu)
@property (strong, nonatomic) NSString *str;

@property (weak, nonatomic) IBOutlet UILabel *lblTitle;

@end

Tham khảo thêm: http://rypress.com/tutorials/objective-c/properties


http://ktmt.github.io/blog/2013/09/10/ios-property-attributes/
/ strong - weak
Weak: Delegates, Outlets, Subviews, Controls...

A Strong: Remaining everywhere which is not included in WEAK.

C
/ Atomic – nonatomic
(void)setPerson:(Person *)newPerson {
_person.name = newPerson.name;
_person.age = newPerson.age;
}

giả sủ ban đầu value của person là (jony, 27)

thread 1 gọi:
setPerson truyền vào object newPerson có value (marry, 30)

Lúc này bên trong setPerson sẽ chạy:


_person.name = newPerson.name;
_person.name giờ có giá trị là marry và _person.age vẫn giữ giá trị là 27

Chưa kịp chạy lệnh tiếp theo để set _person.age thì ở thread 2 gọi getter object person;
Và getter chạy trả về (marry, 27)

thread 1 tiếp tục chạy set age cho person


_person.age = newPerson.age
_person.age giờ mới kịp đổi giá trị là 30
/ Primitive data types from C
• int, short, long
• float,double
• char

int a = 1;
float b = 33.22;
char c = ‘A’;
NSLog(@”Integer %d Float: %f Char: %c”, a, b, c);
/ Operators same as C
• +
• -
• *
• /
• ++
• --
/ Conditionals and Loops
• Same as C/C++
• if / else if/ else
• for
• while
• break
• continue
• do-while
/ Functions
return_type functionName(type v1, type v2, ….)
{
//code of function
}

Example
void showMeInfo(int age)
{
printf(“You are %d years old”, age); //or use NSLog(“You are %d years old”, age);
}
/ Non-GUI – text output
Two standard functions you see used:

printf() – same as C
printf(“Hi Lynne”); //this is actual C code

NSLog()
NSLog(@”Hi Lynne”); //this is strictly Objective-C
/ Whats this + and – stuff?
- (void)setAge:(int)number;

+ (void)setName:(NSString)n;

When declaring or implementing functions for a class, they must begin with a + or -

+ indicates a “class method” that can only be used by the class itself. (they are like static methods in Java

invoked on class itself)

- indicates “instance methods” to be used by the client program (public functions) –invoked on an object /

class instance . (they are like regular methods in Java invoked on object)
/ Object data types
NSString *str= @”hello world”;

NSNumber *num = [NSNumber numberWithInt:36];

NSLog(@”String: %@, str);

NSLog(@”Number: %@, num);


/ NSArray - NSMutableArray
NSArray *arr= [NSArray
arrayWithObjects:obj1,obj2,obj3, nil];

NSArray *arr = @[@“1”,@”2”,@”3”,@”4”];

NSMutableArray *arr= [[NSMutableArray alloc]


init];
[arr addObject:obj1];
[arr addObject:obj2];

[arr insertObject:obj atIndex:i];


[arr removeObjectAtIndex:i];
/ NSDictionary - NSMutableDictionary
// NSDictionary
NSDictionary *dict = @{
@”key1" : value,
@”key2" : value
};

// NSMutableDictionary
NSMutableDictionary *dict= [[NSMutableDictionary alloc] init];
[dict setObject:value1 forKey:@”key1”];
[dict setObject:value2 forKey:@”key2”];
Nãy giờ nói cái gì mà buồn
ngủ thế.

Chuyển chương trình đi !!!


Demo
/ StoryBoard

• Xuất hiện cùng với iOS 5


• Một cách thức tổ chức – quản lý các file giao diện, có cái nhìn tổng quát về toàn bộ màn
hình của ứng dụng.
/ Story Board
Ưu điểm
• Cho phép lập trình viên có cái nhìn tổng quát về chương trình, dễ dàng quan sát luồng UI.
• Dễ dàng kiểm soát các chuyển động của màn hình.Tiết kiệm lượng code  giảm thời gian  tiết kiệm chi phí.

Nhược điểm
• Trong quá trình làm nhóm, dễ bị conflict.
• Nếu bố trí không tốt, đưa quá nhiều view controller vào một story board sẽ làm rối.

Giải pháp:
• Chia nhỏ thành nhiều các storyboard.
/ Auto layout
• Xuất hiện cùng với Xcode 5
• Auto Layout là một hệ thống cho phép bạn bố trí giao diện của ứng dụng bằng cách tạo ra các mối quan hệ giữa
các yếu tố.
• Người dùng xác định các ràng buộc giữa các view với nhau để bố trí hợp lý, tự động thay đổi linh hoạt kích thước
của chúng trên nhiều loại màn hình.
• Thao khảo thêm:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/I
ntroduction.html
Demo Story Board
UIViewController Navigation Controller
Introduction, life cycle, Moving between
Pass data viewcontroller

TableView CollectionView
Introduction, custom Introduction, custom
tableview cell CollectionViewCell
/ UIViewController

• UIViewController Introduction
• UIViewController life cycle
• Pass data UIViewController
/ UIViewController life cycle
viewDidLoad

viewWillAppear

viewDidAppear

viewWillDisappear

viewDidDisappear
/ Navigation Controller
/ Switch UIViewController
• Presentviewcontroller
• Pushviewcontroller (using navigation controller)
UIAlertController * alert= [UIAlertController

/ Alert
alertControllerWithTitle:@”Title"
message:@”This is a message."
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES
completion:nil];

}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES
completion:nil];

}];

[alert addAction:ok];
[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];


UIAlertController * view= [UIAlertController
alertControllerWithTitle:@"My Title"
message:@"Select you Choice"

/ Action sheet
preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES
completion:nil];

}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES
completion:nil];

}];

[view addAction:ok];
[view addAction:cancel];
[self presentViewController:view animated:YES completion:nil];
/ HUB - Indicator
/ Tableview
/ UICollectionView
Demo
Networking Json
Networking Overview. Json overview, parser
json using jsonmodel

Thread/GCD/NSOperation AFNetworking
Overview AFNetworking for REST
based backends
Network
REST – Methods
HTTP Methods are a key corner stone in REST.
They define the action to be taken with a URL.
Proper RESTful services expose all four – “accidental” expose less.
Nothing stopping you doing some Mix & Match
●Some URL’s offering all of them and others a limited set

What are the four methods and what should they do?
REST CRUD (Create, Read, Update, Delete)

POST Create
GET Read
PUT Update or Create
DELETE Delete
JSON – What does it look like?
• {
• "firstName": "John", Name/Value Pairs
• "lastName": "Smith",
• "address": {
• "streetAddress": "21 2nd Street",
• "city": "New York", Child properties

• "state": "NY",
• "postalCode": 10021
• },
• "phoneNumbers": [
• "212 555-1234", String Array Number data type

• "646 555-4567"
• ]
• }
Difference [] and {}
● In general all the JSON nodes will start with a square bracket [] or with a curly bracket {}.
● The difference between [ and { is:
○ The square bracket ([) represents starting of an JSONArray node.
○ The curly bracket ({) represents JSONObject.

● If your JSON node starts with [, then we should use getJSONArray() method. Same as if the
node starts with {, then we should use getJSONObject() method.
/ JSON Model
Demo
/ NSThread
/ NSThread
/ NSOperation
/ GCD - Grand Central Dispatch
Bạn không phải chỉ định có bao nhiêu thread được phép khởi tạo trong một hàng đợi, ios sẽ tự tính toán cho
phù hợp.
NSUserdefaults File
NSUserdefaults File overview
Overview.

CORE DATA WebView, APNS, Map, Photo


Overview, magical Overview
record, mongenerator
/ NSUserdefaults
NSUserDefault là cách lưu dữ liệu nhanh chóng và dễ dàng nhất của Objective-C:
- Thường dùng để lưu những dữ liệu đơn giản như các chuỗi (string), số lượng (number)
hay đơn thuần chỉ là một biến bool.
- Ngoài ra nó cũng có thể dùng để lưu lại các NSArray hay NSDictionary nếu bạn không đưa
những dữ liệu phức tạp vào như phim, hình ảnh và âm thanh. Đây là giải pháp tốt nhất
nếu bạn muốn lưu lại những thiết lập (setting hay option) dành cho chương trình.
/ NSUserdefaults
Save:

NSUserDefaults * nsu = [NSUserDefaults standardUserDefaults];

[nsu setBool:YES forKey:@"IS_LOGIN"];

[nsu setObject:Username forKey:@"USER_NAME"];

[nsu synchronize];
/ NSUserdefaults
LẤY RA:
NSUserDefaults * nsu = [NSUserDefaults standardUserDefaults];
NSString *userName = [nsu objectForKey:@"USER_NAME"];
BOOL isLogin = [nsu boolForKey:@"IS_LOGIN"];
/ File
/ CORE DATA
Demo

You might also like