• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为以前版本的应用程序崩溃创建快捷方式

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:20:08 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在一个应用程序中创建了一个 shortcutItem,当我在 ios 9.2 中运行时它工作正常。但是当我打开具有 ios 8.1 的应用程序时,它会崩溃。

Thread 1:EXC_BAD_ACCESS (code=1, address=0x0)

如果我在 (launchOption == nil) 返回 YES 之后动态创建 shortcutItem 图标和标题,那么如何创建 shortcutItem那么电话显示 shortcutItems 吗?(因为未调用 createShortcutItem 它不应该显示我的想法。)我可以打开一次 shortcutItem即使在 didFinishLaunchingwithoptions 中没有调用 shortcutItems 和图标,我打开的应用程序也已打开并最小化。

iOS 8.1 在这一行遇到崩溃

shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];

所以我返回 if launchOptions == nil 来修复崩溃。

以下是我的应用程序中用于使用快捷方式的代码。它是在我的主应用程序中创建的,因此我稍微修改了名称作为示例。

如何处理早期 iOS(8.0 起)或设备不支持的快捷方式。我希望我的应用同时支持 ios8 和更高版本。

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
  // Override point for customization after application launch.
  BOOL shouldPerformAdditionalDelegateHandling = YES;

  // Shortcut creation and methods
  [[MyAppShortcuts instance] createShortcutItem];

  if (launchOptions == nil) {
    return shouldPerformAdditionalDelegateHandling;
  }

  UIApplicationShortcutItem *shortcutItem;
  shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];

  if ( shortcutItem != nil ){
    // Open app from shortcut
    self.rootViewCtrl_.shortcutItem_ = shortcutItem;

    [[MyAppShortcuts instance] setMyAppShortcutIs:shortcutItem.type];
    [[MyAppShortcuts instance] setAppLaunchedWithShortcut:YES];

    // This will block "performActionForShortcutItem:completionHandler" from being called.
    shouldPerformAdditionalDelegateHandling = NO;
  }
  return shouldPerformAdditionalDelegateHandling;
}


- (void) createShortcutItem {
  UIApplicationShortcutIcon* firstIcon = [UIApplicationShortcutIcon iconWithTemplateImageName"shortcutFirstItem"];
  UIApplicationShortcutItem* firstItem;
  firstItem = [[UIApplicationShortcutItem alloc]initWithType: firstItemType
                                                 localizedTitle: NSLocalizedString(@"First Item", nil)
                                              localizedSubtitle: nil
                                                           icon: firstIcon
                                                       userInfo: nil];
  //..... creating 2nd 3rd 4th item
  [UIApplication sharedApplication].shortcutItems = @[firstItem, secondItem, thirdItem, fourthItem];
}

application:performActionForShortcutItem: 方法在每次使用快捷方式打开应用时调用。如果我在它每次调用的方法中创建 shortcutItems(icon, title and type) ,这是否会以任何方式影响打开快捷方式,因为一次又一次地创建项目?

- (void) applicationUIApplication *)application performActionForShortcutItemUIApplicationShortcutItem *)shortcutItem completionHandlervoid (^)(BOOL))completionHandler
{
    // Shortcut creation and methods
    [ [ MyAppShortcuts instance ] createShortcutItem ];
    [[FinAppShortcuts instance] handleShortCut:shortcutItem ];     
}



Best Answer-推荐答案


您应该在检查快捷方式项目是否可用后放置所有逻辑(iOS 9.1 及更高版本)。对于没有通过点击快捷方式调用的实例,我认为 launchOptions 不是 nil。

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    BOOL shouldPerformAdditionalDelegateHandling = YES;

    //checks if you could user shortcut items. only available in iOS 9.1 onwards
    if ([UIApplicationShortcutItem class]){

        // Shortcut creation and methods
        [[MyAppShortcuts instance] createShortcutItem];

        if (launchOptions[UIApplicationLaunchOptionsShortcutItemKey]){
            // Open app from shortcut
            self.rootViewCtrl_.shortcutItem_ = shortcutItem;
            [[MyAppShortcuts instance] setMyAppShortcutIs:shortcutItem.type];
            [[MyAppShortcuts instance] setAppLaunchedWithShortcut:YES];
            // This will block "performActionForShortcutItem:completionHandler" from being called.
            shouldPerformAdditionalDelegateHandling = NO;
        }

    }
    return shouldPerformAdditionalDelegateHandling;
}

关于ios - 为以前版本的应用程序崩溃创建快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34916648/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap