簡體   English   中英

處理快速啟動應用程序的正確方法

[英]Correct way to handle application launching with quick actions

啟動快速應用程序時,我很難解決如何使快速操作生效的問題。

但是,如果該應用程序在后台運行並通過快速操作重新啟動,那么我的快速操作就會起作用。

當我嘗試直接通過快速操作啟動應用程序時,只需輕按應用程序圖標即可打開該應用程序,就像啟動該應用程序一樣(即它什么也不做)。

這是我的應用程序委托中的一些代碼。

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中)應該切換到某個頁面。

關於導致快捷方式啟動時不執行任何操作而不是從后台重新打開應用程序的任何想法?

我意識到我正在嘗試在尚未在內存中的視圖控制器上調用我的方法。 這在我的應用中造成了奇怪的行為。 我確實擁有訪問視圖控制器的正確方法,然后我想到了嘗試使用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];
    });

上面的方法就像魔術一樣工作,快捷方式指向正確的頁面。 對於希望通過啟動該應用程序來實現其快捷方式的其他任何人,使用類似的方法進行挖掘有望產生預期的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM