Lim Thye Chean
Lim Thye Chean

Reputation: 9434

iOS 9 Quick Actions (3D Touch)

I am trying to understand doing Quick Actions (3D Touch) for iOS 9.

I wanted the user to select 1 of 4 filter to be applied to image, so if I select item 1, I will set the NSUserDefaults.standardUserDefaults() to the filter, then show the correct picture with the applied filter.

In AppDelete.swift:

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    var filterType:Int
    switch (shortcutItem.type) {
        ...set filterType
    }

    NSUserDefaults.standardUserDefaults().setInteger(filterType, forKey:"filterType")
    NSUserDefaults.standardUserDefaults().synchronize()        
}

In ViewController.swift:

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"setDefaultFilter", name: UIApplicationWillEnterForegroundNotification, object:nil) // Handle enter from background
    setDefaultFilter()
}

func setDefaultFilter() {
    filterType = defaults.integerForKey("filterType")
    ...
    imageView.image = filterImage(defaultImage!, filter:filters[filterType])
}

However, when enter the app from the menu, it will always show the last selection (not the current selection). If I select item 1, nothing happened. I select item 3, item 1 will appeared.

I have also try passing parameters via appDelegate and the result is the same. I believe there are some issues with life cycle.

Any ideas?

Upvotes: 1

Views: 931

Answers (2)

sangjoon moon
sangjoon moon

Reputation: 720

didFinishLaunchingWithOptions method is always called before calling performActionForShortcutItem method to response to the quick action. So, I think that you need to check what kind of quick action is selected in didFinishLaunchingWithOptions method. If the app is not launched from quick action, you just continue to your normal app launching process.(default filter)

And if you decide to handle quick action in didFinishLaunchingWithOptions, you have to return NO in didFinishLaunchingWithOptions.

You could get more idea from my demo project:

https://github.com/dakeshi/3D_Touch_HomeQuickAction

Upvotes: 0

Johnny Wang
Johnny Wang

Reputation: 256

NSUserDefaults write data on flash, which may not be so fast.

You can wait a little longer, like observe UIApplicationDidBecomeActiveNotification other than UIApplicationWillEnterForegroundNotification.

Or you can use other ways to pass params, e.g., as an instance variable in AppDelegate.

Upvotes: 1

Related Questions