问题
I had hardly tried few days ago to embed my current location in Message body on my iPhone, but I failed.
I tried all I have in my mind, but I only got errors
Last error I got is
"fatal error: unexpectedly found nil while unwrapping an Optional value"
I created an object of CLPlacemark
and I made it optional, which means it could have value or could have nil.
is there any way to create a message with my current location ?
MainMenu file
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) {}
}
}
Location file
import UIKit
import CoreLocation
class Location: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var currentLocation : CLPlacemark?
}
回答1:
Just creating CLPlacemark will not fetch location for you. You will have to create object of CLLocationManager and then update your current location using startUpdatingLocation method of CLLocationManager object. Also you'll have to implement delegate methods for receiving the current location this way.
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
}
}
Once you get location coordinates you can do reverse geocoding to get the place mark or exact address. Refer to Reverse Geocode Location in Swift for reverse geocoding. Regarding the error you are getting, you can check if place mark object is nil before creating a message.
来源:https://stackoverflow.com/questions/33502130/cant-figure-out-how-to-embed-my-current-location-in-message-body-correctly-usin