2015-09-27 4 views
10

पर नमूना कोड मैं उद्देश्य-सी भाषा का उपयोग कर गतिशील UIApplicationShortCutItem के लिए कुछ नमूना कोड की देखभाल कर रहा हूं।गतिशील UIAplplicationShortcutItems (उद्देश्य-सी)

असल में, मेरे पास तीन स्थिर UIApplicationShortcutItems हैं और मैं केवल उन्हें प्रदर्शित करना चाहता हूं जब मेरे ऐप में एक विशिष्ट बूलियन सत्य है। मुझे लगता है कि आप एक स्थिर UIApplicationShortcutItem की दृश्य स्थिति को नहीं बदल सकते हैं, इसलिए मैं गतिशील UIApplicationShortcutItem एस जोड़ने के लिए एक आसान तरीका ढूंढ रहा हूं।

क्या कोई इस बारे में एक अच्छा ट्यूटोरियल (उद्देश्य-सी) जानता है या मेरे ऐप में गतिशील 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

उद्देश्य-सी में शॉर्टकट से कोई ऐप लॉन्च होने पर आप कैसे पता लगाते हैं? – user1752054

+0

आप 'एप्लिकेशन में जांच सकते हैं: didFinishLaunchingWithOptions' या' application: willFinishLaunchingWithOptions: 'क्या ऐप शॉर्टकट से लॉन्च किया जा रहा है। अगर ऐप शॉर्टकट से लॉन्च किया गया है, तो लॉन्चऑप्शन डिक्शनरी में' यूआईएप्लिकेशंस लॉन्चऑप्शनशॉर्टकट इटैमकी 'शामिल होना चाहिए। और आप मेरे शॉर्टकूट डेमो रिपोजिटरी https को क्लोन कर सकते हैं : //github.com/cp0000/shortcutDemo, और अधिक जानकारी प्राप्त करें। – chengpei

2

यहाँ कैसे करता है, तो अनुप्रयोग ऑब्जेक्टिव-सी में एक त्वरित शॉर्टकट के साथ शुरू किया गया था पता लगाने के लिए है।

- (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

कॉल करने के लिए कोई ज़रूरत नहीं है 'फिनिश लांचिंग'। 'performActionForShortcutItem' हमेशा कॉल किया जाता है। – yershuachu

4

मैंने गिटहब पर एक साधारण उद्देश्य-सी उदाहरण पोस्ट किया है जो होम स्क्रीन पर शॉर्टकट जोड़/हटाता है।

आप इसे यहाँ देख सकते हैं:

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

} 

यह: https://github.com/cjimenezpacho/3Dtouch-home-screen-quick-actions

मुझे लगता है कि शॉर्टकट आइटम (एक और stackoverflow जवाब है कि मैं नहीं मिला सकते हैं :(के आधार पर) संभालती अनुप्रयोग प्रतिनिधि पर एक विधि है आवेदन के द्वारा कहा जाता है: didFinishLaunchingWithOptions और आवेदन:। 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]; 
} 

उम्मीद है कि यह मदद करता है और प्रतिक्रिया का स्वागत है!

संबंधित मुद्दे