some_id
some_id

Reputation: 29896

Writing to a plist in a LocationManager method

I am trying to write a latitude value and longitude value to a plist inside a LocationManager method.

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
     NSString *latitude = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
     NSString *longitude = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];

     NSString *filePath = [[NSBundle mainBundle] pathForResource:
                      @"PlayerData" ofType:@"plist"];

     NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

     [plistDict setValue:latitude forKey:@"MyLatitude"];
     [plistDict setValue:longitude forKey:@"MyLongitude"];

     [plistDict writeToFile:filePath atomically: YES];

     myLatitude.text = latitude;
     myLongitude.text = longitude;

    NSLog(@"Location: %@", [newLocation description]);

}

For some reason it never writes the value to the plist, but prints out the values correctly.

NSLog(@"Lat i s:%@ Lon is :%@", latitude, longitude);

What would cause this not to update/write to the plist?

Thanks

Upvotes: 0

Views: 224

Answers (2)

Jim
Jim

Reputation: 73966

iOS applications are sandboxed, so you can't write to the application bundle. You need to write to one of these directories. Which one you need to write to depends on what kind of data it is.

Upvotes: 3

user467105
user467105

Reputation:

You can't write to the main bundle (may work in simulator but not device).

Try saving the file to the Documents folder instead.

Upvotes: 2

Related Questions