【问题标题】:swift - How to get my location data and put them in variables for iMessage?swift - 如何获取我的位置数据并将它们放入 iMessage 的变量中?
【发布时间】:2015-11-04 13:06:10
【问题描述】:

昨天我在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)
    }

}

【问题讨论】:

  • 当您说消息正文时,您是指短信还是电子邮件或其他内容?
  • 是的,我的意思是消息文本,一旦创建消息,它应该包含位置数据

标签: swift ios9 core-location cllocationmanager clplacemark


【解决方案1】:
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?, urbanity: String?) 等等。这真的取决于你是否想在全局的其他方法中使用这些值。

回答海报中的问题:

设置一个全局变量(在上面的大代码示例中查看这一行)。它使 postalCode 成为全局变量。

var postalCode = ""

在 displayLocationInfo() 中将此变量设置为具有位置的值:

postalCode = placemark.postalCode

在 messageAction() 中设置消息正文以使用该变量:

let messageVC = MFMessageComposeViewController()
messageVC.body = "\(postalCode) is my postalCode"

这将在文本消息窗口中显示“12345 是我的邮政编码”的消息。

【讨论】:

  • 我确切地知道如何实现消息代码,但是我想将我的位置数据嵌入到消息正文中我无法从 displayLocationInfo 函数中获取位置数据
  • 控制台打印工作正常吗?就像它是否正确打印信息一样?如果是这样,那么您只需将信息存储在我上面显示的变量中并在消息函数中调用它们。
  • 好的 - 所以你想把你在控制台中打印出来的东西放到一个消息中。再次查看上面的代码示例 - 它显示了创建一个全局变量(第 7 行)来存储值(在 displayLocationInfo() 中),然后为消息正文调用该值(messageAction())。告诉我这不是你要找的东西
  • 太棒了 :) 很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 2015-04-09
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多