gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:55:02 24 4
gpt4 key购买 nike

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

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

shortcutItem 是如何创建的 如果我在 (launchOption == nil) 返回 YES 之后动态创建 shortcutItem 图标和标题电话会显示 shortcutItems 吗?(因为 createShortcutItem 没有被调用所以我认为它不应该显示。)我能打开 shortcutItem 一次吗即使 didFinishLaunchingwithoptions 中未调用 shortcutItems 和图标,我还是打开了应用程序并将其最小化。

我在 ios 8.1 中的这一行崩溃了

shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];

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

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

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)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) application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
// Shortcut creation and methods
[ [ MyAppShortcuts instance ] createShortcutItem ];
[[FinAppShortcuts instance] handleShortCut:shortcutItem ];
}

最佳答案

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com