Backend-Kommunikation
HTTP(S)-Kommunikation
-
NSDatarepräsentiert Binärdaten und erlaubt auch das Laden von Daten von URLs:NSURL* url = [NSURL URLWithString:@"http://www.google.de/"]; NSError* error = nil; NSData* data = [NSData dataWithContentsOfURL:url options:0 error:&error]; if (error) { NSLog(@"Error %@, %@", error, [error userInfo]); // ... handle error } else { NSLog(@"Loaded data: %@", data); }
-
Alternativ:
[NSString stringWithContentsOfURL:encoding:error] -
NSURLConnectionerlaubt die Ausführung von asynchronen Requests
Beispiel: Asynchrone Requests mit NSURLConnection 1/2
- (void) loadData {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL* url = [NSURL URLWithString:@"http://www.example.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.connection =
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
}
#pragma mark NSURLConnection delegate methods
- (void) connection:(NSURLConnection *)con didReceiveResponse:(NSURLResponse *)resp {
self.data = [NSMutableData data];
}
Beispiel: Asynchrone Requests mit NSURLConnection 2/2
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)aData {
[self.data appendData:aData];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"URL connection failed with %@, %@", error, [error userInfo]);
self.data = nil;
self.connection = nil;
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// parse data here
self.data = nil;
self.connection = nil;
}
TouchJSON
-
TouchJSON ist ein Objective-C JSON-Parser und -Generator.
-
JSON parsen:
#import "CJSONDeserializer.h" NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
-
JSON generieren:
#import "CJSONSerializer.h" NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"]; NSString *jsonString = [[CJSONSerializer serializer] serializeObject:dictionary];
Alternativen zur Kommunikation / Serialisierung
NSXmlParser: SAX-XML-Parser (Bestandteil vom iPhone-SDK)TouchXML: DOM-XML-ParserProtocol Buffers: Nachrichten-/Protokollbasiertes SerialisierungsformatConnection Kit: Dateitransfer via HTTP, FTP, SFTP, WebDAV, .Mac und zu Amazon S3
Reachability
SCNetworkReachabilityerlaubt die Abfrage und Beobachtung der Netzwerk-Erreichbarkeit des iPhones- Beantwortet die Frage “Können Pakete das iPhone verlassen?”
- Siehe Beispielanwendung ”
Reachability”
Tipps & Tricks: Webserver ad hoc starten
cd webroot/ python /cd/toolbox/scripts/www_here.py
Weiterführende Informationen 1/2
- TouchJSON Howto http://code.google.com/p/touchcode/wiki/TouchJSONHowTo
- TouchXML http://code.google.com/p/touchcode/wiki/TouchXML
- Building an Advanced RSS reader using TouchXML: http://dblog.com.au/general/iphone-sdk-tutorial-building-an-advanced-rss-reader-using-touchxml-part-1/
- Protocol Buffers: http://code.google.com/p/protobuf/
- Objective-C Protocol Buffers for OSX and the iPhone: http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers
- iPhoneWebServicesClient: Showcase for transport formats and libraries: http://github.com/akosma/iPhoneWebServicesClient
Weiterführende Informationen 2/2
- HTTPRiot - Easily Consume REST Resources on the iPhone and OS X: http://alternateidea.com/blog/articles/2009/7/11/introducing-httpriot-easily-consume-rest-resources-on-the-iphone-and-os-x
- Connection Kit: http://opensource.utr-software.com/connection/

