Search code examples
xcodeios5gpssmslocation

Send current location through an SMS


I'm trying to get my app to be able to send its current GPS location through an SMS to a preentered phone number.

I have my code right now that only brings up an SMS window inside the app in which I can edit in the receiving number and a body. What I am wondering is how I get the current location in this body so it can be sent through the SMS? Can't figure it out.

This is how my code looks right now regarding the SMS function.

-(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];
    }
}

Solution

  • Firstly add core location framework in Your Project. and call this method for finding 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;
    
        }