0

當我以快速操作啓動應用程序時,我很難弄清楚如何讓我的快速操作正常工作。通過快速操作處理應用程序啓動的正確方法

我快行動工作,但是,如果應用程序在後臺與行動迅速重新啓動。

當我嘗試從行動迅速直啓動應用程序,該應用程序會打開,如果它是隻需按應用程序圖標啓動(即它什麼都不做)。

這裏是我的應用程序代表的一些代碼。

didFinishLaunchingWithOptions:

UIApplicationShortcutItem *shortcut = launchOptions[UIApplicationLaunchOptionsShortcutItemKey]; 
if(shortcut != nil){ 
    performShortcutDelegate = NO; 
    [self performQuickAction: shortcut fromLaunch:YES]; 
} 

調用的方法:

-(BOOL) performQuickAction: (UIApplicationShortcutItem *)shortcutItem fromLaunch:(BOOL)launchedFromInactive { 
NSMutableArray *meetings = [self.fetchedResultController.fetchedObjects mutableCopy]; 
[meetings removeObjectAtIndex:0]; 
unsigned long count = meetings.count; 
BOOL quickActionHandled = NO; 
if(count > 0){ 
    MainViewController *mainVC = (MainViewController *)self.window.rootViewController; 
    if(launchedFromInactive){ 
     mainVC.shortcut = shortcutItem; 
    } 
    else{ 
     UINavigationController *childNav; 
     MeetingViewController *meetingVC; 
     for(int i = 0; i < mainVC.childViewControllers.count; i++){ 
      if([mainVC.childViewControllers[i] isKindOfClass: [UINavigationController class]]){ 
       childNav = mainVC.childViewControllers[i]; 
       meetingVC = childNav.childViewControllers[0]; 
       break; 
      } 
     } 

     self.shortcutDelegate = meetingVC; 

     if ([shortcutItem.type isEqual: @"Meeting"]){ 
      NSNumber *index = [shortcutItem.userInfo objectForKey:@"Index"]; 
      [self.shortcutDelegate switchToCorrectPageWithIndex: index launchedFromInactive:NO]; 
      quickActionHandled = YES; 
     } 
    } 
} 

需要被執行的唯一動作是,我的頁面視圖控制器(其嵌入在meetingVC內)應關於所選擇的快捷方式切換到某個頁面。

是什麼原因導致的快捷使用它來啓動,而不是從後臺重新打開應用程序時,不要做任何事情,任何想法?

+0

我敲了一次前這個問題,我再有同樣的問題,我想這是因爲具有快速行動啓動應用程序時,根視圖控制器尚未建立,所以你不能調用方法在上面。可悲的是我沒有辦法解決。 – SimonBarker

+0

我找到了我的程序解決方案!希望它適用於其他人正在尋找一種方法來做到這一點。 – iOShepherd

回答

0

我才明白,我試圖打電話給我,這不是在內存中還沒有一個視圖控制器上的方法。這在我的應用程序中造成了奇怪的行爲。我確實有正確的方法來訪問視圖控制器,然後它讓我想到使用GCD執行代碼的可能性。

__block MeetingViewController *safeSelf = self; 
contentVC = [self initializeContentViewController: self.didLaunchFromInactive withPageIndex: intIndex]; 
NSArray *viewControllers = @[contentVC]; 
dispatch_async(dispatch_get_main_queue(), ^{ 
     [safeSelf.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 
    }); 

上面的工作像魔術一樣,快捷方式導致正確的頁面。對於希望通過啓動應用程序來獲得快捷方式的人,希望使用類似的方法可以產生期望的結果。

相關問題