xcode - 如何通过短信发送用户经纬度坐标

标签 xcode swift mfmessagecomposeviewcontroller cllocationcoordinate2d

在我的“共享位置”功能中,我希望用户能够发送以长/纬度坐标形式共享当前位置的文本。我所实现的错误是“上下文类型“字符串”不能与数组文字一起使用”。如何或什么是正确的代码来实现?

这是我的代码

 @IBAction func shareLocation(sender: AnyObject) {
   // Send user coordinates through text

    if !MFMessageComposeViewController.canSendText() {
        print("SMS services are not available")

        var coordinate: CLLocationCoordinate2D

        messageVC!.body = [coordinate.latitude, coordinate.longitude];
        messageVC!.recipients = [LookoutCell.description()];
        messageVC!.messageComposeDelegate = self;


        self.presentViewController(messageVC!, animated: false, completion: nil)
    }
}

最佳答案

这段代码有很多错误:

  1. 您正在检查是否无法通过 !MFMessageComposeViewController.canSendText() 发送短信,如果不能的话就发送(括号需要提前结束)
  2. 您声明var coordinate: CLLocationCoordinate2D没有值(value),无法编译
  3. 纬度和经度是 double ,因此您需要字符串格式来输出这些值
  4. body是一个字符串,您试图向它发送一个数组。
  5. 试试这个:messageVC!.body = String(format: "Lat: %.4f, Long: %.4f", coordinate.latitude, coordinate.longitude) - 您需要查看格式指南以获取更多详细信息(您可以从 here 开始)

关于xcode - 如何通过短信发送用户经纬度坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36905203/

相关文章:

ios - 后台任务不在 Swift 中调用类

ios - MessageComposeViewController 仅出现一次

ios11 - 没有收件人字段的 MFMessageComposeViewController (iOS 11)

ios - 从 xib 文件动态加载 iOS View

ios - Apple 的 LoadPresetDemo 不起作用

ios - Xcode 中针对 iPad 的选项

iphone - 数组值递减

iOS SWIFT - CollectionView 特定单元格通过触摸扩展

swift - IOHIDDeviceSetReport 方法不接受 Swift 3 中的 UnsafeRawPointer 类型

ios - 如何从 NSobject 类 iOS 的任何 View Controller 类上弹出 MFMessageComposeViewController 对象