2015-09-26 5 views
14

Ich implementiere einige schnelle 3D-Aktionen für meine iOS 9 App in Swift, und ich habe ein seltsames Problem. Wenn meine App im Hintergrund ist und ich mit der schnellen Aktion starte, läuft alles wie geplant. Wenn meine App tot ist (d. H. Ich habe sie aus dem Multitasking-Menü gelöscht), und ich starte mit der schnellen Aktion, stürzt die App ab. Ich habe Probleme beim Debuggen. Sobald ich die App beendet habe, wird die Debug-Sitzung in Xcode gelöst. Gibt es eine Möglichkeit für mich, eine Verbindung zur App herzustellen, um wie üblich zu debuggen, oder gibt es etwas in meinem Code, das das verursacht? Danke im Voraus.Starten mit UIApplicationShortcutItem

Code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    var launchedFromShortCut = false 

    //Check for ShortCutItem 
    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem 
    { 
     launchedFromShortCut = true 
     self.handleShortCutItem(shortcutItem) 
    } 

    return !launchedFromShortCut 
} 

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) 
{ 
    self.handleShortCutItem(shortcutItem) 
} 

func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) 
{ 
    //Get type string from shortcutItem 
    if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) 
    { 
     //Get root navigation viewcontroller and its first controller 
     let rootNavigationViewController = window!.rootViewController as? UINavigationController 


     if let rootViewController = rootNavigationViewController?.viewControllers.first as! LaunchViewController? 
     { 
      //Pop to root view controller so that approperiete segue can be performed 
      rootNavigationViewController?.popToRootViewControllerAnimated(false) 

      switch shortcutType 
      { 
       case .Compose: 
        rootViewController.shouldCompose() 
        break 
      } 
     } 
    } 
} 

Dank!

+0

Sie können immer noch die Crash-Log-Datei der App sehen, nachdem es getötet wurde ... –

+0

Sie bekommen dies sortiert, habe ich das gleiche Problem? – theiOSDude

+0

@theiOSDude nein habe ich nicht. –

Antwort

15

Ich habe endlich diese Arbeit. Hier ist, was meine AppDelegate.swift Datei endete als;

class AppDelegate: UIResponder, UIApplicationDelegate { 

// Properties 
var window: UIWindow? 
var launchedShortcutItem: UIApplicationShortcutItem? 

func applicationDidBecomeActive(application: UIApplication) { 

    guard let shortcut = launchedShortcutItem else { return } 

    handleShortcut(shortcut) 

    launchedShortcutItem = nil 

} 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    // Override point for customization after application launch. 
    var shouldPerformAdditionalDelegateHandling = true 

    // If a shortcut was launched, display its information and take the appropriate action 
    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem { 

     launchedShortcutItem = shortcutItem 

     // This will block "performActionForShortcutItem:completionHandler" from being called. 
     shouldPerformAdditionalDelegateHandling = false 

    } 

    return shouldPerformAdditionalDelegateHandling 
} 


func handleShortcut(shortcutItem:UIApplicationShortcutItem) -> Bool { 

    // Construct an alert using the details of the shortcut used to open the application. 
    let alertController = UIAlertController(title: "Shortcut Handled", message: "\"\(shortcutItem.localizedTitle)\"", preferredStyle: .Alert) 
    let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 
    alertController.addAction(okAction) 

    // Display an alert indicating the shortcut selected from the home screen. 
    window!.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

    return handled 

} 

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { 

    completionHandler(handleShortcut(shortcutItem)) 

} 

dieser wurde viel von Apples genommen sample code für UIApplicationShortcuts, und während ich meine app starten Sie eine Benachrichtigung mit zu beweisen, dass es die richtige Verknüpfung gewählt wurde, ist zu erkennen, diese zu Ihrem Code angepasst werden könnte, um Pop der View-Controller.

Ich denke, die func applicationDidBecomeActive der kritische Teil war, dass ich fehlte, und die self.handleShortCut(shortcutItem) von didFinishLaunchingWithOptions entfernen (ansonsten wurde es handleShortCut zweimal rufen, wie es schien).

+0

Das ist super wird bald testen. –

+0

Was ist dieser "gehandhabte" Wert, den Sie zurückgeben? –

37
  1. In Xcode, offen Produkt -> Schemata -> Edit Schemes
  2. In Ihrem Run Schema, das Launch 'für ausführbare Warten gestartet werden' Einstellung ändern

Nun, wenn Sie drehen Beim Debuggen und Ausführen Ihrer App wartet Xcode darauf, dass Sie Ihre App vom Startbildschirm aus starten, sodass Sie den Start mit einem 3D Touch Shortcut Item testen können.

See Screenshot in Xcode of the Setting

+1

Vielen Dank! Dies ermöglichte es mir, schnelle Aktionen aus einem abgeschlossenen Zustand zu testen. +1 –

+1

Dies druckt die Protokolle in der Konsole nicht? – genaks

+2

Wie kann ich Dinge auf die Konsole drucken? – iOShepherd

2

Ich habe versucht, alle oben, und es hat das Problem nicht lösen als ich die Verknüpfung nach der Verzögerung in handleShortcut Methode versuchte Handhabung:

self.performSelector("action1", withObject: self, afterDelay: 0.5) 

und ein Verfahren für jede Aktion hinzugefügt, und es funktionierte wie ein Charme

0

Ersetzen Sie Ihre didfinishlaunching Methode mit diesem.