无法弄清楚如何使用CLPlacemark将我的当前位置正确地嵌入到邮件正文中 [英] can't figure out how to embed my current location in message body correctly using CLPlacemark

查看:312
本文介绍了无法弄清楚如何使用CLPlacemark将我的当前位置正确地嵌入到邮件正文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前我几乎没有尝试将当前位置嵌入到iPhone的Message正文中,但是失败了.

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

致命错误:展开Optional时意外发现nil 值"

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

我创建了一个CLPlacemark对象,并将其设置为可选对象,这意味着它可以具有值或可以为nil.

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文件

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方法更新当前位置.另外,您还必须实现委托方法,以这种方式接收当前位置.

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
    }

}

一旦获得位置坐标,就可以进行反向地理编码以获得地标或确切地址.有关反向地理编码,请参见 Swift中的反向地理编码位置.关于收到的错误,可以在创建消息之前检查地标对象是否为空.

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.

这篇关于无法弄清楚如何使用CLPlacemark将我的当前位置正确地嵌入到邮件正文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆