2012-03-12 153 views
4

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

我现在有我的代码,只能在应用程序中调出一个短信窗口,在该窗口中我可以在接收号码和主体中编辑。我想知道的是我如何获得这个机构的当前位置,以便通过短信发送?无法弄清楚。

这就是我的代码现在看起来关于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]; 
    } 
} 
+0

使用核心定位框架来获取当前location.this可以附加到您的短信身体 – 2012-03-12 06:16:17

+1

@Marcus - 是你能解决这个问题吗? – Akshay 2012-08-24 08:30:07

回答

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; 

    } 
+0

我已经添加了核心位置框架,并且还尝试添加上面的代码,它给了我三个问题。 1 - 'locationServicesEnabled'已弃用。 2 - 从不兼容的类型'ERSSecondViewController *'分配给'id '。 3 - 未使用的变量“坐标”。必须缺少的东西..:P但是,我需要什么代码放在IBAction sendInAppSMS然后获取短信息中的坐标?并再次感谢您的帮助! – Marcus 2012-03-13 20:14:14

+0

我已经修复了前两个问题/错误。但最后一个仍然告诉我“未使用的变量”坐标,但是我到了那里:P – Marcus 2012-03-13 20:49:22

+0

只是评论这一行CLLocationCoordinate2D坐标= [位置坐标];使用此位置发送坐标 – 2012-03-14 04:39:19