Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. Weather App WeatherPackt > Time for action — defining the Home screen

Time for action — defining the Home screen

Time for action — defining the Home screen Open the AppDelegate.m file in your newly created WeatherPackt application project. Modify the didFinishLaunchingWithOptions method to include the third view controller in the TabBarController. if ([[UIDevicecurrentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { viewController1 = [[FirstViewControlleralloc] initWithNibName:@"FirstViewController_iPhone"bundle:nil]; viewController2 = [[SecondViewControlleralloc] initWithNibName:@"SecondViewController_iPhone"bundle:nil]; viewController3 = [[ThirdViewControlleralloc] initWithNibName:@"ThirdViewController_iPhone"bundle:nil]; } else { viewController1 = [[FirstViewControlleralloc] initWithNibName:@"FirstViewController_iPad"bundle:nil]; viewController2 = [[SecondViewControlleralloc] initWithNibName:@"SecondViewController_iPad"bundle:nil]; viewController3 = [[ThirdViewControlleralloc] initWithNibName:@"ThirdViewController_iPad"bundle:nil]; } In each individual ViewController main file, for example in your SecondViewController.m file, add the following code for the AdMob ads integration in your viewDidLoad method: - (void)viewDidLoad { [superviewDidLoad]; deviceType =@"iPhone"; NSString *model= [[UIDevicecurrentDevice] model]; NSRange range = [model rangeOfString:@"iPhone"]; if(range.location == NSNotFound) { deviceType =@"iPad"; } else { deviceType =@"iPhone"; } responseData = [[NSMutableDataalloc] init]; // AdMob Code Starts // Create a view of the standard size at the bottom of the screen. if([deviceTypeisEqualToString:@"iPhone"]){ bannerAdView = [[GADBannerViewalloc] initWithFrame:CGRectMake(0.0,43.0, GAD_SIZE_320x50.width,GAD_SIZE_320x50.height)]; }else{ bannerAdView = [[GADBannerViewalloc] initWithFrame:CGRectMake(20.0,43.0, GAD_SIZE_728x90.width,GAD_SIZE_728x90.height)]; } bannerAdView.adUnitID = @"xxxxxxxxxxxxxxx "; bannerAdView.rootViewController = self; [self.viewaddSubview:bannerAdView]; // Initiate a generic request to load it with an ad. [bannerAdViewloadRequest:[GADRequestrequest]]; // AdMob Code Ends } Where xxxxxxxxxxxxxxx is your publisher ID from http://admob.com. Based on the device type and the app UI, we adjust the AdMob code accordingly. Time to use the icons for the tab bars in the SecondViewController.m file. Modify the initWithNibName method to include the icon images from http://glyphish.com. Note that you also need to add the glyphish-icons folder to your Xcode project. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = NSLocalizedString(@"Forecast", @"Forecast"); self.tabBarItem.image = [UIImageimageNamed:@"99-umbrella"]; } returnself; } If all goes well, your Home screen should now look as the one shown in the following screenshot: We create a similar layout for the Forecast page in the SecondViewController_iPhone.xib file. Set up a default location To make sure that our app works well in case the user has not allowed the location services or in case the user's location has not been determined or could not be determined, we initiate the app loading process with a default location; in this case, we load San Francisco by using the following code: newLatitude =@"37.33"; newLongitude =@"-122.03"; locationManager = [[CLLocationManageralloc]init]; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.distanceFilter = 1000.0f; locationManager.delegate = self; userLocation = [[CLLocationalloc] initWithLatitude:[newLatitudedoubleValue] longitude:[newLongitudedoubleValue]]; As the user's new location is detected, we update the userLocation variable to always hold the updated values. Formatting the Weather API for display As we saw in the previous examples, we used the didStartElement, foundCharacters , didEndElement ,and parserDidEndDocument methods of the XMLParser, to fetch and display the weather information. However, the logic we used before was not perfect. It assumed that the XML response is streamlined, and each method calls the subsequent method sequentially. But in the real-world scenario, this would be a bit different. Depending upon your NSURL and network connections, the foundCharacters method of the NSXMLParser class can be called multiple times for the same XML tag. This would create problems in our earlier examples, but we will refactor the code to take care of this issue as well. In our foundCharacters method, we keep appending the string response received to a temporary variable until the didEndElementmethod confirms the end of the XML tag, and resets the temporary variable. -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ if (!currentXMLValue) { currentXMLValue = [[NSMutableStringalloc]init]; } [currentXMLValueappendString:string]; } The settings page We want our WeatherPackt app to have a settings page, registered in the main Settings app of your iOS device, which can be used to flip the display from Celsius to Fahrenheit, and vice versa. We do this by adding a Settings bundle to our application. The Settings bundle helps us manage preferences from within the Settings application. The NSUserDefaults class is used to access the settings/preference values. The type of settings we can incorporate in our Settings page could be the following: A sliderA text fieldA TitleA toggle switch (we will use this for Celsius display on/off)A groupA child paneA multi value More details can be found at Apple's rich documentation available at: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/Preferences/Preferences.html. We will create a simple settings page for our app. Using the File | New File option, and selecting the Resources tab from the Modal window, we add the setting bundle, name the file as Settings Bundle, and save it in your project. The Settings Bundle is just a collection of two files: Root.plist and Root.string. Double-click on the Root.plist file in Xcode, and open the Settings Property List editor, where you can define the Preference items for your Settings Page. Modify the Root.plist file to look as follows: We use the Toggle Switch with Identifier as enabled_preference, to allow the users to switch the Celsius display on/off. This identifier is also used in our application to fetch the current value of its state, the code for which is as follows: // Read the Settings NSUserDefaults *settings = [NSUserDefaultsstandardUserDefaults]; celsiusValue= [settings stringForKey:@"enabled_preference"]; // End of Read Settings When we run the application and open the main iOS device's Settings screen, we should see the following option for WeatherPackt: And its associated Settings: The full code for the WeatherPackt app can be found on the book's website: project titled WeatherPackt. Have a go hero — adding the Alerts page to WeatherPackt We showed you how to build a Live Weather and Forecast page. The app framework also supports a third view for displaying Weather Alerts. We have made provisions for both the iPhone and iPad View in the application. Using your knowledge of what you have learnt so far, complete the third page. Feel free to share the code; who knows your page could end up in the final app and on iTunes !! Here is how the WeatherPackt app should look, when you run it in the iOS simulator:

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint