Connecting Your iOS Apps

Part of the Beginning iOS Programming For Dummies Cheat Sheet

Your iOS application may need to communicate with a variety of other resources. Fortunately, the iOS toolbox has a variety of specialized connections. Pick one up and put it to work.

Opening a browser from your app

Here’s a quick and dirty way of having your app open a web page in a browser. See the following example code:

    - (IBAction)helpViaBrowser: (id) sender{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/Tictactoe"]];

Yes, it is that simple!

Converting an address to a geolocation

Most mapping or location-based apps have a need to convert a text string containing an address or a location to geo-coded coordinates. Luckily the iOS framework provides a built-in geocoder. The following code shows you how to use the geocoder:

…   
NSString *findString = [findTextField text]; // the string is read from a text field
    [myGeocoder geocodeAddressString:findString completionHandler:
                                       ^(NSArray* placemarks, NSError* error){
        MKPlacemark *aPlacemark = [placemarks objectAtIndex:0];
        // aPlacemark contains the geocoded information
        …
    }];
…

Testing for network capabilities

It's a good idea to test what network capabilities are available before your app tries to use the network. Here’s how you can do this test:

    + (void) listCapabilities{
            
       // Testing for the network
       NSString *networkReachability=nil;
            
       Reachability *reachFacade = [Reachability reachabilityForInternetConnection];
       NetworkStatus status = [reachFacade currentReachabilityStatus];
       if (status==NotReachable) networkReachability = @"No network\n";
       else if (status==ReachableViaWiFi) networkReachability = @"Wifi available\n";
       else if (status==ReachableViaWWAN) networkReachability = @"WAN available\n";
       …
       [textVal appendString:networkReachability];
       …
    }

Starting sensors

Before you can read sensed values, you have to start and configure the motion manager. The following code shows you how:

        …
        // Set up the motion manager
        
        self->motionManager = [[CMMotionManager alloc] init];
        self->motionManager.accelerometerUpdateInterval = 1;
        self->motionManager.gyroUpdateInterval = 1;
        self->motionManager. magnetometerUpdateInterval = 1;
        …

However, just starting the motion manager is not enough. You also have to start monitoring the sensors, as shown here:

- (IBAction) startMonitoringSensors:(id)sender{
        [self->motionManager
             startAccelerometerUpdatesToQueue:
                 [NSOperationQueue currentQueue]
                    withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
                        [self handleAccelerationUpdates:accelerometerData.acceleration];
                  }
        ];
        
        [self->motionManager
             startGyroUpdatesToQueue:
                 [NSOperationQueue currentQueue]
                    withHandler:^(CMGyroData *gyroscopeData, NSError *error) {
                        [self handleGyroUpdates:gyroscopeData.rotationRate];
                   }
        ];
        [self->motionManager
             startMagnetometerUpdatesToQueue:
                 [NSOperationQueue currentQueue]
                    withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
                        [self handleMagnetometerUpdates:magnetometerData. magneticField];
                 }
        ];
    }

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

SERIES
Beginning iOS Programming For Dummies Cheat Sheet

Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.