CS 696 Mobile Phone Application DevelopmentFall Semester, 2010Doc 19 Network part 1Nov 4, 2010Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.References2URL Loading System Programming Guide, Apple documentation, http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.htmlEvent-Driven XML Programming Guide, Apple documentation, http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.htmliPhone Programming: The Big Nerd Ranch Guide, Conway & Hillegass,Big Nerd Ranch, 2010, Chapter 21 Web ServicesMore iPhone 3 Development: Tackling iPhone SDK3, Mark & LaMarche, Apress, 2009, Chapter 10 Working with Data from the WebAccessing Data from network3Web pages (UIWebView)URLWeb servicesGameKit & BluetoothBonjourSocket ProgrammingDisplaying Web pages4UIWebViewDisplays pageDoes most of the workNSURLURL to loadNSURLRequestCachingtimeoutHttp request propertiesUIWebViewDelegateInformation about loading of pageView Set up5@interface WebViewViewController : UIViewController <UIWebViewDelegate>{}IBOutlet UIWebView *webpage;IBOutlet UIButton *forwardButton;IBOutlet UIButton *backButton;- (IBAction) back;- (IBAction) showPage;- (IBAction) forward;@endStarting 6- (void)viewDidLoad { [super viewDidLoad]; backButton.hidden = YES; forwardButton.hidden = YES;}- (IBAction) showPage { webpage.delegate = self; [webpage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.eli.sdsu.edu/"]]];}UIWebViewDelegate Methods7- (void)webViewDidStartLoad:(UIWebView *)webView { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;}- (void)webViewDidFinishLoad:(UIWebView *)webView { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; [self hideShowNavigationButtons];}Showing network trafficUIWebViewDelegate Method8- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; // report the error inside the webview NSString* errorString = [NSString stringWithFormat: @"<html><center><font size=+6 color='red'>An error occurred:<br>%@</font></center></html>", error.localizedDescription]; [webpage loadHTMLString:errorString baseURL:nil]; [self hideShowNavigationButtons];}Loading a stringNavigation9- (IBAction) back { [webpage goBack]; [self hideShowNavigationButtons];}- (IBAction) forward { [webpage goForward]; [self hideShowNavigationButtons];}- (void) hideShowNavigationButtons { backButton.hidden = !webpage.canGoBack; forwardButton.hidden = !webpage.canGoForward;}Dealloc10- (void)dealloc { [super dealloc]; webpage.delegate = nil; [webpage release];}You have to set the delegate to nil before releasing itURL11ftp://http://https://file:///Supportsproxy serversAuthenticationCookiesSynchronousAsynchronousMajor Classes12NSURLURL to loadNSURLRequestCachingtimeoutHttp request propertiesNSURLConnectionHandles connection with serverNSURLConnection delegate methodsHandle asynchronous reading of dataNSDataStore data until done readingAsynchronous Example13Connect to ServerRead Data asynchronouslyDisplay resulting dataAsynchronous Example14@interface URLConnectionExampleViewController : UIViewController {}property - NSMutableData *receivedData;property - IBOutlet UITextView * textView;property - IBOutlet UIActivityIndicatorView *spinner;- (IBAction) fetchUrl;@endStarting the connection15- (IBAction) fetchUrl { [spinner startAnimating]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.eli.sdsu.edu/"]]; NSURLConnection *connection =[[NSURLConnection alloc] initWithRequest:request delegate:self]; if (connection) { NSMutableData *data = [[NSMutableData alloc] init]; self.receivedData=data; [data release]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error connecting to remote server" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; } [request release];}If the connection is nil there was some problem connecting to the server.receivedData is an instance variable that holds the data until reading is done.Reading the data16- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [receivedData setLength:0];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data];}depending on the connection and size of the data connection:didReceiveData: may be called many times before all the data is readWhen done reading data17- (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *payloadAsString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; textView.text = payloadAsString; [payloadAsString release]; [connection release]; self.receivedData = nil; [spinner stopAnimating];}connectionDidFinishLoading: is called when the connection is done reading the dataWhen there is an error18- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [connection release]; self.receivedData = nil; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Connection failed! %@", [error localizedDescription]] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; [spinner stopAnimating];}Nice example but 19Displaying html as text not very usefulhtml is not useful when we want dataWeb Services & Rest20Basic IdeaURL - request for dataRequest sent to serverResponse contains data requestedSOAPComplexRestSimplerReturns XML or JSONGeocoding
View Full Document