Download - iOS training (advanced)
Yahoo! Confidential 1
iOS Training (Advanced)
Yahoo! Confidential
Gurpreet SinghSriram Viswanathan
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
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
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
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
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
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
Yahoo! Confidential
Saving and Restoring Application State
8
High-level flow interface preservation
Yahoo! Confidential
Saving and Restoring Application State
9
High-level flow for restoring your user interface
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
Yahoo! Confidential
Introduction to NSURLConnection
Asynchronous Request Synchronous Request POST request Using Connection Timeout Cache Policies
11
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
Yahoo! Confidential
Asynchronous Request
Step 1:
13
Yahoo! Confidential
Asynchronous Request
Step 2:
14
Yahoo! Confidential
Asynchronous Request
Step 3:
15
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
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
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
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
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
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
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