iphone Application Programming Networking

Similar documents
iphone Application Programming Networking

AsyncNetwork Cocoa & ios Networking Evolved

iphone Application Programming L09: Networking

BCS Autumn School. Distinguish your application with Bonjour. Nan Xu. 20 November 2013

Multitasking Support on the ios Platform

Mobile Application Development

Cross Platform Nearby Networking

An ios Static Library for Service Discovery and Dynamic Procedure Calls

What's New in Foundation Networking

Universal Linking Developer s Guide Release

Networking with NSURLSession

Cisco StadiumVision Mobile API for Apple ios

Cisco StadiumVision Mobile API for Apple ios

SDK Quick Start Guide

Game Center Techniques, Part 1

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy

Embed ios SDK in to a fitness app

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

ECE 435 Network Engineering Lecture 2

App Extension Best Practices

Introduction and Overview Socket Programming Lower-level stuff Higher-level interfaces Security. Network Programming. Samuli Sorvakko/Nixu Oy

Introducing the Photos Frameworks

Multitasking and Background Execution

What s New in Notifications

ECE 435 Network Engineering Lecture 2

CS193E Lecture 18. Web Kit and Networking with Bonjour & Distributed Objects

Sockets 15H2. Inshik Song

Notification SDK. Integration Guide 4.17

Effective Networking with Swift and ios 8

Introduction to Client-Server Model

ITP 342 Mobile App Dev

Notifications. Mobile Application Development in ios School of EECS Washington State University Instructor: Larry Holder

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Create a Bluetooth LE App for ios

E Event-based parser, XML, 180 Extended attributes, URLs, 118 API, 119 command line, 118 description, 118 NSURL category, 119

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Mobile Application Development L12: Storage & Communication

Computer Network Programming

It Just (Net)works. The Truth About ios' Multipeer Connectivity Framework. Alban

CS321: Computer Networks Introduction to Application Layer

SDK for ios (Swift) VMware Workspace ONE UEM

CSE 124 Discussion Section Sockets Programming 10/10/17

CS193P - Lecture 16. iphone Application Development. Audio APIs Video Playback Displaying Web Content Settings

SDK for ios (Objective- C) VMware Workspace ONE UEM

Mobile Application Development L14: Miscellaneous

Kevin van Vechten Core OS

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Activating Intrusion Prevention Service

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

Concurrent Servers. Overview. In our current assignment we have the following changes:

Intel Multi-OS Engine. Vitaly Adobe

Server-side Programming

VMware AirWatch SDK for ios (Swift) v17.5. Technical Implementation Guide Empowering your enterprise applications with MDM capabilities

Staying Secure and Unprepared: Understanding and Mitigating the Security Risks of Apple ZeroConf

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Internet Connectivity with

A Client-Server Exchange

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

Backends and Databases. Dr. Sarah Abraham

dotstack integration with STM32F4 & FreeRTOS.

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

Data IAP 2010 iphonedev.csail.mit.edu edward benson / Thursday, January 14, 2010

Network Programming in Python. based on Chun, chapter 2; plus material on classes

Socket Programming for TCP and UDP

HP Roam - Business Deployment Guide

Computer Networks Prof. Ashok K. Agrawala

Server-side Programming

CS193P - Lecture 11. iphone Application Development. Text Input Presenting Content Modally

About 1. Chapter 1: Getting started with sockets 2. Remarks 2. Examples 2. How to instantiate a socket class object 2

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy

QGDocs Documentation. Release 1.0. QuantumGraph Engineers

ios Core Data Example Application

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

4. The transport layer

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

EECS122 Communications Networks Socket Programming. Jörn Altmann

(Refer Slide Time: 1:09)

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

Using Messaging Protocols to Build Mobile and Web Applications. Jeff Mesnil

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

C-Bindings for BTNS APIs

ios Developer s Guide Version 1.0

Introducing CloudKit. A how-to guide for icloud for your Apps. Frameworks #WWDC14. Session 208 Olivier Bonnet CloudKit Client Software

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Use the API or contact customer service to provide us with the following: General ios Android App Name (friendly one-word name)

Computer Networks. More on Standards & Protocols Quality of Service. Week 10. College of Information Science and Engineering Ritsumeikan University

Introduction to Socket Programming

Networked Applications: Sockets. End System: Computer on the Net

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

About the MBS Xojo Network Kit 2. NSConnection Wrapper 3. NSNetService Wrapper 4. Interfaces 5. NetworkKitUtilityModule 5. NSNetServiceBrowserMB 6

Design and Architecture. Derek Collison

Introduction and Overview Socket Programming Lower-level stuff Higher-level interfaces Security. Network Programming. Samuli Sorvakko/Trusteq Oy

Linux SDK for UPnP Devices v1.4

CS321: Computer Networks Socket Programming

IERG 4080 Building Scalable Internet-based Services

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Duration 5 days (For basic crowd 5+3days needed)

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Transcription:

iphone Application Programming Networking Media Computing Group RWTH Aachen University WS 2013/2014 http://hci.rwth-aachen.de/iphone

Networking Connect Two Peers Standard UI What Is the Peer Picker? Game Center Multipeer Connectivity URL Loading System Bonjour 33 2 AirDrop Push Notifications Socket Networking AsyncNetwork

Game Center Leaderboards, Achievements, Challenges Matchmaking Real-time: all players are connected simultaneously and can exchange data Turn-based: players connect sequentially, data is exchanged as needed Self-hosted: Game Center provides the players, you provide the networking 3

Game Center Checklist Set up Game Center in the developer program Requires explicit app ID and provisioning profile (ask Jan-Peter) Enable Game Center in the list of capabilities of your app Add GameKit framework Authenticate the local player Implement Game Center features 4

Game Center Authentication GKLocalPlayer *localplayer = [GKLocalPlayer localplayer]; localplayer.authenticatehandler = ^(UIViewController *viewcontroller, NSError *error) { GKLocalPlayer *localplayer = [GKLocalPlayer localplayer]; if (viewcontroller = nil) { // show the authentication dialog by presenting viewcontroller else if (localplayer.isauthenticated) { // store localplayer as the authenticated player else { // disable Game Center ; 5

Real-Time Matchmaking Create GKMatchRequest Find players via GKMatchmaker or GKMatchmakerViewController Accept direct invites by implementing the invitehandler of GKMatchmaker Optionally find nearby players Optionally find players for a hosted match Exchange Data Send data to other players Optionally pick a player to act as server 6

Turn-Based Matchmaking Create GKMatchRequest Find players via GKTurnBasedMatch or GKTurnBasedMatchmakerViewController Or load ongoing matches via GKTurnBasedMatch Determine current player Load and update match data Match data is stored with the match and distributed to all players Advance the match to the next player 7

Documentation Game Center Overview Game Center Programming Guide GameKit Framework Reference 8

Multipeer Connectivity Connect Two Peers hat Is the Peer Picker? 33 Connect to and exchange data with nearby devices Uses Wifi, peer-to-peer Wifi, or Bluetooth 9

Multipeer Connectivity Overview Create an MCPeerID for your device Create an MCNearbyServiceBrowser or MCBrowserViewController to find and connect to advertised devices Exchange data via the MCSession object Create an MCSession Create an MCNearbyServiceAdvertiser or MCAdvertiserAssistant to advertise your device 10

Multipeer Connectivity Example // set up peer id self.peerid = [[MCPeerID alloc] initwithdisplayname:[[uidevice currentdevice] name]]; // set up session self.session = [[MCSession alloc] initwithpeer:self.peerid]; self.session.delegate = self; // set up advertising service self.advertiser = [[MCAdvertiserAssistant alloc] initwithservicetype:myservicename discoveryinfo:nil session:self.session]; self.advertiser.delegate = self; [self.advertiser start]; // set up peer browser self.browserviewcontroller = [[MCBrowserViewController alloc] initwithservicetype:myservicename session:self.session]; self.browserviewcontroller.delegate = self; [self presentviewcontroller:self.browserviewcontroller animated:yes completion:null]; 11

Multipeer Connectivity Example (cont.) // asynchronously send data to all peers if ([self.session senddata:data topeers:self.session.connectedpeers withmode:mcsessionsenddatareliable error:&error]) { // handle error // receive data from peer - (void)session:(mcsession *)session didreceivedata:(nsdata *)data frompeer: (MCPeerID *)peerid { //... // open output stream to a specific peer self.stream = [self.session startstreamwithname:mystreamname topeer:peer error:&error]; if (self.stream) { // handle error self.stream.delegate = self; // receive input stream from peer - (void)session:(mcsession *)session didreceivestream:(nsinputstream *)stream withname:(nsstring *)streamname frompeer: (MCPeerID *)peerid { self.inputstream = stream; self.inputstream.delegate = self; 12

Documentation Multipeer Connectivity Framework Reference 13

AirDrop Exchange data with nearby devices Uses Wifi, peer-to-peer Wifi, or Bluetooth 14

AirDrop Checklist Only supports transfer between ios 7 devices Enable AirDrop on the receiving device from the control center Create and configure UIActivityViewController Attach your content to be shared Supports NSString and UIImage Optionally use UIActivityItemProvider or UIActivityItemSource To accept shared content, register a custom UTI or URL scheme 15

AirDrop Example // configure and display the activity view controller // self.mycontent implements the UIActivityItemSource protocol UIActivityViewController *activityviewcontroller = [[UIActivityViewController alloc] initwithactivityitems:@[self.mycontent] applicationactivities:nil]; [self presentviewcontroller:activityviewcontroller animated:yes completion:nil]; // Handle opening URLs in the AppDelegate - (BOOL)application:(UIApplication *)application openurl:(nsurl *)url sourceapplication:(nsstring *)sourceapplication annotation:(id)annotation { if (url) { //... 16

Documentation AirDrop Example Code 17

Local and Push Notifications Notify the user (and your app) about an external event Notifications are visualized in multiple ways: Badge with a number Alert Sound Notification Center Not used to transmit data 18

Local Notifications Schedule a local notification Create and configure UILocalNotification Tell UIApplication to schedule the location notification Receive a local notification Implement UIAppDelegate Application is not running: Application is running: application:didreceivelocalnotification: application:didfinishlaunchingwithoptions: 19

Scheduling Local Notifications Example UILocalNotification *notification = [UILocalNotification new]; notification.firedate = [[NSDate alloc] initwithtimeintervalsincenow:10.]; notification.alertbody = @"Answer me"; notification.soundname = UILocalNotificationDefaultSoundName; notification.applicationiconbadgenumber = 1; notification.userinfo = self.custominfo; [[UIApplication sharedapplication] schedulelocalnotification:notification]; 20

Receiving Local Notifications Example - (BOOL)application:(UIApplication *)application didfinishlaunchingwithoptions: (NSDictionary *)launchoptions { UILocalNotification *notification = launchoptions[uiapplicationlaunchoptionslocalnotificationkey]; if (notification) { [self handlenotification:notification]; return YES; - (void)application:(uiapplication *)application didreceivelocalnotification: (UILocalNotification *)notification; { [self handlenotification:notification]; 21

Remote Notifications Register for Remote Notifications Tell UIApplication to register for remote notifications Implement UIApplicationDelegate and send device token to your server Receiving Remote Notifications Similar to local notifications (replace Local with Remote) 22

Registering for Remote Notifications Example - (BOOL)application:(UIApplication *)application didfinishlaunchingwithoptions: (NSDictionary *)launchoptions { // register for remote notifications [application registerforremotenotificationtypes:(uiremotenotificationtypebadge UIRemoteNotificationTypeSound)]; return YES; - (void)application:(uiapplication *)app didregisterforremotenotificationswithdevicetoken:(nsdata *)devtoken { // send the device token to your server 23

Receiving Remote Notifications Example - (BOOL)application:(UIApplication *)application didfinishlaunchingwithoptions: (NSDictionary *)launchoptions { NSDictionary *userinfo = launchoptions[uiapplicationlaunchoptionsremotenotificationkey]; if (userinfo) { [self handleremotenotification:userinfo]; return YES; - (void)application:(uiapplication *)application didreceiveremotenotification: (NSDictionary *)userinfo; { [self handleremotenotification:userinfo]; 24

Sending Remote Notifications Your server Apple Your client s device 25

Device Token 26

Remote Notification Payload JSON (max. 256 bytes) aps defines notification type { alert message, badge count, sound Additional keys can be provided as needed "aps" : { "alert" : "You got your emails.", "badge" : 9, "sound" : "bingbong.aiff", "acme1" : "bar", "acme2" : 42 Payload is available via the userinfo dictionary 27

Implementing the Server Many packages available Ruby: https://github.com/jpoz/apns NodeJS: https://github.com/argon/node-apn PHP: https://code.google.com/p/apns-php Python: https://github.com/djacobs/pyapns 28

URL Loading System Download content from a web server Supports background download Communicate with a web service 29

URL Session Access content via HTTP Session Types: Default: store content on the disk Ephemeral: store content to memory Background: process task in the background (when app is closed) Session Tasks: Data tasks: exchange NSData objects with a web server (not available for background session type) Download/Upload tasks: download or upload large files 30

Data Task Example // create the data task to download an image NSURLSession *session = [NSURLSession sharedsession]; NSURLSessionDataTask *task = [session datataskwithurl:self.imageurl completionhandler:^(nsdata *data, NSURLResponse *response, NSError *error) { ]; // update the UI on the main thread [[NSOperationQueue mainqueue] addoperationwithblock:^{ self.imageview.image = [UIImage imagewithdata:data]; ]; // start the data task [task resume]; 31

Background Download Task Example // AppDelegate - (void)createsession { if (self.session) { NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundsessionconfiguration:id]; _session = [NSURLSession sessionwithconfiguration:config delegate:self delegatequeue:nil]; - (void)start { // create and start the download task [self createsession]; [[self.session downloadtaskwithurl:downloadurl] resume]; - (void)application:(uiapplication *)application handleeventsforbackgroundurlsession:(nsstring *)identifier completionhandler:(void (^)())completionhandler { // recreate the session if necessary and store the completion handler [self createsession]; self.completionhandler = completionhandler; - (void)urlsession:(nsurlsession *)session downloadtask:(nsurlsessiondownloadtask *)downloadtask didfinishdownloadingtourl:(nsurl *)location { // update the UI, then create a new snapshot by calling the completion handler if (self.completionhandler) self.completionhandler(); 32

Documentation URL Loading System Programming Guide 33

Bonjour Service discovery framework (zeroconf networking) Does not transfer data Also available for Unix/Linux & Windows Based on multicast DNS (mdns) Every client stores own DNS table Service lookup over DNS request broadcast 34

Bonjour Service Publish service using NSNetService Unique Service name Service type and transport layer ( _http._tcp. ) Registration domain ( local. ) Port Discover services using NSNetServiceBrowser 35

Bonjour Browser

Bonjour Example // setup and start net service self.netservice = [[NSNetService alloc] initwithdomain:@"local." type:@"_myservice._tcp" name:@"myservice" port:8080]; [self.netservice publish]; // setup and start net service browser self.netservicebrowser = [NSNetServiceBrowser new]; self.netservicebrowser.delegate = self; [self.netservicebrowser searchforservicesoftype:@"_myservice._tcp" indomain:@"local."]; // delegate methods - (void)netservicebrowser:(nsnetservicebrowser *)browser didfindservice:(nsnetservice *)netservice morecoming:(bool)hasmore { // handle the discovered service - (void)netservicebrowser:(nsnetservicebrowser *)browser didremoveservice:(nsnetservice *)netservice morecoming:(bool)hasmore { // handle the removed service 37

Documentation Bonjour for Developers Bonjour Overview NSNetServices Programming Guide CocoaEcho Example 38

Socket Networking Sockets are used to establish a connection between devices BSD sockets CFNetwork Streams are used to communicate through sockets CFStream NSStream 39

Socket Networking Checklist Include CFNetwork Framework Create listening socket on server Create read and write streams from client to server Create read and write streams from server to client Send streaming data and respond to stream events 40

Server Example Create socket // create new socket CFSocketContext socketcontext = {0, ( bridge void *) self, NULL, NULL, NULL; socket = CFSocketCreate(kCFAllocatorDefault, AF_INET, SOCK_STREAM, 0, kcfsocketacceptcallback, &AcceptCallback, &socketcontext); if (socket) { // handle error // configure socket to allow wildcard address reuse static const int yes = 1; setsockopt(cfsocketgetnative(socket), SOL_SOCKET, SO_REUSEADDR, (const void *) &yes, sizeof(yes)); 41

Server Example Bind socket to address and port and attach to run loop // set up socket as a listening socket struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_len = sizeof(addr); addr.sin_family = AF_INET; // TCP socket addr.sin_port = htons(0); // automatic port addr.sin_addr.s_addr = htonl(inaddr_any); // bind to all interfaces NSData *addrdata = [NSData datawithbytes:&addr length:sizeof(addr)]; if (kcfsocketsuccess = CFSocketSetAddress(socket, ( bridge CFDataRef)addrData)) { // handle error // attach the socket to the current run loop CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kcfrunloopcommonmodes); CFRelease(source); 42

Server Example Incoming connection callback static void AcceptCallback(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { CFSocketNativeHandle nativesocket = *(CFSocketNativeHandle *)data; CFReadStreamRef readstream = NULL; CFWriteStreamRef writestream = NULL; // create read and write streams CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativesocket, &readstream, &writestream); if (readstream writestream) { close(nativesocket); return; // configure streams CFReadStreamSetProperty(readStream, kcfstreampropertyshouldclosenativesocket, kcfbooleantrue); CFWriteStreamSetProperty(writeStream, kcfstreampropertyshouldclosenativesocket, kcfbooleantrue); // create connection object [( bridge MyServer *)info createconnectionwithinputstream:( bridge NSInputStream *)readstream outputstream:( bridge NSOutputStream *)writestream]; CFRelease(readStream); CFRelease(writeStream); 43

Client Example [netservice getinputstream:&_inputstream outputstream:&_outputstream]; // configure and open the input stream self.inputstream.delegate = self; [self.inputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [self.inputstream open]; // configure and open the output stream self.outputstream.delegate = self; [self.outputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [self.outputstream open]; // alternative: CFStreamCreatePairWithSocketToHost() 44

Stream Events Example // NSStream delegate method - (void)stream:(nsstream *)stream handleevent:(nsstreamevent)event { uint8_t buffer[size]; NSInteger length; // there is incoming data to be collected if (event == NSStreamEventHasBytesAvailable) { length = [(NSInputStream *)stream read:(uint8_t *)buffer maxlength:size]; if (length > 0) { // process this chunk of data // there is space in the buffer for more outgoing data else if (event == NSStreamEventHasSpaceAvailable) { // copy next chunk of data into the buffer length = [(NSOutputStream *)stream write:(uint8_t *)buffer maxlength:size]; 45

Data Chunking Transferred data might be bigger than the stream buffer Data must be sent and arrives in chunks Chunking strategies: Transmit a special character (\0) at the end of the data Transmit length of the data before the data 46

Documentation Networking Programming Topics Stream Programming Guide BSD Sockets CocoaEcho Example 47

AsyncNetwork Automatic server discovery and connection (Bonjour) Automatic Object Encoding (NSCoding) Simple communication (read, write, request, command byte) Broadcasting Based on GCDAsyncSocket 48

AsyncNetwork Client and Server // Setup the server - (void)setupserver { self.server = [AsyncServer new]; self.server.servicename = @"My Service"; self.server.delegate = self; [self.server start]; // Client did connect - (void)server:(asyncserver *)theserver didconnect:(asyncconnection *)connection { [self.server sendobject:@"hello Client"]; // Setup client - (void)setupclient { self.client = [AsyncClient new]; self.client.delegate = self; [self.client start]; // Client did receive message - (void)client:(asyncclient *)theclient didreceivecommand:(asynccommand)command object:(id)object connection:(asyncconnection *)connection { NSLog(@"%@", object); 49

AsyncNetwork Requests // send a request to the server [AsyncRequest firerequestwithhost:@"192.168.0.1" port:10000 command:0 object:@"hello" responseblock:^(id response, NSError *error) { if (error) return; NSLog(@"%@", response); ]; 50

AsyncNetwork Broadcasting - (void)setupbroadcaster { // Create and start the broadcaster self.broadcaster = [AsyncBroadcaster new]; self.broadcaster.port = 10000; self.broadcaster.delegate = self; [self.broadcaster start]; - (void)broadcast { // Encode the string message NSString *message = @"Hello World\n"; NSData *encodedmessage = [message datausingencoding:nsutf8stringencoding]; // Broadcast the encoded message [self.broadcaster broadcast:encodedmessage]; - (void)broadcaster:(asyncbroadcaster *)thebroadcaster didreceivedata:(nsdata *)data fromhost:(nsstring *)host { // Decode the message NSString *message; message = [[NSString alloc] initwithdata:data encoding:nsutf8stringencoding]; NSLog(@"%@", message); 51

AsyncNetwork https://github.com/jdiehl/async-network 52