2015-09-27 17 views
10

Sto cercando un codice di esempio per un dinamico UIApplicationShortCutItem utilizzando il linguaggio Objective-C.Codice di esempio su UIApplicationShortcutItems dinamico (Objective-C)

Fondamentalmente, ho tre statici UIApplicationShortcutItems e voglio solo visualizzarli quando uno specifico booleano nella mia app è vero. Presumo che non è possibile modificare lo stato visibile di uno statico UIApplicationShortcutItem, quindi mi occupo di un modo semplice per aggiungere dinamici UIApplicationShortcutItem s.

Qualcuno conosce un buon tutorial (Objective-C) su questo o ha anche qualche codice di esempio per aggiungere dinamici UIApplicationShortcutItem s alla mia app?

risposta

18

È possibile utilizzare il seguente codice per aggiungere shortcutitem per voi App dinamica:

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

Come si rileva se un'app viene avviata da una scorciatoia in Objective-C? – user1752054

+0

puoi effettuare il check-in 'application: didFinishLaunchingWithOptions' o' application: willFinishLaunchingWithOptions: 'se l'app avviata dal collegamento. Se l'app è lanciata dal collegamento, il dizionario launchOptions deve essere contenuto' UIApplicationLaunchOptionsShortcutItemKey'. E puoi clonare il mio repository shortCutDemo https : //github.com/cp0000/shortcutDemo e ottieni maggiori dettagli. – chengpei

2

Ecco come rilevare se l'applicazione è stata lanciata con un collegamento rapido 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 
    } 
} 

Per rilevare il tipo di collegamento selezionato mentre l'app è in esecuzione in background.

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

Non c'è bisogno di chiamare 'didFinishLaunching'. 'performActionForShortcutItem' viene sempre chiamato. – yershuachu

4

Ho inserito un semplice esempio obiettivo-c su GitHub che aggiunge/rimuove scorciatoie alla schermata principale.

È possibile controllare qui: https://github.com/cjimenezpacho/3Dtouch-home-screen-quick-actions

Ho un metodo su App delegato che gestisce gli elementi di scelta rapida (sulla base di un'altra risposta StackOverflow che non posso trovato :():

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

} 

E ' è chiamato da applicazione: didFinishLaunchingWithOptions e applicazione:. performActionForShortcutItem se l'applicazione viene avviata o meno

e per aggiungere/rimuovere i collegamenti a richiesta:

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

Spero che sia d'aiuto e il feedback sia benvenuto!