0

こんにちは、よろしくお願いします。

3Dタッチを使用して、ホーム画面で新しいUIApplicationShortcutを使用するアプリケーションを作成しました。アプリがアプリになるとショートカットは機能しますが、アプリが閉じられている場合、ショートカットはホーム画面で機能しません。

アプリケーションを閉じると、ショートカットがアプリを起動しますが、ショートカットは機能せず、アプリケーションのみが起動します。

ショートカットに使用する 2 つの方法を次に示します。

- (void)setupViewControllers {
    // setup the navigation stack

    CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
    if (iOSDeviceScreenSize.height == 480)
    {
        _navigationController = (UINavigationController *)self.window.rootViewController;

        // iPhone 5 and iPod Touch 5th generation: 4 inch screen
        // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
        UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"Main4s" bundle:nil];

        UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        self.window.rootViewController  = initialViewController;

        self.myViewController = (ViewController *)_navigationController.topViewController;
        self.myViewController.managedObjectContext = self.managedObjectContext;

        [self.window makeKeyAndVisible];
    }

    if (iOSDeviceScreenSize.height == 568)
    {
        _navigationController = (UINavigationController *)self.window.rootViewController;

        // iPhone 5 and iPod Touch 5th generation: 4 inch screen
        // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
        UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        self.window.rootViewController  = initialViewController;

        self.myViewController = (ViewController *)_navigationController.topViewController;
        self.myViewController.managedObjectContext = self.managedObjectContext;

        [self.window makeKeyAndVisible];
    }

    if (self.myViewController){NSLog(@"myViewController is not nil, setupViewControllers");}
    else
    {
        NSLog(@"myViewController is nil, setupViewControllers");//breakpoint inserted here...
    }
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {

    [self setupViewControllers];

    // react to shortcut item selections
    NSLog(@"A shortcut item was pressed. It was %@.", shortcutItem.localizedTitle);
    if([shortcutItem.localizedTitle isEqualToString:@"Scan Tag"])
    {
        [self.myViewController toggleScanningTapped:nil];
    }
    if([shortcutItem.localizedTitle isEqualToString:@"Enter Tag"])
    {
        [self.myViewController manualEntry];
    }
    if([shortcutItem.localizedTitle isEqualToString:@"Search for a Tag"]){
        [self.myViewController SearchSwitch];
        [self.myViewController toggleScanningTapped:nil];
    }
    if([shortcutItem.localizedTitle isEqualToString:@"Just Scan & Copy"]){
        [self.myViewController justCopy];
    }

    if (self.myViewController){NSLog(@"myViewController is not nil, performActionForShortcutItem");}
    else
    {
        NSLog(@"myViewController is nil, performActionForShortcutItem");
    }
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [self setupViewControllers];

    NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
    if (url != nil && [url isFileURL]) {
        [self.myViewController handleOpenURL:url];
    }

    if (self.myViewController){NSLog(@"myViewController is not nil, didFinishLaunchingWithOptions");}
    else{NSLog(@"myViewController is nil, didFinishLaunchingWithOptions");}

    return YES;
}

ここにplistエントリがあります...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>com.PALIANTech.SUNYScanner.static1</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Scan Tag</string>
        <key>UIApplicationShortcutItemSubtitle</key>
        <string>Add tag to DB</string>
        <key>UIApplicationShortcutItemIconFile</key>
        <string>barcode.png</string>
    </dict>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>com.PALIANTech.SUNYScanner.static2</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Enter Tag</string>
        <key>UIApplicationShortcutItemSubtitle</key>
        <string>Manually add tag to DB</string>
        <key>UIApplicationShortcutItemIconFile</key>
        <string>ManualEntry.png</string>
    </dict>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>com.PALIANTech.SUNYScanner.static3</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Search for a Tag</string>
        <key>UIApplicationShortcutItemSubtitle</key>
        <string>Scan a tag to search for</string>
        <key>UIApplicationShortcutItemIconFile</key>
        <string>search.png</string>
    </dict>
</array>
</plist>

どんな助けや提案も大歓迎です。

4

1 に答える 1

2

その理由は、アプリを新たに起動するときは、とでショートカット アイテムをチェックした時点にself.myViewControllerとどまっているためです。だから何も起こりません。nilperformActionForShortcutItemdidFinishLaunchingWithOptions

これを修正するにmyViewControllerは、ショートカットを介してアプリを開いたときにインスタンス化してナビゲーション スタックに追加する必要があります。

編集

これは機能する(簡素化された)バージョンです:

AppDelegate

@interface AppDelegate ()
@property ViewController *viewController;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self setupViewControllers];

    return YES;
}

- (void)setupViewControllers {
    self.viewController = [[ViewController alloc] init];
    self.viewController.view.backgroundColor = [UIColor lightGrayColor];
    self.viewController.label.text = @"Normal Launch";
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    self.window.rootViewController  = navigationController;
    [self.window makeKeyAndVisible];
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    [self setupViewControllers];

    if([shortcutItem.localizedTitle isEqualToString:@"Scan Tag"]) {
        self.viewController.label.text = @"Shortcut: Scan Tag";
    }
    if([shortcutItem.localizedTitle isEqualToString:@"Enter Tag"]) {
        self.viewController.label.text = @"Shortcut: Enter Tag";
    }
    if([shortcutItem.localizedTitle isEqualToString:@"Search for a Tag"]) {
        self.viewController.label.text = @"Shortcut: Search Tag";
    }
    if([shortcutItem.localizedTitle isEqualToString:@"Just Scan & Copy"]) {
        self.viewController.label.text = @"Shortcut: Scan & Copy Tag";
    }
}

@end

このクラスは、デモンストレーション用のラベルだけを持つViewController単純なサブクラスです。UIViewController

バックグラウンドにあるアプリと、アプリがメモリから完全に削除された状態で、ショートカットをテストしました。どちらも意図したとおりに機能しました。

アップデート

ストーリーボードから ViewController を使用している場合は、次のように初期化する必要があります。

- (void)setupViewControllers {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    ...
}

ストーリーボードで ViewController の識別子を設定することを忘れないでください。

ここに画像の説明を入力

于 2015-10-13T13:17:06.937 に答える