【问题标题】:Send current location through an SMS通过短信发送当前位置
【发布时间】:2012-03-12 05:27:15
【问题描述】:

我正在尝试让我的应用能够通过 SMS 将其当前的 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];
    }
}

【问题讨论】:

  • 使用核心位置框架来获取当前位置。这可以附加到您的短信正文中
  • @Marcus - 你能解决这个问题吗?

标签: xcode ios5 gps sms location


【解决方案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;

    }

【讨论】:

  • 我已经添加了核心定位框架,也尝试添加了上面的代码,它给了我三个问题。 1 - 'locationServicesEnabled' 已弃用。 2 - 从不兼容的类型“ERSSecondViewController*”分配给“id”。 3 - 未使用的变量“坐标”。必须遗漏一些东西.. :P 但是我需要在 IBAction sendInAppSMS 中输入什么代码才能获取短信中的坐标?再次感谢您的帮助!
  • 我已经修复了前两个问题/错误。但最后一个仍然告诉我“未使用的变量'坐标'。但我到了那里:P
  • 只需评论这一行 CLLocationCoordinate2D坐标 = [位置坐标];在你想发送坐标的地方使用它。
  • 现在感觉很愚蠢 :P 但是我应该把这段代码放在哪里? -(IBAction) sendInAppSMS:(id) sender { MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease]; if([MFMessageComposeViewController canSendText]) { controller.body = @"报警!,呼叫发件人号码!"; controller.recipients = [NSArray arrayWithObjects:@"phonenumber1", @"phonenumber2", nil]; [自我presentModalViewController:控制器动画:YES]; } }
  • 工作就像一个魅力!现在我需要了解如何对其进行编码,以便能够以短信中的链接形式获取它,以便接收者只需单击它并在地图中打开它。现在不知何故,当我打开 sendInAppSMS 窗口时,我的取消按钮不起作用。但我仍然可以发送.. 那太好了! :)
猜你喜欢
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多