Monday, February 16, 2015

Get User Current Location

Get User Current Location

  1. Import CoreLocation Framework to your Project
  2. #import <CoreLocation/CoreLocation.h> to your .h file
  3. Add CLLocationManagerDelegate to your .h file
  4. Now make object of CLLocationManager class
    e.g. 
    CLLocationManager *locationManager
  5. After this add following code to ViewDidLoad

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];
    locationManager.distanceFilter=kCLDistanceFilterNone;    
    [locationManager startUpdatingLocation];
  6. Add two Delegate method to your .m file

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"Latitude:%f Longitude:%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
        
    [locationManager stopUpdatingLocation];
    }
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        
    }
  7. Add Properties to your info.plist file

    NSLocationAlwaysUsageDescription = location required
    NSLocationWhenInUseUsageDescription = Location is required to find out where you are
I got failure to get location because of not adding Property in Info.plist file.
In new versions of Xcode and iOS this is mandatory.

If you are testing in simulator then you have to put breakPoint before startUpdatingLocation and at the execution time you have to select city manually through Xcode. You can also add .GPX file for same.

Saturday, February 14, 2015

Share via Whatsapp in ios

Share via Whatsapp in ios

There are two method to share via whatsapp
  1. URL scheme
  2. Document Interaction API
If you want to go with URL Scheme
then here is example.

/**Just open whatsapp*/
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://app"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

/** Send Hello World! to any contact/group*/
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

/** Send message to selected contact*/
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://abid?/*Address Book ID*/"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

And if you want to go with Document Interaction API then check whatsapp documentation.