ios training (advanced)

22
Yahoo! Confidential 1 iOS Training (Advanced) Yahoo! Confidential Gurpreet Singh Sriram Viswanathan

Upload: gurpreet-singh-sachdeva

Post on 22-Apr-2015

211 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: iOS training (advanced)

Yahoo! Confidential 1

iOS Training (Advanced)

Yahoo! Confidential

Gurpreet SinghSriram Viswanathan

Page 2: iOS training (advanced)

Yahoo! Confidential

Topics

Using custom delegate and protocols Saving and Restoring the Application State Introduction to NSURLConnection Hands on example for

Fetching data from server Using Table View Controller Populating Data in a Table Refreshing Data

Deploying your app on a device

2

Page 3: iOS training (advanced)

Yahoo! Confidential

Using custom delegate and protocols

Defining Protocol

@protocol DataConsumerDelegate

// Method to handle response of the rest connection call

- (void) finishedReceivingData: (NSData *) data;

@end

Conforming to protocol

@interface MyDataConsumer : NSObject <DataConsumerDelegate>

@end

@implementation MyDataConsumer

- (void) finishedReceivingData: (NSData *) data {

// HANDLE THE DATA

}

@end

3

Page 4: iOS training (advanced)

Yahoo! Confidential

Using custom delegate and protocols

Using delegate

@interface AsynchronousRestConnection : NSObject {

id<DataConsumerDelegate> dataConsumerDelegate;

}

// Used to store delgate object (it must conform to DataConsumerDelegateprotocol)

@property (nonatomic, weak) id<DataConsumerDelegate> dataConsumerDelegate;

- (void) getData;

@end

@implementation AsynchronousRestConnection

@synthesize dataConsumerDelegate;

- (void) getData {

// GET DATA AND STORE IT IN data

// YOU CAN DO MULTI QUERY AND MERGE OUTPUT

[self.dataConsumerDelegate finishedReceivingData:data];

}

@end

4

Page 5: iOS training (advanced)

Yahoo! Confidential

Using custom delegate and protocols

Updating the MyDataConsumer class to use AsynchronousRestConnection

@interface MyDataConsumer : NSObject <DataConsumerDelegate>

- (void) getDataFromApi;

@end

@implementation MyDataConsumer

- (void) finishedReceivingData: (NSData *) data {

// HANDLE THE DATA

}

- (void) getDataFromApi {

AsynchronousRestConnection *arc = [[AsynchronousRestConnection alloc] init];

arc.dataConsumerDelegate = self;

// CONSUME DATA

}

@end

5

Page 6: iOS training (advanced)

Yahoo! Confidential

Topics

Using custom delegate and protocols Saving and Restoring the Application State Introduction to NSURLConnection Hands on example for

Fetching data from server Using Table View Controller Populating Data in a Table Refreshing Data

Deploying your app on a device

6

Page 7: iOS training (advanced)

Yahoo! Confidential

Saving and Restoring Application State

During the preservation and restoration process, your app has a handful of responsibilities.

During preservation, your app is responsible for:

Telling UIKit that it supports state preservation. Telling UIKit which view controllers and views should be preserved. Encoding relevant data for any preserved objects.

During restoration, your app is responsible for: Telling UIKit that it supports state restoration. Providing (or creating) the objects that are requested by UIKit. Decoding the state of your preserved objects and using it to return the

object to its previous state.

7

Page 8: iOS training (advanced)

Yahoo! Confidential

Saving and Restoring Application State

8

High-level flow interface preservation

Page 9: iOS training (advanced)

Yahoo! Confidential

Saving and Restoring Application State

9

High-level flow for restoring your user interface

Page 10: iOS training (advanced)

Yahoo! Confidential

Topics

Using custom delegate and protocols Saving and Restoring the Application State Introduction to NSURLConnection Hands on example for

Fetching data from server Using Table View Controller Populating Data in a Table Refreshing Data

Deploying your app on a device

10

Page 11: iOS training (advanced)

Yahoo! Confidential

Introduction to NSURLConnection

Asynchronous Request Synchronous Request POST request Using Connection Timeout Cache Policies

11

Page 12: iOS training (advanced)

Yahoo! Confidential

Asynchronous Request

To fetch some data, we’ll follow these 3 basic steps:

Have our class conform to the NSURLConnectionDelegate protocol and declare a var to store the response data

Implement the NSURLConnectionDelegate protocol methods Create an instance of NSURLRequest and NSURLConnection to kick off

the request

12

Page 13: iOS training (advanced)

Yahoo! Confidential

Asynchronous Request

Step 1:

13

Page 14: iOS training (advanced)

Yahoo! Confidential

Asynchronous Request

Step 2:

14

Page 15: iOS training (advanced)

Yahoo! Confidential

Asynchronous Request

Step 3:

15

Page 16: iOS training (advanced)

Yahoo! Confidential

Asynchronous Request

Step 4:

First connection:didReceiveResponse will be hit, indicating that the server has responded.

Then the handler, connection:didReceiveData: will be hit several times. This is where you’ll append the latest data to the response data variable

you declared in Step 1. Then finally, connectionDidFinishLoading: will be hit and you can parse the

response data variable.

16

Page 17: iOS training (advanced)

Yahoo! Confidential

POST Request

A synchronous request looks like the below:

You’ll set the HTTPMethod property to POST You’ll assign the header fields and value in the body Ensure that you’re using an instance of NSMutableURLRequest because

NSURLRequest is immutable.

17

Page 18: iOS training (advanced)

Yahoo! Confidential

Using Connection Timeout

Using Connection Timeout

If you want to set a timeout for your data retrieval, you can set the timeoutInterval property of your NSURLRequest.

Ensure that you’re using an instance of NSMutableURLRequest because NSURLRequest is immutable.

18

Page 19: iOS training (advanced)

Yahoo! Confidential

Cache Policies

To specify how your data should be cached locally, you can set a cache policy with your NSURLRequest.

Following snippet shows you how you can define a cache policy and a timeout.

If you don’t specify any caching policy with your NSURLRequest, then it will try to conform to whatever the protocol states.

For example, with HTTP it will look at the server response headers and try to conform to what it dictates about caching.

19

Page 20: iOS training (advanced)

Yahoo! Confidential

Topics

Using custom delegate and protocols Saving and Restoring the Application State Introduction to NSURLConnection Hands on example for

Fetching data from server Using Table View Controller Populating Data in a Table Refreshing Data

Deploying your app on a device

20

Page 21: iOS training (advanced)

Yahoo! Confidential

Topics

Using custom delegate and protocols Saving and Restoring the Application State Introduction to NSURLConnection Hands on example for

Fetching data from server Using Table View Controller Populating Data in a Table Refreshing Data

Deploying your app on a device

21

Page 22: iOS training (advanced)

Yahoo! Confidential

Deploying your app on a device

Steps to deploy your app on a device

Step 1: iOS Developer Program (iPhone provision portal)

Step 2: Create a Certificate Signing Request

Step 3: Create a Development Certificate

Step 4: Adding a Device

Step 5: Create an App ID

Step 6: Create a Provisioning Profile

Step 7: Configuring the Project

Step 8: Build and Run

For detail refer:

http://mobile.tutsplus.com/tutorials/iphone/how-to-test-your-apps-on-physical-ios-devices/

22