Skip to content

Instantly share code, notes, and snippets.

@hagino3000
Created April 1, 2011 05:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagino3000/897783 to your computer and use it in GitHub Desktop.
Save hagino3000/897783 to your computer and use it in GitHub Desktop.
Handle location event And HTTP get by Objective-C
-(NSString*)locationString:(CLLocation *)newLocation {
NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
return [NSString stringWithFormat:@"(%@) Location %.06f %.06f %@", ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) ? @"bg" : @"fg", newLocation.coordinate.latitude, newLocation.coordinate.longitude, [formatter stringFromDate:newLocation.timestamp]];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation!!");
NSLog([ self locationString:newLocation ]);
NSLog(@"start send seerver");
NSString * url;
NSString * state = ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) ? @"bg" : @"fg";
url = [NSString stringWithFormat:@"http://49.212.11.68:3000/location?lat=%.06f&long=%.06f&type=%@", newLocation.coordinate.latitude, newLocation.coordinate.longitude, state];
NSMutableURLRequest *request;
request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:url]];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setTimeoutInterval:20];
[request setHTTPShouldHandleCookies:FALSE];
[request setHTTPMethod:@"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"END");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment