无法弄清楚如何使用CLPlacemark正确地在消息正文中embedded我的当前位置

我几天前几乎没有尝试将我目前的位置embedded到我的iPhone上的Message body中,但是我失败了。

我尝试了我所有的想法,但我只有错误

我得到的最后一个错误是

“致命错误:意外地发现零,而解包可选值”

我创build了一个CLPlacemark的对象,我把它作为可选项,这意味着它可能有价值或可能CLPlacemark

有什么方法来创build一个消息与我目前的位置?

MainMenu文件

 import UIKit import Social import MessageUI import Foundation import CoreLocation class MainMenu: UIViewController , MFMessageComposeViewControllerDelegate , UINavigationControllerDelegate , MFMailComposeViewControllerDelegate, CLLocationManagerDelegate { let locationManager = CLLocationManager() func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) { } let placeMarks = Location() @IBAction func TapOnEmergency(sender: UIButton) { let textMessageRecipients = ["+123456789"] let messageComposer = MFMessageComposeViewController() let Emergency = UIAlertController(title: "Do You Need Help ?", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) let Yes = UIAlertAction(title: "YES", style: .Default) { (action) in if (MFMessageComposeViewController.canSendText()) { messageComposer.messageComposeDelegate = self messageComposer.recipients = textMessageRecipients messageComposer.body = "I'm Lost, I need some Help, Here's my Location \(self.placeMarks.currentLocation!.country)" self.navigationController?.presentViewController(messageComposer, animated: true){} self.presentViewController(messageComposer, animated: true, completion: nil) self.messageComposeViewController(messageComposer, didFinishWithResult: MessageComposeResultCancelled) } else { let errorAlert = UIAlertController(title: "Cannot Send Text Message", message: "Your device is not able to send text messages.", preferredStyle: UIAlertControllerStyle.Alert) errorAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)) self.presentViewController(errorAlert, animated: true, completion: nil) } let Options = UIAlertController(title: "Do You Want to Call Your Supervisor ?", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) let dialNumber = UIAlertAction(title: "YES", style: .Default) { (action) in let call:NSURL = NSURL(string: "tel://123456789")! UIApplication.sharedApplication().openURL(call) } Options.addAction(dialNumber) let Dismiss = UIAlertAction(title: "Dismiss", style: .Destructive) { (action) in } Options.addAction(Dismiss) self.presentViewController(Options, animated: true) {} } Emergency.addAction(Yes) let No = UIAlertAction(title: "Dismiss", style: .Destructive) { (action) in } Emergency.addAction(No) self.presentViewController(Emergency, animated: true) {} } } 

位置文件

 import UIKit import CoreLocation class Location: UIViewController, CLLocationManagerDelegate { let locationManager = CLLocationManager() var currentLocation : CLPlacemark? } 

只是创buildCLPlacemark不会为您获取位置。 您必须创buildCLLocationManager的对象,然后使用CLLocationManager对象的startUpdatingLocation方法更新您的当前位置。 你也必须实现委托方法来接收当前位置。

 import UIKit import CoreLocation class Location: UIViewController, CLLocationManagerDelegate { let locationManager = CLLocationManager() var currentLocation : CLPlacemark? var locationManager = CLLocationManager() private override init() { super.init() locationManager.delegate = self } func updateLocation() { locationManager.startUpdatingLocation() } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { locationManager.stopUpdatingLocation() let location : CLLocation = locations.last as CLLocation! //use reverse geocoding to get the address from location coordinates you got } } 

一旦你得到位置坐标,你可以做反向地理编码来获得地点标记或确切的地址。 请参阅Swift中的反向地理编码位置进行反向地理编码。 关于你得到的错误,你可以在创build消息之前检查地点标记对象是否为零。