Sorry this is a bit long but I have been struggling with this for a week now... I have created a location tracking iOS application by modifying this code. The LocationManager files adds a new location to my "LocationArray.plist", which is saved on the device that can be accessed with File Sharing.
The plist looks like this: LocationArray.plist
I made a method that uses a web request and if the requestReply
returns "OK", I want to erase the first item on the plist (Item 0) in LocationArray. Because it is a saved profile, if I call [self.myLocationArrayInPList removeObjectAtIndex:0]
, it registers that it has been deleted, but it is not actually removed from savedProfile
in LocationArray.plist.
- (void)removeLocationFromPList {
NSString *plistName = [NSString stringWithFormat:@"LocationArray.plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docDir, plistName];
NSMutableDictionary *savedProfile = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
for (NSDictionary *frame in _myLocationArrayInPlist) {
NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSNumber *two = [frame objectForKey:@"Latitude"];
NSNumber *three = [frame objectForKey:@"Longitude"];
NSDate *four = [frame objectForKey:@"Time"];
NSArray *keys = [_myLocationDictInPlist allKeys];
NSString *latitude = [two stringValue];
NSString *longitude = [three stringValue];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *time = [dateFormatter stringFromDate:four];
NSURLComponents *components = [NSURLComponents componentsWithString:@"http://api.earlyconnect.com/Product/GPSLog.ashx"];
NSURLQueryItem *identifier = [NSURLQueryItem queryItemWithName:@"id" value:uniqueIdentifier];
NSURLQueryItem *lat = [NSURLQueryItem queryItemWithName:@"lat" value:latitude];
NSURLQueryItem *lng = [NSURLQueryItem queryItemWithName:@"lng" value:longitude];
NSURLQueryItem *tim = [NSURLQueryItem queryItemWithName:@"t" value:time];
components.queryItems = @[ identifier, lat, lng, tim ];
NSURL *url = components.URL; // URL with parameters filled in
NSString *urlString = [url absoluteString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSData * responseData = [requestReply dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if([requestReply isEqual:@"OK"])
{
// ERASE FIRST ITEM IN PLIST HERE
}
}] resume];
}
}
To my understanding, I have to erase both myLocationArrayInPlist and myLocatiionDictInPlist inside the savedProfile... but I am not sure. Please help!
When you are removing the first item from the dictionary you're only removing from the in-memory copy of the file, you're not actually changing the file itself.
Once you're done removing everything you want to remove, you need to save the dictionary back to disk.
You can use:
[self.myLocationArrayInPList removeObjectAtIndex:0]
[self.myLocationArrayInPList writeToURL:URLtoFile error:&error]
See doc here: https://developer.apple.com/documentation/foundation/nsdictionary/2879139-writetourl?language=objc