Sample code on dynamic UIApplicationShortcutItems

前端 未结 4 911
眼角桃花
眼角桃花 2021-02-04 10:32

I\'m looking after some Obj-C sample code for a dynamic UIApplicationShortCutItem.

Basically, I have three static UIApplicationShortcutItems an

相关标签:
4条回答
  • 2021-02-04 10:58

    Here's how to detect if the app was launched with a quick shortcut in Objective-c.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];
        if(shortcutItem){
            [self handleShortCutItem:shortcutItem];
        }
    }
    
    - (void)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem  {
        if([shortcutItem.type isEqualToString:@"takePhotoAction"]){
            //ACTION HERE
        }
    }
    

    To detect the type of shortcut selected while the app is running in the background.

    - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    NSLog(@"%@", shortcutItem.type);
        if([shortcutItem.type isEqualToString:@"takePhotoAction"]){
            //ACTION HERE
        }
    }
    
    0 讨论(0)
  • 2021-02-04 10:58

    For someone who is looking for a Swift4 version for @chengpei answer, it is here:

    let photoIcon = UIApplicationShortcutItem(type: "Selfie", localizedTitle:"Take selfie", localizedSubtitle: "Loc Subtitle", icon: nil, userInfo:nil)
    let videoIcon = UIApplicationShortcutItem(type: "Video", localizedTitle:"Take video", localizedSubtitle: "Loc Subtitle for Video", icon: UIApplicationShortcutIcon(type: .captureVideo), userInfo:nil)
    UIApplication.shared.shortcutItems = [photoIcon, videoIcon]
    
    0 讨论(0)
  • 2021-02-04 11:01

    I've post a simple objective-c example on GitHub that add/remove shortcuts to Home Screen.

    You can check it here: https://github.com/cjimenezpacho/3Dtouch-home-screen-quick-actions

    I have a method on App Delegate that handles shortcut items (based on another stackoverflow answer that I can't found :( ):

    - (BOOL)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem {
        BOOL handled = NO;
    
        if (shortcutItem == nil) {
            return handled;
        }
    
        handled = YES;
        UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Handle Shortcut" message:shortcutItem.type delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [av show];
    
        return handled;
    
    }
    

    It is called by application:didFinishLaunchingWithOptions and application: performActionForShortcutItem whether app is launched or not.

    And to add/remove shortcuts on demand:

    - (void) addActionToShortCutItems{
        NSArray <UIApplicationShortcutItem *> *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
        if([existingShortcutItems count]){
            NSMutableArray <UIApplicationShortcutItem *> *updatedShortcutItems = [existingShortcutItems mutableCopy];
            NSInteger numberOfActions = [existingShortcutItems count];
            [updatedShortcutItems addObject:[self createItemNumber:numberOfActions]];
            [[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];
        }else{
            [UIApplication sharedApplication].shortcutItems = @[[self createItemNumber:0]];
        }
    }
    
    - (UIApplicationShortcutItem*)createItemNumber:(NSInteger)number{
        UIApplicationShortcutItem *newItem = [[UIApplicationShortcutItem alloc]initWithType:[NSString stringWithFormat:@"type%ld",number]
                                                                             localizedTitle:[NSString stringWithFormat: NSLocalizedString(@"Action %ld", nil),number]
                                                                          localizedSubtitle:nil
                                                                                       icon:nil
                                                                                   userInfo:nil];
        return newItem;
    
    }
    
    - (void) removeActionToShortCutItems{
        NSArray <UIApplicationShortcutItem *> *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
        NSMutableArray <UIApplicationShortcutItem *> *updatedShortcutItems = [existingShortcutItems mutableCopy];
        [updatedShortcutItems removeObjectAtIndex:[updatedShortcutItems count]-1];
        [[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];
    }
    

    Hope it helps and feedback are welcome!

    0 讨论(0)
  • 2021-02-04 11:21

    You can use the following code to add shortcutitem for you app dynamic:

    UIApplicationShortcutIcon * photoIcon = [UIApplicationShortcutIcon iconWithTemplateImageName: @"selfie-100.png"]; // your customize icon
    UIApplicationShortcutItem * photoItem = [[UIApplicationShortcutItem alloc]initWithType: @"selfie" localizedTitle: @"take selfie" localizedSubtitle: nil icon: photoIcon userInfo: nil];
    UIApplicationShortcutItem * videoItem = [[UIApplicationShortcutItem alloc]initWithType: @"video" localizedTitle: @"take video" localizedSubtitle: nil icon: [UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypeCaptureVideo] userInfo: nil];
    
    [UIApplication sharedApplication].shortcutItems = @[photoItem,videoItem];
    
    0 讨论(0)
提交回复
热议问题