0

クイック アクションでアプリを起動するときに、クイック アクションを機能させる方法を理解するのに苦労しています。

ただし、アプリがバックグラウンドにあり、クイック アクションで再起動された場合、クイック アクションは機能します。

クイック アクションから直接アプリを起動しようとすると、アプリ アイコンをタップするだけで起動したかのようにアプリが開きます (つまり、何もしません)。

これが私のApp Delegateのコードです。

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 内に埋め込まれている) が、選択したショートカットに関して特定のページに切り替える必要があることです。

バックグラウンドからアプリを再度開くのではなく、ショートカットを使用して起動するときにショートカットが何もしない原因についてのアイデアはありますか??

4

1 に答える 1

0

I came to realize I was trying to call my methods on a view controller that was not in memory yet. This was causing bizarre behavior in my app. I did have the correct approach to getting access to the view controller and then it dawned on me the possibility of trying to execute the code using 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];
    });

The above worked like magic, and the shortcuts are leading to the correct page. Using a similar approach to mine hopefully yields the desired results for anyone else who wanted to get their shortcuts working by launching the app.

于 2016-09-18T22:57:48.757 に答える