【问题标题】:PresentViewController From AppDelegate handleActionWithIdentifier来自 AppDelegate 的 PresentViewController 句柄ActionWithIdentifier
【发布时间】:2015-04-02 22:04:29
【问题描述】:

是否可以通过 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 推文现在窗口。

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试这个:

      self.window.rootViewController = tweetSheet;
      

      【讨论】:

        【解决方案3】:

        试试这个

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

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-05
          • 1970-01-01
          • 2016-04-26
          • 2017-06-18
          • 2018-10-12
          • 1970-01-01
          • 2012-06-06
          相关资源
          最近更新 更多