我正在尝试让我的应用程序能够通过短信将其当前的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]; } }
解决方法
首先在您的项目中添加核心位置框架.并调用此方法来查找当前位置.
-(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",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; }