Showing posts with label StringWithFormat. Show all posts
Showing posts with label StringWithFormat. Show all posts

Friday, December 10, 2010

Where is iPhone Simulator NSTemporaryDirectory

Sometimes I need to save temporary files. The problem is cleaning up afterwards: application might crash during debug sessions leaving files, which are accessed on following launch thus messing up normal expected application behaviour.

iOS provides application specific temporary folder. The benefit in using this is that even if the worst case happens and your app fails to clean up temporary files for some reason (try to view 65 MB image, for example), application temporary folder is automatically cleaned out every 3 days. Your application is thus not able to leave behind "zombie" files, which would fill up device harddisk until all available space is used!

Friday, September 3, 2010

How to Create Human-readable Geo Coordinate Strings

How to create human-readable coordinate strings, a code snippet that I wrote long time ago. Released here as a reminder for The Next Time (there's always a next time):
CLLocationCoordinate2D loc = mapView.userLocation.coordinate;

BOOL loc_NS;
if (loc.latitude > 0)
{
    loc_NS = YES;
}
else
{
    loc_NS = NO;
    loc.latitude *= -1;
}

int lat_d = floor(loc.latitude);
loc.latitude -= lat_d;
loc.latitude *= 60;
int lat_m = floor(loc.latitude);
loc.latitude -= lat_m;
loc.latitude *= 60;
int lat_s = floor(loc.latitude);

BOOL loc_WE;
if (loc.longitude > 0)
{
    loc_WE = NO;
}
else
{
    loc_WE = YES;
    loc.longitude *= -1;
}

int lon_d = floor(loc.longitude);
loc.longitude -= lon_d;
loc.longitude *= 60;
int lon_m = floor(loc.longitude);
loc.longitude -= lon_m;
loc.longitude *= 60;
int lon_s = floor(loc.longitude);

location.text = [NSString stringWithFormat:@"Location: %d°%d′%d″%@ %d°%d′%d″%@",
    lat_d, lat_m, lat_s, loc_NS ? @"N" : @"S",
    lon_d, lon_m, lon_s, loc_WE ? @"W" : @"E"];
Based on Wikipedia article about Geographic coordinate conversion.