2015-11-03 20 views
1

我幾天前幾乎沒有嘗試將我當前的位置嵌入到iPhone的郵件正文中,但是失敗了。無法弄清楚如何使用CLPlacemark正確地在郵件正文中嵌入我的當前位置

我嘗試了所有我在我的腦海裏,但我只得到了錯誤

最後一個錯誤我得到的是

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

我創建的CLPlacemark一個對象,我做到了可選的,這意味着它可能有價值或可能有零。

有什麼方法可以用我的當前位置創建消息嗎?

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? 
} 

回答

0

只是創造CLPlacemark不會爲你取的位置。您必須創建CLLocationManager對象,然後使用CLLocationManager對象的startUpdatingLocation方法更新您的當前位置。你也必須實現委託方法來接收當前位置。

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進行反向地理編碼。關於您遇到的錯誤,您可以在創建消息之前檢查地點標記對象是否爲零。