How can I add a UIAlertView that will appear only if successfully saving my coordinates to the text file, thanks so much.
I just updated the post with all the code & wonder if you can see why it is showing the alert view twice.
Thanks.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = (id)self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error); UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location updated: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_LatitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
_LongitudeLabel.text = [NSString stringWithFormat:@"%.6f", currentLocation.coordinate.longitude];
_GPSAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.horizontalAccuracy];
_AltitudeLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.altitude];
_VerticalAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.verticalAccuracy];
}
NSString *newLocString = [NSString stringWithFormat:@"%s%f\n%s%f","Lat=",currentLocation.coordinate.latitude,"Long=",currentLocation.coordinate.longitude];
NSString *path = @"var/mobile/Documents/location.txt";
NSError *error = nil;
// Save string and check for error
if (![newLocString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
NSLog(@"An Error occurred: %@", error.localizedDescription);
} else {
UIAlertView *savedAlert = [[UIAlertView alloc] initWithTitle:@"File saved"message:@"File saved successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[savedAlert show];
}
/* IOS8
else {
// Create the alert itself
UIAlertController *savedAlert = [UIAlertController alertControllerWithTitle:@"File saved" message:@"File saved successfully" preferredStyle:UIAlertControllerStyleAlert];
// Create the "OK" button 'Action'
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
// Add the 'Action' created above, to our alert (otherwise, we won't have any button in it)
[savedAlert addAction:defaultAction];
// Presents the alert
[self presentViewController:savedAlert animated:YES completion:nil];
}
======================================
FOR IOS 7
else {
UIAlertView *savedAlert = [[UIAlertView alloc] initWithTitle:@"File saved"message:@"File saved successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[savedAlert show];
}
*/
// Stop Location Manager
[locationManager stopUpdatingLocation];
}
@end