PresentViewController 来自 AppDelegate handleActionWithIdentifier

PresentViewController From AppDelegate handleActionWithIdentifier

是否可以通过 AppDelegate 方法呈现视图控制器handleActionWithIdentifier

我正在注册类似于以下的操作类别:

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

当我的远程通知到达时,我可以在主屏幕上向下拉或向左滑动以查看文本 "Tweet"

我的handleActionWithIdentifier方法很简单:

- (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();
    }
}

然而,当我点击通知中的 "Tweet" 操作类别时,它什么也没做。它现在不会显示 Twitter 推文 window。

我不希望必须打开应用程序才能执行此操作。想法?

试试这个:

self.window.rootViewController = tweetSheet;

试试这个

- (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();
}

}

AppDelegate 操作处理程序仅用于执行后台操作,仅在短时间内启动应用程序,在后台线程上执行块,然后再次将应用程序置于后台。请务必阅读苹果文档:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

对于您的示例,您需要直接发送推文而不需要用户交互,而不是显示推文视图控制器。