web-dev-qa-db-fra.com

Exemple de code sur les UIApplicationShortcutItems dynamiques

Je m'occupe de quelques exemples de code Obj-C pour un UIApplicationShortCutItem dynamique.

En gros, j'ai trois UIApplicationShortcutItems statiques et je ne souhaite les afficher que sous certaines conditions. Je suppose que vous ne pouvez pas modifier le statut visible d'un UIApplicationShortcutItem statique, je cherche donc un moyen simple d'ajouter des UIApplicationShortcutItems dynamiques.

12
fredpi

Vous pouvez utiliser le code suivant pour ajouter un raccourci pour votre application dynamique:

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];
21
chengpei

J'ai posté un exemple d'objectif-c simple sur GitHub qui ajoute/supprime des raccourcis vers l'écran d'accueil.

Vous pouvez le vérifier ici: https://github.com/cjimenezpacho/3Dtouch-home-screen-quick-actions

J'ai une méthode sur App Delegate qui gère les éléments de raccourci (basée sur une autre réponse stackoverflow que je n'ai pas trouvée :():

- (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;

}

Il est appelé par application: didFinishLaunchingWithOptions et application: performActionForShortcutItem si l'application est lancée ou non.

Et pour ajouter/supprimer des raccourcis sur demande:

- (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];
}

J'espère que cela aide et les commentaires sont les bienvenus!

4
Carlos Jiménez

Voici comment détecter si l'application a été lancée avec un raccourci rapide dans 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
    }
}

Pour détecter le type de raccourci sélectionné lorsque l'application est en cours d'exécution en arrière-plan.

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(@"%@", shortcutItem.type);
    if([shortcutItem.type isEqualToString:@"takePhotoAction"]){
        //ACTION HERE
    }
}
3
user1752054

Pour ceux qui recherchent une version Swift4 pour la réponse @chengpei, voici:

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]
1
iosCurator