Sending current location via SMS

I am trying to get my application to send its current GPS location via SMS to a pre-allocated phone number.

I have my code right now, which only calls the SMS window inside the application, in which I can edit in the receiving number and body. I am wondering how to get the current location in this body so that it can be sent via SMS? I can not understand.

Here's what my code looks like right now regarding the SMS feature.

-(IBAction) sendInAppSMS:(id) sender { MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease]; if([MFMessageComposeViewController canSendText]) { controller.body = @"Alarm!, call the senders number!"; controller.recipients = [NSArray arrayWithObjects:@"phonenumber1", @"phonenumber2", nil]; [self presentModalViewController:controller animated:YES]; } } 
+4
source share
1 answer

First, add the main layout structure to your project. and call this method to find the current location.

  -(IBAction) sendInAppSMS:(id) sender { MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease]; if([MFMessageComposeViewController canSendText]) { CLLocation *location=[self findCurrentLocation]; CLLocationCoordinate2D coordinae=[location coordinate]; controller.body =[[NSString alloc] initWithFormat:@" Alarm!, call the senders number with latitude:%f longitude:%f",coordinae.latitude,coordinae.longitude]; ; controller.recipients = [NSArray arrayWithObjects:@"phonenumber1", @"phonenumber2", nil]; [self presentModalViewController:controller animated:YES]; } } -(CLLocation*)findCurrentLocation { CLLocationManager *locationManager = [[CLLocationManager alloc] init]; if ([locationManager locationServicesEnabled]) { locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.distanceFilter = kCLDistanceFilterNone; [locationManager startUpdatingLocation]; } CLLocation *location = [locationManager location]; CLLocationCoordinate2D coordinate = [location coordinate]; return location; } 
+1
source

Source: https://habr.com/ru/post/1400935/


All Articles