Checking the Network and Creating a Connection in Objective-C

Creator ECC200 in Objective-C Checking the Network and Creating a Connection

Listing 7-5. Checking the Network and Creating a Connection
DataMatrix Encoder In Objective-C
Using Barcode generator for iPhone Control to generate, create Data Matrix 2d barcode image in iPhone applications.
www.OnBarcode.com
Universal Product Code Version A Drawer In Objective-C
Using Barcode encoder for iPhone Control to generate, create UPC A image in iPhone applications.
www.OnBarcode.com
- (void)loadPredictionXML:(id<BARTPredictionLoaderDelegate>)delegate { _delegate = delegate; // Load the predictions XML from BART's web site // Make sure that bart.gov is reachable using the current connection SCNetworkReachabilityFlags flags; SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [@"www.bart.gov" UTF8String]); SCNetworkReachabilityGetFlags(reachability, &flags); // The reachability flags are a bitwise set of flags // that contain the information about // connection availability BOOL reachable = ! (flags & kSCNetworkReachabilityFlagsConnectionRequired); NSURLConnection *conn; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.bart.gov/dev/eta/bart_eta.xml"]]; if ([NSURLConnection canHandleRequest:request] && reachable) { conn = [NSURLConnection connectionWithRequest:request delegate:self]; if (conn) { self.predictionXMLData = [NSMutableData data]; } } }
Making UCC - 12 In Objective-C
Using Barcode generator for iPhone Control to generate, create UCC-128 image in iPhone applications.
www.OnBarcode.com
Barcode Creator In Objective-C
Using Barcode creator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
5. NSURLConnection s connectionWithRequest method also takes a delegate argument. In this case, we ll set the delegate to self, so that we can implement the connection s delegate methods right here in the BARTPredictionLoader class. NSURLConnection has several delegate methods, three of which we ll implement: didReceiveResponse, didReceiveData, and connectionDidFinishLoading. The comments in Listing 7-6 explain how each of the delegate methods works, while Figure 7-19 shows the order in which these delegate methods are called.
Print Barcode In Objective-C
Using Barcode generator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Creating Quick Response Code In Objective-C
Using Barcode encoder for iPhone Control to generate, create Quick Response Code image in iPhone applications.
www.OnBarcode.com
CHAPTER 7: Going the Routesy Way with Core Location, XML, and SQLite
Print DataMatrix In Objective-C
Using Barcode generator for iPhone Control to generate, create ECC200 image in iPhone applications.
www.OnBarcode.com
Paint EAN / UCC - 8 In Objective-C
Using Barcode drawer for iPhone Control to generate, create EAN-8 Supplement 2 Add-On image in iPhone applications.
www.OnBarcode.com
Listing 7-6. The NSURLConnection s Delegate didReceiveResponse Method
Data Matrix Recognizer In Visual Basic .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
ECC200 Drawer In Objective-C
Using Barcode generation for iPhone Control to generate, create Data Matrix 2d barcode image in iPhone applications.
www.OnBarcode.com
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response { // didReceiveResponse is called at the beginning of the request when // the connection is ready to receive data. We set the length to zero // to prepare the array to receive data [self.predictionXMLData setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Each time we receive a chunk of data, we'll appeend it to the // data array. [self.predictionXMLData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // When the data has all finished loading, we set a copy of the // loaded data for us to access. This will allow us to not worry about // whether a load is already in progress when accessing the data. self.lastLoadedPredictionXMLData = [self.predictionXMLData copy]; // Make sure the _delegate object actually has the xmlDidFinishLoading // method, and if it does, call it to notify the delegate that the // data has finished loading. if ([_delegate respondsToSelector:@selector(xmlDidFinishLoading)]) { [_delegate xmlDidFinishLoading]; } }
Make PDF417 In .NET
Using Barcode generation for .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications.
www.OnBarcode.com
Printing UCC-128 In VB.NET
Using Barcode creation for VS .NET Control to generate, create GS1-128 image in VS .NET applications.
www.OnBarcode.com
More data to be loaded...
GS1 - 12 Generator In Objective-C
Using Barcode creator for iPad Control to generate, create UCC - 12 image in iPad applications.
www.OnBarcode.com
Printing Code 39 Extended In Visual Studio .NET
Using Barcode encoder for .NET framework Control to generate, create Code 39 Extended image in .NET framework applications.
www.OnBarcode.com
didReceiveResponse
UPC-A Supplement 2 Creation In Visual Basic .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create UPC-A Supplement 5 image in .NET framework applications.
www.OnBarcode.com
PDF 417 Creation In Java
Using Barcode generation for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
didReceiveData
UCC - 12 Maker In None
Using Barcode encoder for Online Control to generate, create EAN / UCC - 14 image in Online applications.
www.OnBarcode.com
Creating 2D Barcode In Visual Basic .NET
Using Barcode generator for VS .NET Control to generate, create 2D Barcode image in .NET framework applications.
www.OnBarcode.com
connectionDidFinishLoading
Painting Matrix Barcode In C#
Using Barcode maker for .NET Control to generate, create 2D image in .NET framework applications.
www.OnBarcode.com
Make Barcode In Java
Using Barcode creation for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Data Array
Figure 7-19. NSURLConnection s delegate methods
CHAPTER 7: Going the Routesy Way with Core Location, XML, and SQLite
Finally, you ll need to set up a singleton instance of BARTPredictionLoader so that you can easily call it from anywhere in your application s code (see Listing 7-7). The @synchronized block around the initialization of the prediction loader ensures that the instance you create will be thread-safe. Refer to 3 for more detailed information on how threading works. With this class method in place, you ll be able to access the shared instance of the prediction loader from anywhere by simply calling [BARTPredictionLoader sharedBARTPredictionLoader]. For a full explanation of how to properly implement singletons, see Apple s Cocoa Fundamentals Guide:
http://developer.apple.com/documentation/Cocoa/Conceptual/ CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid /TP40002974-CH4-SW32
Listing 7-7. Creating a Shared Instance of BARTPredictionLoader
static BARTPredictionLoader *predictionLoader; + (BARTPredictionLoader*)sharedBARTPredictionLoader { @synchronized(self) { if (predictionLoader == nil) { [[self alloc] init]; } } return predictionLoader; }
Now that we have the ability to load our data on demand, we need to figure out where to use it. Let s look again at the BART feed at http://www.bart.gov/dev/eta/bart_eta.xml. You ll notice that the feed contains predictions for all the stations in one very small, quickto-load XML file. Normally, we would only load predictions for the station that a user selects, but this file contains everything, so it makes the most sense to begin loading the data when our list of stations first loads, so we can be ready with predictions when the user chooses a station. 7. Let s revisit the viewDidLoad code in RootViewController.m. First, we need to keep the user from selecting anything in the table until the predictions are done loading. Then, we ll begin loading the XML. Add #import "BARTPredictionLoader.h" to the imports in RootViewController.m, and then add the following code to the end of viewDidLoad:
self.tableView.userInteractionEnabled = NO; [[BARTPredictionLoader sharedBARTPredictionLoader] loadPredictionXML:self];
Copyright © OnBarcode.com . All rights reserved.