几天前我几乎没有尝试将我当前的位置嵌入到我的 iPhone 上的消息正文中,但我失败了。
我尝试了所有我的想法,但我只得到了错误
我得到的最后一个错误是
“致命错误:在展开可选值时意外发现 nil”
我创建了一个对象CLPlacemark
并将其设为可选,这意味着它可以具有值或可以具有 nil。
有什么方法可以用我当前的位置创建消息吗?
主菜单文件
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?
}