gpt4 book ai didi

swift - 如何获取我的位置数据并将其放入 iMessage 的变量中?

转载 作者:行者123 更新时间:2023-11-30 13:56:52 25 4
gpt4 key购买 nike

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

让你的类成为正确的委托(delegate)(这个用于 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?, cityity: String?) 等。这实际上取决于您是否想在全局的其他方法中使用这些值。

回答海报中的问题:

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

var postalCode = ""

在 displayLocationInfo() 中设置此变量以获取位置值:

postalCode = placemark.postalCode

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

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

这将在短信窗口中显示一条消息,显示“12345 是我的邮政编码”。

关于swift - 如何获取我的位置数据并将其放入 iMessage 的变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33522726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com