PresentViewController来自AppDelegate handleActionWithIdentifier

时间:2015-04-02 22:04:30

标签: ios objective-c apple-push-notifications presentviewcontroller

是否可以通过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();
    }
}

但是,当我点击通知中的“推文”操作类别时,它什么也没做。它没有显示Twitter推文现在的窗口。

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

3 个答案:

答案 0 :(得分:1)

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

对于您的示例而不是呈现推文视图控制器,您需要在没有用户交互的情况下直接发送推文。

答案 1 :(得分:0)

试试这个:

self.window.rootViewController = tweetSheet;

答案 2 :(得分:0)

试试这个

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

}