aherrick
aherrick

Reputation: 20161

PresentViewController From AppDelegate handleActionWithIdentifier

Is it possible to present a view controller from the AppDelegate method handleActionWithIdentifier?

I am registering action categories similar to below:

action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:@"Tweet"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];

When my remote notification arrives I can pull down or swipe left on the home screen to see the text "Tweet"

My handleActionWithIdentifier method is very simple:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"Tweeting this is awesome!"];
        [self.window.rootViewController presentViewController:tweetSheet animated:YES completion:^{}];
    }

    if (completionHandler) {

        completionHandler();
    }
}

However when I click the "Tweet" action category from my notification it does nothing. It doesn't bring up the Twitter tweet now window.

I don't want the app to have to open in order to have this action occur. Thoughts?

Upvotes: 1

Views: 645

Answers (3)

NRimer
NRimer

Reputation: 274

The AppDelegate action handlers are only meant to perform background operations, only launching the app for short amount of time, performing the block on a background thread then backgrounding the app again. Be sure to read the apple documentation : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

For your example instead of presenting the tweet view controller you'll need to directly send the tweet without user interaction.

Upvotes: 1

Ram Vadranam
Ram Vadranam

Reputation: 485

Try this

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:@"Tweeting this is awesome!"];
    [self.window makeKeyAndVisible];
    [self.window.rootViewController presentViewController:tweetSheet animated:YES completion:^{}];
}

if (completionHandler) {

    completionHandler();
}

}

Upvotes: 0

Pablo A.
Pablo A.

Reputation: 2052

Just try with this:

self.window.rootViewController = tweetSheet;

Upvotes: 0

Related Questions