通过短信发送当前位置

时间:2012-03-12 05:27:15

标签: xcode ios5 gps sms location

我正在尝试让我的应用能够通过短信将其当前的GPS位置发送到预先输入的电话号码。

我现在有了我的代码,只在应用程序内部显示一个SMS窗口,我可以在其中编辑接收号码和正文。我想知道的是我如何得到这个机构的当前位置,以便它可以通过短信发送?无法理解。

这就是我的代码现在关于SMS功能的看法。

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

1 个答案:

答案 0 :(得分:1)

首先在您的项目中添加核心位置框架。并调用此方法查找当前位置。

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

    }