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!
fonte
2015-12-16 10:17:08
Come si rileva se un'app viene avviata da una scorciatoia in Objective-C? – user1752054
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