【问题标题】:Sample code on dynamic UIApplicationShortcutItems动态 UIApplicationShortcutItems 上的示例代码
【发布时间】:2015-09-27 07:52:09
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c ios9


    【解决方案1】:

    您可以使用以下代码为您的应用动态添加快捷方式:

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

    【讨论】:

    • 如何检测应用程序是否从 Objective-C 中的快捷方式启动?
    • 你可以在application:didFinishLaunchingWithOptions或者application:willFinishLaunchingWithOptions:查看应用是否是从快捷方式启动的。如果应用是从快捷方式启动的,那么launchOptions字典应该包含UIApplicationLaunchOptionsShortcutItemKey。你可以克隆我的shortCutDemo存储库github.com/cp0000/shortcutDemo,并获取更多详细信息。
    【解决方案2】:

    我在 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;
    
    }
    

    无论应用是否启动,都由 application: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];
    }
    

    希望对您有所帮助,欢迎提供反馈!

    【讨论】:

      【解决方案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
          }
      }
      

      【讨论】:

      • 无需致电didFinishLaunchingperformActionForShortcutItem 总是被调用。
      【解决方案4】:

      对于正在寻找@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]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-25
        • 2011-07-18
        • 2011-07-21
        • 1970-01-01
        • 2021-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多