iphone Application Programming Networking

Similar documents
iphone Application Programming Networking

iphone Application Programming L09: Networking

Multitasking Support on the ios Platform

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

AsyncNetwork Cocoa & ios Networking Evolved

App Extension Best Practices

What s New in Notifications

Networking with NSURLSession

Cross Platform Nearby Networking

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

Enhancing your apps for the next dimension of touch

Mobile Application Development

Effective Networking with Swift and ios 8

Seamless Linking to Your App

What's New in Foundation Networking

An ios Static Library for Service Discovery and Dynamic Procedure Calls

Game Center Techniques, Part 1

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

Create a Bluetooth LE App for ios

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

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

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

Cocoa Touch Best Practices

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

What s New in Core Bluetooth

Monetize and Promote Your App with iad

Mobile Application Programing: ios. Messaging

SDK for ios (Swift) VMware Workspace ONE UEM

Media Playback and Recording. CS193W - Spring Lecture 3

Using and Extending the Xcode Source Editor

Applications and Layered Architectures. Chapter 2 Communication Networks Leon-Garcia, Widjaja

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

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

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Client software design

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Computer Networks Prof. Ashok K. Agrawala

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

UDP CONNECT TO A SERVER

SOCKETS. COMP750 Distributed Systems

Universal Linking Developer s Guide Release

Network Communication

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

The User Datagram Protocol

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

HP Roam - Business Deployment Guide

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Advanced Notifications

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

Getting the Most Out of HealthKit

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

Backends and Databases. Dr. Sarah Abraham

Mobile Application Programming. Controls

Creating Audio Apps for watchos

Introduction to Client-Server Model

GKPlayer object, 59 NSMutableArray, 60 playernameforid, 62 posting a score, 64 presentation Apple s GUI, 49 custom GUI, 53 in social app or game, 36

Introducing Search APIs

Mobile Application Programming. Data and Persistence

WatchKit In-Depth, Part 2

What s New in tvos #WWDC16. App Frameworks. Session 206. Hans Kim tvos Engineer

Windows Sockets: A Quick And Dirty Primer

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

Sockets 15H2. Inshik Song

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

Cisco StadiumVision Mobile API for Apple ios

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

ECE 435 Network Engineering Lecture 2

Cisco StadiumVision Mobile API for Apple ios

Piotr Mielecki Ph. D.

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

Amazon Mobile Analytics. User Guide

CUWN Release 8.2 mdns Gateway with Chromecast Support Feature Deployment Guide

UNIX Network Programming. Overview of Socket API Network Programming Basics

Notification Services

Multicast/Broadcast Setup

ECE 435 Network Engineering Lecture 2

Libelium Cloud Hive. Technical Guide

Integrating Game Center into a BuzzTouch 1.5 app

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

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

What s New in HomeKit

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

ITP 342 Mobile App Dev

Computer Network Programming

Stanford CS193p. Developing Applications for ios Fall Stanford CS193p. Fall 2013

Embedded System Design

Cisco Events Mobile Application

Writing Energy Efficient Apps

What's New in Core Spotlight

UNIT IV- SOCKETS Part A

Java Basics 5 - Sockets. Manuel Oriol - May 4th, 2006

IERG 4080 Building Scalable Internet-based Services

A Client-Server Exchange

Linux SDK for UPnP Devices v1.4

MVC and Interface Builder IAP 2010

Document Version Date: 1st March, 2015

lecture 10 UI/UX and Programmatic Design cs : spring 2018

Backends and Databases. Dr. Sarah Abraham

Transcription:

iphone Application Programming Networking RWTH Aachen University WS 2015/16 http://hci.rwth-aachen.de/iphone

Networking on ios There are many ways to network Think about what you need to do first Then select the most appropriate technology Networking operates on binary data All networking is done asynchronously Delegate Blocks 2

Converting to/from NSData NSString (NSString NSData) NSPropertyListSerialization (NSDictionary/NSArray Plist) NSJSONSerialization (NSDictionary/NSArray JSON) NSCoding (Any Object NSData) 3

Networking Technologies GameKit Multipeer Connectivity Air Drop Push Notifications URL Loading System Bonjour Socket Networking 4

GameKit Leaderboards, Achievements, Challenges, In-game Voice Chat 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 5

GameKit Checklist Set up your app in itunes Connect Enable Game Center in your app s capabilities Import GameKit framework import GameKit Authenticate the local player 6

GameKit Authentication var localplayer = GKLocalPlayer.localPlayer() localplayer.authenticatehandler = {(viewcontroller : UIViewController?, error : NSError?) -> Void in if ((viewcontroller)!= nil) { self.presentviewcontroller(viewcontroller, animated: true, completion: ^{ self.didauthenticate }) }else{ self.didauthenticate } } 7

Real-Time Matchmaking Create GKMatchRequest Find Players via GKMatchmaker or GKMatchmakerViewController Matches can be either peer-to-peer or hosted To receive invitations, you need to provide an invitehandler GKMatch to send data senddata to all players func senddata(data: NSData,toPlayers playerids: [String],withDataMode mode: GKMatchSendDataMode) 8

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 9

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

Multipeer Connectivity Connect to and exchange data with nearby devices Uses Wifi, peer-to-peer Wifi, or Bluetooth personal area network 11

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

Multipeer Connectivity Example //set up peerid let mypeerid = MCPeerID(displayName: "uniqueid") lazy var session : MCSession = { let session = MCSession(peer: self.mypeerid, securityidentity: nil, encryptionpreference: MCEncryptionPreference.Required) session.delegate = self return session }() //set up advertising service var serviceadvertiser : MCNearbyServiceAdvertiser func setupadvertiser() { self.serviceadvertiser = MCNearbyServiceAdvertiser(peer: mypeerid, discoveryinfo: nil, servicetype: myservicetype) self.serviceadvertiser.delegate = self self.serviceadvertiser.startadvertisingpeer() } //set up peer browser var browserviewcontroller : MCBrowserViewController func presentbrowser() { self.browserviewcontroller = MCBrowserViewController(serviceType: myservicetype, session: session) browserviewcontroller.delegate = self self.presentbrowser() } 13

Multipeer Connectivity Example (cont.) func senddata(nsdata, topeers: [MCPeerID], withmode: MCSessionSendDataMode) func startstreamwithname("streamname", topeer: MCPeerID) func session(session: MCSession, didreceivedata data: NSData, frompeer peerid: MCPeerID) func session(session: MCSession, didreceivestream stream: NSInputStream, withname streamname: String, frompeer peerid: MCPeerID) -> NSOutputStream 14

Documentation Multipeer Connectivity Framework Reference 15

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

AirDrop Checklist Transfer files between ios and Mac devices Must enable AirDrop on the receiving device 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 17

18

AirDrop Example let activityviewcontroller = UIActivityViewController(activityItems: [image],applicationactivities: []) presentviewcontroller(vc, animated: true, completion: nil) func application(_ app: UIApplication, openurl url: NSURL, options options: [String : AnyObject]) -> Bool 19

Documentation UIActivityViewController Class Reference AirDrop Example Code 20

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

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

Scheduling Local Notifications Example let notification = UILocalNotification() notification.firedate = NSDate(timeIntervalSinceNow: 10.0) notification.alertbody = "Answer Chris" notification.soundname = UILocalNotificationDefaultSoundName notification.applicationiconbadgenumber = 1 notification.userinfo = self.custominfo; UIApplication.sharedApplication().scheduleLocalNotifications(notification) 23

Receiving Local Notifications Example func application(application: UIApplication, didfinishlaunchingwithoptions launchoptions: [NSObject : AnyObject]?) -> Bool { } let notification = launchoptions![uiapplicationlaunchoptionslocalnotificationkey] self.handlenotification() func application(application: UIApplication, didreceivelocalnotification notification: UILocalNotification) { } self.handlenotification() 24

Remote Notifications Register for Remote Notifications Tell UIApplication to register for remote notifications Implement UIApplicationDelegate and send device token to your server Receive a Remote Notifications Similar to local notifications (replace Local with Remote) Send a Remote Notification Implement your own server Request certificate via the Apple ios Developer Portal 25

Register for Remote Notifications Example func application(application: UIApplication, didfinishlaunchingwithoptions launchoptions: [NSObject : AnyObject]?) -> Bool { } let settings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) UIApplication.sharedApplication().registerForRemoteNotifications() func application(application: UIApplication, didregisterforremotenotificationswithdevicetoken devicetoken: NSData) { //send the device token to your server } 26

Receive Remote Notifications Example func application(application: UIApplication, didfinishlaunchingwithoptions launchoptions: [NSObject : AnyObject]?) -> Bool { } let notification = launchoptions![uiapplicationlaunchoptionsremotenotificationkey] self.handlenotification() func application(application: UIApplication, didreceivelocalnotification notification: UILocalNotification) { } self.handlenotification() 27

Sending Remote Notifications Your server Apple Your client s device 28

Device Token 29

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 30

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

Documentation Local and Push Notifications Overview Local and Remote Notification Programming Guide 32

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

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 34

Data Task Example func httpget(imageurl: NSURL!, callback: (UIImage?, String?) -> Void) { //create the data task to download let session = NSURLSession.sharedSession() let task = session.datataskwithurl(imageurl){ (data, response, error) -> Void in if error!= nil { callback(nil, error!.localizeddescription) } else { let result = UIImage(data: data!) callback(result! as UIImage, nil) } } } task.resume() 35

Background Download Task Example func createsession() -> NSURLSession { let identifier = "url_background_session" let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier) let backgroundsession = NSURLSession(configuration: config, delegate: self.delegate, delegatequeue: nil) return backgroundsession } func start() { let backgroundsession = self.createsession() backgroundsession.downloadtaskwithurl(imageurl).resume() } func application(application: UIApplication, handleeventsforbackgroundurlsession identifier: String, completionhandler: () -> Void) { self.createsession() self.completionhandler = completionhandler; } func URLSession(session: NSURLSession, downloadtask:nsurlsessiondownloadtask, didfinishdownloadingtourl location: NSURL) { self.completionhandler(); } 36

Documentation URL Loading System Programming Guide 37

Bonjour Service discovery framework 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 38

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

Bonjour Browser 40

Bonjour Example //setup and start net service let netservice = NSNetService(domain: "local", type: "_myservice._tcp", name: "MyService", port: 8000) netservice.publish() //setup and start net service browser let netservicebrowser = NSNetServiceBrowser() netservicebrowser.delegate = self netservicebrowser.searchforservicesoftype("_myservice._tcp", indomain: "local") func netservicebrowser(browser: NSNetServiceBrowser, didfindservice service: NSNetService, morecoming: Bool) { //handle discovered service } func netservicebrowser(browser: NSNetServiceBrowser, didremoveservice service: NSNetService, morecoming: Bool) { // handle the removed service } 41

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

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

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 44

Socket Networking int socket_connect(const char *host,int port,int timeout){ struct sockaddr_in sa; struct hostent *hp; int sockfd = -1; hp = gethostbyname(host); bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length); sa.sin_family = hp->h_addrtype; sa.sin_port = htons(port); sockfd = socket(hp->h_addrtype, SOCK_STREAM, 0); socket_set_block(sockfd,0); connect(sockfd, (struct sockaddr *)&sa, sizeof(sa));... } 45

Socket Networking CFSocket @asmname("socket_connect") func socket_connect(host:unsafepointer<int8>,port:int32,timeout:int32) -> Int32 //create a socket yourself let socket = CFSocketCreate(kCFAllocatorDefault, 0, SOCK_STREAM, 0, 0, callback, context) //attach the socket to your run loop let source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0) CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kcfrunloopcommonmodes) source.release() 46

Stream Networking Example Connection //Create the connection with CFStreamRef var readstream: Unmanaged<CFReadStream>? var writestream: Unmanaged<CFWriteStream>? CFStreamCreatePairWithSocketToHost(nil, self.serveraddress, self.serverport, &readstream, &writestream) //Proceed with NSStream objects var inputstream: NSInputStream! var outputstream: NSOutputStream! self.inputstream = readstream!.takeretainedvalue() self.outputstream = writestream!.takeretainedvalue() self.inputstream.delegate = self self.outputstream.delegate = self self.inputstream.scheduleinrunloop(nsrunloop.currentrunloop(), formode: NSDefaultRunLoopMode) self.outputstream.scheduleinrunloop(nsrunloop.currentrunloop(), formode: NSDefaultRunLoopMode) self.inputstream.open() self.outputstream.open() 47

Stream Networking Example Handle Stream Events func stream(stream: NSStream, handleevent eventcode: NSStreamEvent) { switch eventcode { case NSStreamEvent.HasBytesAvailable : var buffer = u_char(1024) var length = 0 let inputstream = stream as! NSInputStream length = inputstream.read(&buffer, maxlength: length) if(length > 0) { data.appendbytes(&buffer, length: length) } } } 48

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 49

Documentation Guide to Network Programming Stream Programming Guide CFSocket Reference 50