0

I have a problem with implementing 3D Touch Quick actions. The action should set an annotation to the user's location on a mapview. It works perfectly fine when the app is already running. But if I kill the app and try again, it just crashes.

AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    //shortcut Item


    let shortcut1 = UIMutableApplicationShortcutItem(type: "markLocation", localizedTitle: NSLocalizedString("Mark my location", comment: "Shortcut mark location"), localizedSubtitle: "", icon: UIApplicationShortcutIcon(type: .markLocation), userInfo: nil)



    application.shortcutItems = [shortcut1]


    return true
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    switch shortcutItem.type {
    case "markLocation":
        mapVC.addNewPin()
    default:
        print("")
    }


}

MapViewController (mapVC):

func addNewPin() {


    if lastAnnotation != nil {
        let alertController = UIAlertController(title: NSLocalizedString("Annotation already dropped", comment: "Error Message"), message: NSLocalizedString("There is an annotation on screen. Try dragging it if you want to change its location!", comment: "Eror Message"), preferredStyle: .alert)
        let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.destructive) { alert in
            alertController.dismiss(animated: true, completion: nil)
        }
        alertController.addAction(alertAction)
        present(alertController, animated: true, completion: nil)

    } else {
        let location = LocationAnnotation(coordinate: mapView.userLocation.coordinate, title: NSLocalizedString("To explore", comment: "Annotation Title"), subtitle: " ")

        mapView.addAnnotation(location)
        lastAnnotation = location
    }
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • What do you see on the development console? – Wukerplank Jul 31 '17 at 14:35
  • Just my deduction, but I think you are not saving any of the information into some kind of storage (UserDefaults/CoreData) So taht might be the source of problem - the app is looking for info which is not there... – Dominik Bucher Jul 31 '17 at 14:39
  • I can't see the console because the app is killed. And because of this it is not running in Xcode anymore. Why should I save any information in a storage? I think it crashes because the mapView is not finished loading, when the action is called. So there is no user Location, where a annotation could be placed. I tried to avoid this and tried many ways to let the view run first, but it didn't worked. – Konrad Löchner Jul 31 '17 at 15:09
  • I have a strong suspicion that the `mapVC` is nil when the shortcut runs with the app closed. – Slayter Jul 31 '17 at 15:34
  • mapVC is declered on very top under import... var mapVC = MapViewController(). Is there an other way to do it? – Konrad Löchner Jul 31 '17 at 15:57

0 Answers0