iphone application programming l09:...
TRANSCRIPT
Jan Borchersmedia computing group
iPhone Application ProgrammingL09: Networking
Prof. Dr. Jan Borchers, Florian Heller, Jonathan DiehlMedia Computing Group, RWTH Aachen
WS 2009/2010http://hci.rwth-aachen.de/iphone
Jan Borchersmedia computing group
Networking
2
• Bonjour Networking• Push Notifications• GameKit• Remote (Web) Objects
Jan Borchersmedia computing group
Bonjour Networking
3
Jan Borchersmedia computing group
Bonjour
4
• Apples flavor of zeroconf networking• a.k.a. multicast DNS
• Service discovery framework• Does not transfer data• Likely to become an official standard• Also available for Unix/Linux & Windows
Jan Borchersmedia computing group
Using Bonjour
• As Server (NSNetService)• Create a socket to provide the service• Define service
• Name: Service name• Type: Service type and transport layer (“_http._tcp.”)• Registration domain ("local.")• Port
• Publish service
5
Jan Borchersmedia computing group
Using Bonjour
• As Client • Browse for available services / Create NSNetService directly• Check if service is still available with resolveWithTimeout:• Get socket information and connect
6
Jan Borchersmedia computing group
Bonjour Example// set up network service
myService = [[NSNetService alloc] initWithDomain:@"" type:@"_neighbors._tcp"
! ! ! ! ! ! ! ! name:[[UIDevice currentDevice] name] ! ! ! ! ! ! ! ! port:9090];myService.delegate = self;// publish network service[myService publish];// published- (void)netServiceDidPublish:(NSNetService *)sender {! NSLog(@"published: %@", sender);}// not published- (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict {! NSLog(@"not published: %@ -> %@", sender, errorDict);}- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data {! NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];! NSLog(@"TXT: %@", message);}
7
Jan Borchersmedia computing group
Bonjour Example// set up array
netServices = [[NSMutableArray alloc] init];!// set up the service browserserviceBrowser = [[NSNetServiceBrowser alloc] init];serviceBrowser.delegate = self;[serviceBrowser searchForServicesOfType:@"_neighbors._tcp" inDomain:@""];
// new service- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing;{! [netServices addObject:netService];}
// service removed- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didRemoveService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing;{! [netServices removeObject:netService];}
8
Jan Borchersmedia computing group
What’s a Socket
• Combination of Address and port• Listening socket handles data via callback
9
Jan Borchersmedia computing group
Socket Networking
• Sockets are used to actually transmit data• CFSocket: C encapsulation of BSD sockets
• Runloop functionality, no blocking calls
• Objective C wrapper: CocoaAsyncSocket
10
Jan Borchersmedia computing group
Setting up a Listening Socket
• Create a socket context• Create a socket• Bind the socket to an address• Handle events in a callback
11
Jan Borchersmedia computing group
CFSocketContextstruct CFSocketContext {
CFIndex version; void *info; CFAllocatorRetainCallBack retain; CFAllocatorReleaseCallBack release; CFAllocatorCopyDescriptionCallBack copyDescription;};
12
Jan Borchersmedia computing group
CFSocketCreateCFSocketRef CFSocketCreate (
CFAllocatorRef allocator, SInt32 protocolFamily, //default: PF_INET SInt32 socketType, //default:SOCK_STREAM, UDP:SOCK_DGRAM SInt32 protocol, //STREAM:IPPROTO_TCP DGRAM:IPPROTO_UDP CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context);
13
Jan Borchersmedia computing group
CFSocket ExampleCFSocketContext socketCtxt = {0, self, NULL, NULL, NULL};ipv4socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, (CFSocketCallBack)&TCPServerAcceptCallBack, &socketCtxt);
struct sockaddr_in socketAddress;memset(&socketAddress, 0, sizeof(socketAddress));socketAddress.sin_len = sizeof(socketAddress);socketAddress.sin_family = AF_INET; // Address family (IPv4 vs IPv6)socketAddress.sin_port = 0; // Actual port will get assigned automatically by kernel// We must use "network byte order" format (big-endian) for the value heresocketAddress.sin_addr.s_addr = htonl(INADDR_ANY); // Convert the endpoint data structure into something that CFSocket can useNSData *socketAddressData = [NSData dataWithBytes:&socketAddress length:sizeof(socketAddress)];
//Bind to addressCFSocketSetAddress(listeningSocket,(CFDataRef)socketAddressData)
14
Jan Borchersmedia computing group
CFSocket Example (Continued)//Create a runloop source for the socket
CFRunLoopRef cfrl = CFRunLoopGetCurrent();CFRunLoopSourceRef source4 =
CFSocketCreateRunLoopSource(kCFAllocatorDefault, _ipv4socket, 0);CFRunLoopAddSource(cfrl, source4, kCFRunLoopCommonModes);CFRelease(source4);
15
Jan Borchersmedia computing group
CFSocket Callbacksstatic void TCPServerCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { if (type == kCFSocketAcceptCallBack) {
! ! //handle a new connection}if (type == kCFSocketDataCallBack) {
//handle incoming data}
}
16
Jan Borchersmedia computing group
Demo
17
Jan Borchersmedia computing group
Push Notifications
18
Jan Borchersmedia computing group
Push Notifications
• Notify the user that data is waiting• Does not transmit data• Behavior is defined by the user
19
Jan Borchersmedia computing group
Apple Push Notification Service
• Central service provided by Apple• Your server connects to the APNS• The APNS transmits the message to the device• The user possibly starts your application• Your application retrieves the data
20
Jan Borchersmedia computing group
Registering a Device
• registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
• UIRemoteNotificationTypeNone• UIRemoteNotificationTypeBadge• UIRemoteNotificationTypeSound• UIRemoteNotificationTypeAlert
• Receive unique device token
• Communicate this token to your own server
• Your server specifies token when sending a payload to APNS
21
Jan Borchersmedia computing group
Registering a Device// Add registration for push nofication
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
!- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { ! ! NSLog(@"devToken=%@",deviceToken);}- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { ! ! NSLog(@"Error in registration. Error: %@", err);}
- (void)unregisterForRemoteNotifications- (UIRemoteNotificationType)enabledRemoteNotificationTypes
22
Jan Borchersmedia computing group
Message Delivery
• Server connects to APNS via TCP connection• SSL Secured• Payload delivery not guaranteed• Check the feedback service regularly
23
Jan Borchersmedia computing group
Example Payload
{ "aps" : { "alert" : "You got your emails.", "badge" : 9, "sound" : "bingbong.aiff" }, "acme1" : "bar", "acme2" : 42}
24
Jan Borchersmedia computing group
Clear a Badge Number
{ "aps" : { }, "acme2" : [ 5, 8 ]}
25
Jan Borchersmedia computing group
APNS Requirements
• Own Server providing the service• SSL certificate for your server• Defined App ID registered for Push Notifications
26
Jan Borchersmedia computing group
GameKit
27
• High-level Framework for Bluetooth Networking• Automatic Service Discovery• Efficient, low-latency communication• Integrated voice chat
Jan Borchersmedia computing group
GameKit Classes
• GKSession• Discover peers and manage connections
• GKPeerPickerController• Controller for showing and connecting to peers• Comes with its own UI
• GKVoiceChatService• control voice chat
28
Jan Borchersmedia computing group
GKPickerController Example- (void)viewDidLoad {
! session = [[GKSession alloc] initWithSessionID:@"chat" displayName:nil sessionMode:GKSessionModePeer];! peerPickerController = [[GKPeerPickerController alloc] init];! peerPickerController.delegate = self;! [peerPickerController show];}!# pragma mark GKPeerPickerControllerDelegate
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {! return session;}
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session {! ...}
29
Jan Borchersmedia computing group30
Jan Borchersmedia computing group31
GKSession
• peersWithConnectionState:• list all peers with the given state (e.g. available)
• connectToPeer:withTimeout:• request a connection to the peer
• acceptConnectionFromPeer:error:• accept a connection request
Jan Borchersmedia computing group32
GKSessionDelegate
• session:peer:didChangeState:• informs delegate about new peers and state changes• Possible states: available, unavailable, connecting,
disconnecting, connected
• session:didReceiveConnectionRequestFromPeer:• respond to connection request from peer
• Error handling• session:connectionWithPeerFailed:withError:• session:didFailWithError:
Jan Borchersmedia computing group
GKSession Communication
• setDataReceiveHandler:withContext:• define receive handler (controller) for the session
• receiveData:fromPeer:inSession:context:• implemented by receive handler
• sendData:toPeers:withDataMode:error:• send NSData to given peers• Possible data modes: reliable, unreliable
33
Jan Borchersmedia computing group
GKVoiceChatService
• Singleton • Service to control voice chat
• Manage connections• Record audio from microphone
• Delegate: Client• Transmit audio data• Playback audio data
34
Jan Borchersmedia computing group
Remote (Web) Objects
35
Jan Borchersmedia computing group
Web-Backend
36
Database
Database Database
DatabaseDatabase DatabaseDatabase
Jan Borchersmedia computing group
Web-Backend
37
Web Backend
XMLHTTP
Database
Jan Borchersmedia computing group
Ruby on Rails
38
• Open Source Web Application Framework• Runs as its own service• Main Components:
• ActionController: interface controller and routing• ActiveRecord: database abstraction• ActiveResource: remote objects
Jan Borchersmedia computing group
Ruby on Rails: Quickstart• create the app:
• rails sample_application
• generate a model• script/generate model person
• generate a “scaffold” controller• script/generate scaffold person
• migrate the database• rake db:migrate
• launch the server• script/server
39
Jan Borchersmedia computing group
Representational State Transfer
• Data is made available through (web-)resources• Each resource has its own URI• http://server/people
• HTTP operations are used to manipulate resources• POST http://server/people?id=1person[name]=Paul
• Communication via XML or JSON
40
Jan Borchersmedia computing group
REST Operations
• GET: request all resources (or by id)• GET http://server/people• GET http://server/people?id=1
• POST: update resource• POST http://server/people?id=1&person[...]=...
• PUT: create resource• PUT http://server/people?person[...]=...
• DELETE: delete resource• DELETE http://server/people?id=1
41
Jan Borchersmedia computing group
Ruby on Rails & REST
• Scaffold controller supports all REST operation• script/generate scaffold people• supports HTML (backend website) and XML
• Routes can be created automatically• map.resources :people in routes.rb
• Format and id can be specified in URI• http://server:3000/people.xml• http://server:3000/people/1.xml
42
Jan Borchersmedia computing group
iPhone HTTP RequestsNSURL *url = [NSURL URLWithString:@"http://server:3000/people.xml"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
// set the HTTP operation[request setHTTPMethod:@"POST"];
// set the post dataNSData *postData = [@"id=1&person[name]=Paul" dataUsingEncoding:NSUTF8StringEncoding];[request setHTTPBody:postData];
// fire the requestNSURLConnection *con = [NSURLConnection connectionWithRequest:request delegate:self];[con start];
43
Jan Borchersmedia computing group
Summary
• Discover services with Bonjour• Receive notifications• Connect to other devices vie BlueTooth• Access remote objects• Reading Assignment:
• Net Services Programming Guide• Push Notification Service Programming Guide• GameKit Guide
44