昨天我在stack Overflow中问了一个问题,关于如何在消息体中嵌入我的当前位置
我得到了答案,我纠正了我的代码
我的代码在控制台中运行得很好,但我不知道如何从displayLocationInfo函数中获取数据并将其存储在变量中;然后在消息正文中放入变量。
我是iPhone开发的新手
有什么帮助吗?
class Location: NSObject,CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var currentLocation : CLPlacemark?
var detectLocation : CLLocationManager?
var locManager = CLLocationManager()
func viewDidLoad()
{
viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func updateLocation()
{
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.stopUpdatingLocation()
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if (error != nil)
{
print("Error: " + error!.localizedDescription)
return
}
if placemarks!.count > 0
{
let pm = placemarks![0]
self.displayLocationInfo(pm)
}
else
{
print("Error with the data.")
}
})
}
func displayLocationInfo(placemark: CLPlacemark)
{
self.locationManager.stopUpdatingLocation()
print(placemark.locality)
print(placemark.postalCode)
print(placemark.administrativeArea)
print(placemark.country)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error: " + error.localizedDescription)
}
}
import MessageUI
让你的班级成为正确的代表(这是为iMessage准备的):
MFMessageComposeViewControllerDelegate
制作一些全局变量来存储您想要存储的字段(当您在当前代码中打印到控制台时设置它们):
class Location: NSObject,CLLocationManagerDelegate, MFMessageComposeViewControllerDelegate {
var locationManager = CLLocationManager()
var currentLocation : CLPlacemark?
var detectLocation : CLLocationManager?
var locManager = CLLocationManager()
//put your variables for your message below:
//you should use optionals (?) here but to keep it simple I'm assigning ""
var locality = ""
var postalCode = ""
//set your variables when you are currently printing them in the console:
func displayLocationInfo(placemark: CLPlacemark)
{
self.locationManager.stopUpdatingLocation()
print(placemark.locality)
print(placemark.postalCode)
print(placemark.administrativeArea)
print(placemark.country)
//here I set postalcode to be used in the message later
postalCode = placemark.postalCode
}
//call this function when the user hits a send button
func messageAction() {
if MFMessageComposeViewController.canSendText() {
let messageVC = MFMessageComposeViewController()
messageVC.messageComposeDelegate = self
messageVC.body = "(postalCode) is my postalCode"
self.presentViewController(messageVC, animated: true, completion: nil)
}
else {
print("User hasn't setup Messages.app")
}
}
//if you have any code you want to run when the message is done sending put it in
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
self.dismissViewControllerAnimated(true, completion: nil)
switch (result.rawValue) {
case MessageComposeResultSent.rawValue:
print("Message sent success")
default:
return
}
}
另一种选择是使messageAction()方法具有包含在消息中的参数。比如messageAction(postalCode:String?,locality:String™,municity:String®)等。这实际上取决于你是否想在其他方法中全局使用这些值。
回复发帖人的问题:
设置一个全局变量(在上面的大型代码示例中查找此行)。它使postalCode成为全局变量。
var postalCode = ""
在displayLocationInfo()中,将此变量设置为具有位置值:
postalCode = placemark.postalCode
在messageAction()中,将消息的正文设置为使用该变量:
let messageVC = MFMessageComposeViewController()
messageVC.body = "(postalCode) is my postalCode"
这将在短信窗口中显示一条消息,上面写着"12345是我的邮政编码"。