SlideShare uma empresa Scribd logo
1 de 14
Baixar para ler offline
iPhone	
  Development:	
  
       H"p	
  Connec*on	
  

             Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
H>p	
  Connec@on	
  
•  NSUrlConnection
   –  Easiest	
  way	
  to	
  download	
  the	
  contents	
  of	
  an	
  URL	
  
•  Crea@ng	
  connec@on	
  
•  Canceling	
  connec@on	
  
•  Collec@on	
  of	
  delegate	
  methods	
  for	
  controlling	
  
   the	
  connec@on	
  
Crea@ng	
  a	
  Connec@on	
  
// Create the request
NSURLRequest *theRequest=
     [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.tamk.fi"]
                      cachePolicy: NSURLRequestUseProtocolCachePolicy
                  timeoutInterval: 60.0];

// Create the connection with the request
// and start loading the data
NSURLConnection *theConnection=
     [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if ( theConnection )
{
    // Create the NSMutableData that will hold
    // the received data (A wrapper for byte buffer)
    // receivedData is declared as a method instance elsewhere .
    // Creates empty data wrapper.
    receivedData = [ [NSMutableData data] retain ];
} else
{
    // inform the user that the download could not be made
}
Delegate?	
  
// Create the connection with the request
// and start loading the data
NSURLConnection *theConnection=
     [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

//     Now "self" must conform to the delegate methods that are:
//     - (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse *)response
//     - (void) connection:(NSURLConnection*) connection didFailWithError:(NSError *) error
//     - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data
//     - (void) connectionDidFinishLoading:(NSURLConnection *) connection
	
  
// These methods are are now called automatically!
URLConnec@on	
  class	
  
@interface URLConnection : NSObject
{
    // Contents of the URL
    NSMutableData* receivedData;
    NSURLConnection* theConnection;
}
// Make the connection
- (void) connect: (NSString* url);

// Cancel the connection
- (void) cancel;

// Delegate methods
- (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse*)
    response
- (void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error
- (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data
- (void) connectionDidFinishLoading:(NSURLConnection*) connection
Connec@ng	
  
- (void) connect:(NSString*) url
{
    // create the request
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];

    // create the connection with the request
    // and start loading the data
    self.theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
         receivedData=[[NSMutableData data] retain];
    }
    else
    {
         // Connection failed
         // Give error message
    }
}
Receiving	
  data	
  
- (void) connection:(NSURLConnection*) connection
   didReceiveData:(NSData*) data
{
    [receivedData appendData:data];
}
When	
  connec@on	
  is	
  finished	
  
- (void) connectionDidFinishLoading:(NSURLConnection *) connection
{
    // Create byte array from the received data
    unsigned char byteBuffer[[receivedData length]];
    [receivedData getBytes:byteBuffer];

    NSString *temp = [[NSString alloc]
                      initWithBytes:byteBuffer
                             length:[receivedData length]
                           encoding:4];

    // Do something with the data.. For example
    // make a call back method for the viewController

    [temp release];
    [connection release];
    [receivedData release];
    self.theConnection = nil;
}
When	
  failes..	
  
- (void) connection:(NSURLConnection *) connection
   didFailWithError:(NSError *) error
{
   // Error handling
}
Canceling	
  
- (void) cancel
{
    if ( self.theConnection )
    {
        [self.theConnection cancel];
        [self.theConnection release];
    }
}
Usage	
  
[URLConnection *myConn = [[URLConnection alloc] init];
[myConn connect:@"http://www.tamk.fi"];
CallBack	
  Methods?	
  
@interface URLConnection : NSObject
{
    // Contents of the URL
    NSMutableData* receivedData;
    NSURLConnection* theConnection;
    MyViewController* myViewController;
}

- (id) initWithHost:(MyViewController*) host
{
    self = [super init];
    self.myViewController = host;
    self.theConnection    = nil;
    return self;
}

// Make the connection
- (void) connect: (NSString* url);

// Cancel the connection
- (void) cancel;

// Delegate methods
- (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse*)response
- (void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error
- (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data
- (void) connectionDidFinishLoading:(NSURLConnection*) connection
Usage	
  
// We are now in some ViewController class
[URLConnection *myConn = [[URLConnection alloc] initWithHost: self];
[myConn connect:@"http://www.tamk.fi"];
When	
  connec@on	
  is	
  finished	
  
- (void) connectionDidFinishLoading:(NSURLConnection *) connection
{
    unsigned char byteBuffer[[receivedData length]];

    [receivedData getBytes:byteBuffer];

    NSString *temp = [[NSString alloc]
                      initWithBytes:byteBuffer
                             length:[receivedData length]
                           encoding:4];

    // Do something with the data.. For example
    // make a call back method for the viewController
    [myViewController setThisTextToTextView: temp];

    [temp release];
    [connection release];
    [receivedData release];
    self.theConnection = nil;
}

Mais conteúdo relacionado

Mais procurados

C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT IIIMinu Rajasekaran
 
Mobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi LuhurMobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi LuhurRiza Fahmi
 
The love child of Android and .NET: App development with Xamarin
The love child of Android and .NET: App development with XamarinThe love child of Android and .NET: App development with Xamarin
The love child of Android and .NET: App development with XamarinLorenz Cuno Klopfenstein
 
Non blocking io with netty
Non blocking io with nettyNon blocking io with netty
Non blocking io with nettyZauber
 
Introducing Docker to Mac Management – Nick McSpadden
Introducing Docker to Mac Management – Nick McSpaddenIntroducing Docker to Mac Management – Nick McSpadden
Introducing Docker to Mac Management – Nick McSpaddenmacbrained
 
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)I Goo Lee
 
Websockets, Ruby y Pusher Webprendedor 2010
Websockets, Ruby y Pusher Webprendedor 2010Websockets, Ruby y Pusher Webprendedor 2010
Websockets, Ruby y Pusher Webprendedor 2010Ismael Celis
 
Node.js and websockets intro
Node.js and websockets introNode.js and websockets intro
Node.js and websockets introkompozer
 
Netty: asynchronous data transfer
Netty: asynchronous data transferNetty: asynchronous data transfer
Netty: asynchronous data transferVictor Cherkassky
 
Java осень 2012 лекция 2
Java осень 2012 лекция 2Java осень 2012 лекция 2
Java осень 2012 лекция 2Technopark
 
Async task
Async taskAsync task
Async taskLwp Xd
 
Hands On Intro to Node.js
Hands On Intro to Node.jsHands On Intro to Node.js
Hands On Intro to Node.jsChris Cowan
 
Tornado web
Tornado webTornado web
Tornado webkurtiss
 
Couch to OpenStack: Neutron (Quantum) - August 13, 2013 Featuring Sean Winn
Couch to OpenStack: Neutron (Quantum) - August 13, 2013 Featuring Sean WinnCouch to OpenStack: Neutron (Quantum) - August 13, 2013 Featuring Sean Winn
Couch to OpenStack: Neutron (Quantum) - August 13, 2013 Featuring Sean WinnTrevor Roberts Jr.
 
[213] ethereum
[213] ethereum[213] ethereum
[213] ethereumNAVER D2
 
Performance from the user's perspective
Performance from the user's perspectivePerformance from the user's perspective
Performance from the user's perspectiveorangecoding
 

Mais procurados (19)

C# Application program UNIT III
C# Application program UNIT IIIC# Application program UNIT III
C# Application program UNIT III
 
Mobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi LuhurMobile Programming - Network Universitas Budi Luhur
Mobile Programming - Network Universitas Budi Luhur
 
The love child of Android and .NET: App development with Xamarin
The love child of Android and .NET: App development with XamarinThe love child of Android and .NET: App development with Xamarin
The love child of Android and .NET: App development with Xamarin
 
Non blocking io with netty
Non blocking io with nettyNon blocking io with netty
Non blocking io with netty
 
Introducing Docker to Mac Management – Nick McSpadden
Introducing Docker to Mac Management – Nick McSpaddenIntroducing Docker to Mac Management – Nick McSpadden
Introducing Docker to Mac Management – Nick McSpadden
 
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
 
Phd3
Phd3Phd3
Phd3
 
RegistryModClass
RegistryModClassRegistryModClass
RegistryModClass
 
Websockets, Ruby y Pusher Webprendedor 2010
Websockets, Ruby y Pusher Webprendedor 2010Websockets, Ruby y Pusher Webprendedor 2010
Websockets, Ruby y Pusher Webprendedor 2010
 
Node.js and websockets intro
Node.js and websockets introNode.js and websockets intro
Node.js and websockets intro
 
Netty: asynchronous data transfer
Netty: asynchronous data transferNetty: asynchronous data transfer
Netty: asynchronous data transfer
 
Java осень 2012 лекция 2
Java осень 2012 лекция 2Java осень 2012 лекция 2
Java осень 2012 лекция 2
 
Async task
Async taskAsync task
Async task
 
Hands On Intro to Node.js
Hands On Intro to Node.jsHands On Intro to Node.js
Hands On Intro to Node.js
 
Tornado web
Tornado webTornado web
Tornado web
 
Couch to OpenStack: Neutron (Quantum) - August 13, 2013 Featuring Sean Winn
Couch to OpenStack: Neutron (Quantum) - August 13, 2013 Featuring Sean WinnCouch to OpenStack: Neutron (Quantum) - August 13, 2013 Featuring Sean Winn
Couch to OpenStack: Neutron (Quantum) - August 13, 2013 Featuring Sean Winn
 
[213] ethereum
[213] ethereum[213] ethereum
[213] ethereum
 
Performance from the user's perspective
Performance from the user's perspectivePerformance from the user's perspective
Performance from the user's perspective
 
Couch to open_stack_keystone
Couch to open_stack_keystoneCouch to open_stack_keystone
Couch to open_stack_keystone
 

Semelhante a iPhone: Http Connection

Beginning icloud development - Cesare Rocchi - WhyMCA
Beginning icloud development - Cesare Rocchi - WhyMCABeginning icloud development - Cesare Rocchi - WhyMCA
Beginning icloud development - Cesare Rocchi - WhyMCAWhymca
 
Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesMaksym Davydov
 
iPhone and Rails integration
iPhone and Rails integrationiPhone and Rails integration
iPhone and Rails integrationPaul Ardeleanu
 
Chapter 27 Networking - Deitel & Deitel
Chapter 27 Networking - Deitel & DeitelChapter 27 Networking - Deitel & Deitel
Chapter 27 Networking - Deitel & DeitelCSDeptSriKaliswariCo
 
Statying Alive - Online and OFfline
Statying Alive - Online and OFflineStatying Alive - Online and OFfline
Statying Alive - Online and OFflineErik Hellman
 
Working with AFNetworking
Working with AFNetworkingWorking with AFNetworking
Working with AFNetworkingwaynehartman
 
High Performance Core Data
High Performance Core DataHigh Performance Core Data
High Performance Core DataMatthew Morey
 
Web CrawlersrcedusmulylecrawlerController.javaWeb Crawler.docx
Web CrawlersrcedusmulylecrawlerController.javaWeb Crawler.docxWeb CrawlersrcedusmulylecrawlerController.javaWeb Crawler.docx
Web CrawlersrcedusmulylecrawlerController.javaWeb Crawler.docxcelenarouzie
 
Effective iOS Network Programming Techniques
Effective iOS Network Programming TechniquesEffective iOS Network Programming Techniques
Effective iOS Network Programming TechniquesBen Scheirman
 
Advanced data access with Dapper
Advanced data access with DapperAdvanced data access with Dapper
Advanced data access with DapperDavid Paquette
 
RESTfull with RestKit
RESTfull with RestKitRESTfull with RestKit
RESTfull with RestKitTaras Kalapun
 
The Open Web and what it means
The Open Web and what it meansThe Open Web and what it means
The Open Web and what it meansRobert Nyman
 
Sitecore 7: A developers quest to mastering unit testing
Sitecore 7: A developers quest to mastering unit testingSitecore 7: A developers quest to mastering unit testing
Sitecore 7: A developers quest to mastering unit testingnonlinear creations
 

Semelhante a iPhone: Http Connection (20)

I os 13
I os 13I os 13
I os 13
 
Beginning icloud development - Cesare Rocchi - WhyMCA
Beginning icloud development - Cesare Rocchi - WhyMCABeginning icloud development - Cesare Rocchi - WhyMCA
Beginning icloud development - Cesare Rocchi - WhyMCA
 
Android Networking
Android NetworkingAndroid Networking
Android Networking
 
Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile Devices
 
iPhone and Rails integration
iPhone and Rails integrationiPhone and Rails integration
iPhone and Rails integration
 
Web sockets in Java
Web sockets in JavaWeb sockets in Java
Web sockets in Java
 
Chapter 27 Networking - Deitel & Deitel
Chapter 27 Networking - Deitel & DeitelChapter 27 Networking - Deitel & Deitel
Chapter 27 Networking - Deitel & Deitel
 
Statying Alive - Online and OFfline
Statying Alive - Online and OFflineStatying Alive - Online and OFfline
Statying Alive - Online and OFfline
 
Working with AFNetworking
Working with AFNetworkingWorking with AFNetworking
Working with AFNetworking
 
Moar tools for asynchrony!
Moar tools for asynchrony!Moar tools for asynchrony!
Moar tools for asynchrony!
 
High Performance Core Data
High Performance Core DataHigh Performance Core Data
High Performance Core Data
 
iOS5 NewStuff
iOS5 NewStuffiOS5 NewStuff
iOS5 NewStuff
 
Web CrawlersrcedusmulylecrawlerController.javaWeb Crawler.docx
Web CrawlersrcedusmulylecrawlerController.javaWeb Crawler.docxWeb CrawlersrcedusmulylecrawlerController.javaWeb Crawler.docx
Web CrawlersrcedusmulylecrawlerController.javaWeb Crawler.docx
 
Effective iOS Network Programming Techniques
Effective iOS Network Programming TechniquesEffective iOS Network Programming Techniques
Effective iOS Network Programming Techniques
 
Advanced data access with Dapper
Advanced data access with DapperAdvanced data access with Dapper
Advanced data access with Dapper
 
RESTfull with RestKit
RESTfull with RestKitRESTfull with RestKit
RESTfull with RestKit
 
Web sockets Introduction
Web sockets IntroductionWeb sockets Introduction
Web sockets Introduction
 
Concurrent networking - made easy
Concurrent networking - made easyConcurrent networking - made easy
Concurrent networking - made easy
 
The Open Web and what it means
The Open Web and what it meansThe Open Web and what it means
The Open Web and what it means
 
Sitecore 7: A developers quest to mastering unit testing
Sitecore 7: A developers quest to mastering unit testingSitecore 7: A developers quest to mastering unit testing
Sitecore 7: A developers quest to mastering unit testing
 

Mais de Jussi Pohjolainen

libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferencesJussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationJussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDXJussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript DevelopmentJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDXJussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesJussi Pohjolainen
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 

Mais de Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: User Input
libGDX: User InputlibGDX: User Input
libGDX: User Input
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
 

Último

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 

Último (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 

iPhone: Http Connection

  • 1. iPhone  Development:   H"p  Connec*on   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2. H>p  Connec@on   •  NSUrlConnection –  Easiest  way  to  download  the  contents  of  an  URL   •  Crea@ng  connec@on   •  Canceling  connec@on   •  Collec@on  of  delegate  methods  for  controlling   the  connec@on  
  • 3. Crea@ng  a  Connec@on   // Create the request NSURLRequest *theRequest= [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.tamk.fi"] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0]; // Create the connection with the request // and start loading the data NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if ( theConnection ) { // Create the NSMutableData that will hold // the received data (A wrapper for byte buffer) // receivedData is declared as a method instance elsewhere . // Creates empty data wrapper. receivedData = [ [NSMutableData data] retain ]; } else { // inform the user that the download could not be made }
  • 4. Delegate?   // Create the connection with the request // and start loading the data NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; // Now "self" must conform to the delegate methods that are: // - (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse *)response // - (void) connection:(NSURLConnection*) connection didFailWithError:(NSError *) error // - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data // - (void) connectionDidFinishLoading:(NSURLConnection *) connection   // These methods are are now called automatically!
  • 5. URLConnec@on  class   @interface URLConnection : NSObject { // Contents of the URL NSMutableData* receivedData; NSURLConnection* theConnection; } // Make the connection - (void) connect: (NSString* url); // Cancel the connection - (void) cancel; // Delegate methods - (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse*) response - (void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data - (void) connectionDidFinishLoading:(NSURLConnection*) connection
  • 6. Connec@ng   - (void) connect:(NSString*) url { // create the request NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; // create the connection with the request // and start loading the data self.theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if( theConnection ) { receivedData=[[NSMutableData data] retain]; } else { // Connection failed // Give error message } }
  • 7. Receiving  data   - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data { [receivedData appendData:data]; }
  • 8. When  connec@on  is  finished   - (void) connectionDidFinishLoading:(NSURLConnection *) connection { // Create byte array from the received data unsigned char byteBuffer[[receivedData length]]; [receivedData getBytes:byteBuffer]; NSString *temp = [[NSString alloc] initWithBytes:byteBuffer length:[receivedData length] encoding:4]; // Do something with the data.. For example // make a call back method for the viewController [temp release]; [connection release]; [receivedData release]; self.theConnection = nil; }
  • 9. When  failes..   - (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error { // Error handling }
  • 10. Canceling   - (void) cancel { if ( self.theConnection ) { [self.theConnection cancel]; [self.theConnection release]; } }
  • 11. Usage   [URLConnection *myConn = [[URLConnection alloc] init]; [myConn connect:@"http://www.tamk.fi"];
  • 12. CallBack  Methods?   @interface URLConnection : NSObject { // Contents of the URL NSMutableData* receivedData; NSURLConnection* theConnection; MyViewController* myViewController; } - (id) initWithHost:(MyViewController*) host { self = [super init]; self.myViewController = host; self.theConnection = nil; return self; } // Make the connection - (void) connect: (NSString* url); // Cancel the connection - (void) cancel; // Delegate methods - (void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse*)response - (void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data - (void) connectionDidFinishLoading:(NSURLConnection*) connection
  • 13. Usage   // We are now in some ViewController class [URLConnection *myConn = [[URLConnection alloc] initWithHost: self]; [myConn connect:@"http://www.tamk.fi"];
  • 14. When  connec@on  is  finished   - (void) connectionDidFinishLoading:(NSURLConnection *) connection { unsigned char byteBuffer[[receivedData length]]; [receivedData getBytes:byteBuffer]; NSString *temp = [[NSString alloc] initWithBytes:byteBuffer length:[receivedData length] encoding:4]; // Do something with the data.. For example // make a call back method for the viewController [myViewController setThisTextToTextView: temp]; [temp release]; [connection release]; [receivedData release]; self.theConnection = nil; }