gpt4 book ai didi

ios - 无法弄清楚如何使用 CLPlacemark 将我的当前位置正确地嵌入到消息正文中

转载 作者:行者123 更新时间:2023-11-28 08:54:29 24 4
gpt4 key购买 nike

几天前我几乎没有尝试在我的 iPhone 上的消息正文中嵌入我的当前位置,但我失败了。

我想尽了所有办法,但我只得到了错误

我得到的最后一个错误是

"fatal error: unexpectedly found nil while unwrapping an Optional value"

我创建了一个 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?
}

最佳答案

只是创建 CLPlacemark 不会为您获取位置。您将必须创建 CLLocationManager 对象,然后使用 CLLocationManager 对象的 startUpdatingLocation 方法更新您的当前位置。此外,您还必须实现委托(delegate)方法以通过这种方式接收当前位置。

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
}

}

获得位置坐标后,您可以进行反向地理编码以获取地标或确切地址。引用Reverse Geocode Location in Swift用于反向地理编码。关于您遇到的错误,您可以在创建消息之前检查地点标记对象是否为 nil。

关于ios - 无法弄清楚如何使用 CLPlacemark 将我的当前位置正确地嵌入到消息正文中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33502130/

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