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

3. Using Location in your iOS Apps — Cor... > Time for action — boundary monitorin...

Time for action — boundary monitoring with Location Manager

Time for action — boundary monitoring with Location Manager Let's now move to the next Location Service provided by the iOS Location Manager — Region/Boundary Monitoring. We use the CLRegion class and its method, namely, didEnterRegion to monitor whether the user's position falls in the boundary. Open the Hello Location project. In the Hello_locationViewController.h file, add the CLRegion definition as follows: CLRegion *boundary; In the Hello_LocationViewController.m file, we create a circular boundary/region centered around San Francisco geo co-ordinates, with a radius of 1000 meters. We initialize the boundary variable as the following: CLLocationCoordinate2D regionCords = CLLocationCoordinate2DMake (37.78 , -122.408); boundary = [[CLRegion alloc]initCircularRegionWithCenter:regionCords radius:1000.0f identifier:@"San Francisco"]; Next in the viewDidLoad method, we tell the Location Manager to start monitoring the region using the startMonitoringForRegion method as follows: [locMgr startMonitoringForRegion:boundary]; To detect whether the device has entered the defined region, we implement the didEnterRegion method and alert the user in case he has entered the region (San Francisco boundary defined earlier) - (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"You Entered San Francisco" message:@"Welcome to San Francisco" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } When you run the application in the simulator and use location simulation to pass San Francisco co-ordinates to the application, you get an output as shown in the following screenshot: What just happened? We created a region of 1000 meters around the San Francisco Geo co-ordinates and monitored the user's device location against this. As soon as the user enters the specific region, an alert is displayed, welcoming the user to San Francisco. For best results, start the application by setting the Product | Setting | User Location to say Moscow or Mumbai and then when running your application on the iOS Simulator or the iPhone, go to Product | Debug | Simulate Location and select San Francisco. You should see the alert on your device immediately. The didEnterRegion and didExitRegion method of the CLRegion class are used to detect if the user's iPhone enters or leaves the region. This is the simplest form of Geo Fencing that can be accomplished by core iOS APIs. Have a go hero — remembering a user's location with Core Data As we have discussed so far, Location calls on the iOS device can be taxing on the battery. It is a good programming technique to store the user's last position on the device. It might be an application design requirement as well to store a user's location history, in case you are building a Travel Trip application or a Travelling Tour application. Core Data allows iOS developers to store, retrieve, and manage their application's data in an object-oriented manner. Think of it as an Object-relational mapping (ORM) for iOS development. Core Data is based on the Model View Controller software development methodology. Let's look at the key building blocks of Core Data: Managed-object model: Similar to "Tables" in an RDBMS SchemaManaged-object context: Connector between the developer and the managed objectsPersistent object stores: A single File or External Data storeManaged object: A "Row" in a table (in the RDBMS context) iOS 5 brings some new features in Core Data, namely: iCloud integrationIncremental storeData protection (with encryption)ConcurrencyUIManagedDocument A complete analysis of Core Data framework is beyond the scope of this book. However, let's use the iOS Core Data framework to store the user's location history on the iOS device. Try out building an application that uses Core Data to store the Location info.

  

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