动态UIApplicationShortcutItems上的示例代码

时间:2015-09-27 07:52:09

标签: objective-c ios9

我正在为动态UIApplicationShortCutItem设置一些Obj-C示例代码。

基本上,我有三个静态UIApplicationShortcutItems,我只想在某些条件下显示它们。我假设您无法更改静态UIApplicationShortcutItem的可见状态,因此我正在寻找一种添加动态UIApplicationShortcutItem的简单方法。

4 个答案:

答案 0 :(得分:21)

您可以使用以下代码为app动态添加shortcutitem:

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

答案 1 :(得分:4)

我在GitHub上发布了一个简单的Objective-c示例,用于添加/删除主屏幕的快捷方式。

您可以在此处查看:https://github.com/cjimenezpacho/3Dtouch-home-screen-quick-actions

我在App Delegate上有一个处理快捷项的方法(基于另一个我找不到的stackoverflow答案:():

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

}

它由应用程序调用:didFinishLaunchingWithOptions和application:performActionForShortcutItem是否启动应用程序。

根据需要添加/删除快捷方式:

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

希望它有所帮助,欢迎反馈!

答案 2 :(得分:3)

以下是检测应用程序是否在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
    }
}

检测应用程序在后台运行时选择的快捷方式类型。

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

答案 3 :(得分:1)

对于正在寻找@chengpei答案的Swift4版本的人,在这里:

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]