Konrad Löchner
Konrad Löchner

Reputation: 49

Swift 3dTouch Quick Action crash after restarting the app

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

Upvotes: 0

Views: 144

Answers (0)

Related Questions