1

是否可以通過AppDelegate方法handleActionWithIdentifier展示視圖控制器?PresentViewController From 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的鳴叫窗口。

我不希望應用程序必須打開才能執行此操作。思考?

回答

1

的AppDelegate的動作處理程序只是爲了進行後臺操作,只推出了短量的應用時間,在後臺線程上執行該塊,然後再次背景應用程序。請務必閱讀蘋果文檔:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleActionWithIdentifier:forRemoteNotification:completionHandler

對於您的示例,而不是呈現推文視圖控制器,您需要直接發送推文而無需用戶交互。

0

只是這種嘗試:

self.window.rootViewController = tweetSheet; 
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(); 
} 

}

相關問題