2015-09-27 10 views
10

Objective-C言語を使用して動的UIApplicationShortCutItemのサンプルコードを探しています。動的UIApplicationShortcutItems(Objective-C)のサンプルコード

基本的に、私は3つの静的UIApplicationShortcutItemsを持っています。私は、アプリ内の特定のブール値が真である場合にのみ、それらを表示したいと思います。私はあなたが静的なUIApplicationShortcutItemの可視状態を変更することはできないと推測しているので、簡単な方法で動的なUIApplicationShortcutItemを追加しています。

これについての良いチュートリアル(Objective-C)を知っている人もいますし、動的なUIApplicationShortcutItemを私のアプリに追加するサンプルコードもありますか?

答えて

18

あなたはダイナミックなアプリケーションのための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]; 
+0

Objective-Cのショートカットからアプリを起動した場合、どのように検知しますか? – user1752054

+0

あなたは 'application:didFinishLaunchingWithOptions'または' application:willFinishLaunchingWithOptions'をチェックすることができます。アプリがショートカットから起動されるかどうか。ショートカットから起動された場合、launchOptions辞書には 'UIApplicationLaunchOptionsShortcutItemKey'が含まれていなければなりません。そして、shortCutDemoリポジトリhttps ://github.com/cp0000/shortcutDemoを参照してください。 – chengpei

2

はここでアプリが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 
    } 
} 
+2

'didFinishLaunching'を呼び出す必要はありません。 'performActionForShortcutItem'は常に呼び出されます。 – yershuachu

4

私は、ホーム画面にショートカットを追加/削除することGitHubの上で簡単なObjective-Cの例を掲載しました。

あなたがここでそれを確認することができます:https://github.com/cjimenezpacho/3Dtouch-home-screen-quick-actions

を私は(私が見つけたことができない別の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; 

} 

それは、

アプリが起動されているかどうかをperformActionForShortcutItemと需要にショートカットを追加/削除する::didFinishLaunchingWithOptionsとアプリケーション:アプリケーションによって呼び出され

ご協力いただきありがとうございます。

関連する問題