1

What I'm trying to do is to build simple SOS (help me!) application.

There will be a button that will invoke a send-email form. Currently I am on the stage where I got the location from the device and want to embed it in an email that will be sent to an email address.

I want all known functionality of google maps inside the sent mail. If possible, I would like to be able to mark "X" on the exact location on the map. How to do that when I already have the location in hand?

I am new to swift and iOS so any suggestions to change the design are also welcome!

The code I have so far is:

//
//  ViewController.swift
//  SOSapp
//
//  Created by Max Segal on 7/22/15.
//  Copyright (c) 2015 Max Segal. All rights reserved.
//

import UIKit
import CoreLocation
import MessageUI

class ViewController: UIViewController, CLLocationManagerDelegate, MFMailComposeViewControllerDelegate{

    @IBOutlet var statusLabel: UILabel!

    let locationManager=CLLocationManager()

    var locationString=""



    override func viewDidLoad() {
        super.viewDidLoad()
        if ( MFMailComposeViewController.canSendMail() == false){
            var alert=UIAlertController(title: "No email account", message: "No email account - can't send SOS message. Be strong!", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated:true, completion:nil)
            exit(0)
        }

        locationManager.delegate=self
        locationManager.desiredAccuracy=kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func sosButtonPressed(sender: UIButton) {



        var emailTitle = "SOS message"
        var messageBody = "Please help me out! I am here:\n\(locationString)"
        var recipient=SosSender.emailAddress

        var mc = MFMailComposeViewController()
        mc.mailComposeDelegate=self
        mc.setSubject(emailTitle)
        mc.setMessageBody(messageBody, isHTML:true)
        mc.setToRecipients([recipient])

        self.presentViewController(mc, animated: true, completion: nil)

        statusLabel.text="Sending SOS email to " + SosSender.emailAddress+"\nLocation: " + locationString

    }


    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in

            if (error != nil) {
                println("Error: " + error.localizedDescription)
                return
            }

            if (placemarks.count > 0) {
                let pm = placemarks[0] as! CLPlacemark

                self.locationString=self.locationInfoToString(pm)
                println("Debug:" + self.locationString)

            }else{
                println("Error with data")
            }
        })
    }

    func locationInfoToString(placemark: CLPlacemark) -> String
    {
        self.locationManager.stopUpdatingLocation()

        return placemark.locality + "\n" + placemark.administrativeArea + "\n" + placemark.country;
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("Error: " + error.localizedDescription)
    }

    //Email delegate
    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        switch result.value {

        case MFMailComposeResultCancelled.value:
            println("Mail Cancelled")
        case MFMailComposeResultSaved.value:
            println("Mail Saved")
        case MFMailComposeResultSent.value:
            println("Mail Sent")
        case MFMailComposeResultFailed.value:
            println("Mail Failed")
        default:
            break

        }
        self.dismissViewControllerAnimated(false, completion: nil)
    }


}

Thanks.

Max Segal
  • 1,955
  • 1
  • 24
  • 53
  • If you want to capture the device's current screen, you can refer to [this SO answer](http://stackoverflow.com/a/25445629/4195406) using `UIGraphics`. – ztan Jul 27 '15 at 16:51
  • how this post is supposed to help me? I want to capture the geographic location not the screen of the device... – Max Segal Jul 27 '15 at 17:19
  • But you want to screenshot your Google Map in your application right? – ztan Jul 27 '15 at 17:25

0 Answers0