Ich suche nach Obj-C-Beispielcode für eine dynamische UIApplicationShortCutItem
.
Grundsätzlich habe ich drei statische UIApplicationShortcutItems
und möchte sie nur unter bestimmten Bedingungen anzeigen. Ich gehe davon aus, dass Sie den sichtbaren Status einer statischen UIApplicationShortcutItem
nicht ändern können, also suche ich nach einer einfachen Möglichkeit, dynamische UIApplicationShortcutItem
s hinzuzufügen.
Sie können den folgenden Code verwenden, um ein Verknüpfungselement für Ihre App-Dynamik hinzuzufügen:
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];
Ich habe ein einfaches Objective-c-Beispiel auf GitHub veröffentlicht, das Verknüpfungen zum Startbildschirm hinzufügt/entfernt.
Sie können es hier überprüfen: https://github.com/cjimenezpacho/3Dtouch-home-screen-quick-actions
Ich habe eine Methode für App Delegate, die Verknüpfungselemente behandelt (basierend auf einer anderen Antwort von stackoverflow, die ich nicht finden kann :():
- (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;
}
Es wird von application: didFinishLaunchingWithOptions und application: performActionForShortcutItem aufgerufen, unabhängig davon, ob die App gestartet wird oder nicht.
Und um Verknüpfungen bei Bedarf hinzuzufügen oder zu entfernen:
- (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];
}
Hoffe, es hilft und Feedback ist willkommen!
So können Sie feststellen, ob die App mit einer kurzen Verknüpfung in Objective-c gestartet wurde.
- (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
}
}
So erkennen Sie den Typ der ausgewählten Verknüpfung, während die App im Hintergrund ausgeführt wird.
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(@"%@", shortcutItem.type);
if([shortcutItem.type isEqualToString:@"takePhotoAction"]){
//ACTION HERE
}
}
Für jemanden, der nach einer Swift4-Version für @chengpei sucht, ist dies hier:
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]