1. 背景:
隨著iOS9 和 iPhone 6s的普及爹梁,蘋果官方提供的3D Touch將帶給我們更好玩切平,更便捷的操作習(xí)慣头谜,桌面快捷菜單可謂是3D Touch功能中最實(shí)用的一個(gè)私爷,有了它煞聪,用戶不再需要進(jìn)入app后做額外的操作斗躏,便能快速進(jìn)入指定的頁面。
2. 前期工作:
由于手頭“并(wo)沒(xiang)有(yao)”iPhone 6s 的設(shè)備昔脯,很多人說瑟捣,那我怎么開發(fā)這個(gè)功能呢馋艺?不怕,github上早有大神寫好了模擬器的解決方案迈套。按照這個(gè)文檔上的方法依次執(zhí)行捐祠,你的模擬器也能喚出快捷菜單。
3. 正式接入
①.創(chuàng)建UIApplicationShortcutItem
我們先來看一下每個(gè)UIApplicationShortcutItem中能夠包含哪些信息
key | Description | required |
---|---|---|
UIApplicationShortcutItemType | 事件的唯一標(biāo)識(shí)桑李,可以通過這個(gè)標(biāo)識(shí)來辨別你具體點(diǎn)擊了哪個(gè)事件 | Y |
UIApplicationShortcutItemTitle | 標(biāo)題踱蛀,在沒有子標(biāo)題的情況下如果標(biāo)題太長能自動(dòng)換行 | Y |
UIApplicationShortcutItemSubtitle | 子標(biāo)題,在標(biāo)題的下方 | N |
UIApplicationShortcutItemIconType | 枚舉選取系統(tǒng)中的一個(gè)圖標(biāo)類型 | N |
UIApplicationShortcutItemIconFile | 自定義一個(gè)圖標(biāo)贵白,以單一顏色35x35的大小展示率拒,如果設(shè)置這個(gè),UIApplicationShortcutItemIconType將不起作用 | N |
UIApplicationShortcutItemUserInfo | 字典禁荒,里面可以添加各種key猬膨、value對(duì) | N |
UIApplicationShortcutItem 的創(chuàng)建有2種方式
- 第一種是在info.plist里面靜態(tài)添加:
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemType</key>
<string>3dtouch.homePage</string>
<key>UIApplicationShortcutItemTitle</key>
<string>首頁</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>這是首頁</string>
<key>UIApplicationShortcutItemIconFile</key>
<string>shouye.png</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>url</key>
<string>index</string>
</dict>
</dict>
<dict>
<key>UIApplicationShortcutItemType</key>
<string>3dtouch.guanzhupage</string>
<key>UIApplicationShortcutItemTitle</key>
<string>關(guān)注</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>這是關(guān)注</string>
<key>UIApplicationShortcutItemIconFile</key>
<string>guanzhu.png</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>url</key>
<string>guanzhu</string>
</dict>
</dict>
</array>
- 第二種是在程序初始化的時(shí)候用代碼動(dòng)態(tài)添加:
我們先看一下UIApplicationShortcutItem.h,發(fā)現(xiàn)它的使用非常簡單,習(xí)慣完全符合官方API固有方式呛伴,而且和之前那種方式所構(gòu)建的包含的信息是一一對(duì)應(yīng)的勃痴,其中有3個(gè)@interface分別是:
- UIApplicationShortcutIcon
- UIApplicationShortcutItem
- UIMutableApplicationShortcutItem
//創(chuàng)建快捷item的icon 即UIApplicationShortcutItemIconFile
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"money"];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"gouwuche"];
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"wode"];
//創(chuàng)建快捷item的userinfo 即UIApplicationShortcutItemUserInfo
NSDictionary *info1 = @{@"url":@"money"};
NSDictionary *info2 = @{@"url":@"gouWuche"};
NSDictionary *info3 = @{@"url":@"wode"};
//創(chuàng)建ShortcutItem
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"3dtouch.moneyPage" localizedTitle:@"資產(chǎn)" localizedSubtitle:@"這是資產(chǎn)" icon:icon1 userInfo:info1];
UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"3dtouch.shopPage" localizedTitle:@"購物車" localizedSubtitle:@"這是購物車" icon:icon2 userInfo:info2];
UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"3dtouch.mypage" localizedTitle:@"我的" localizedSubtitle:@"這是我的" icon:icon3 userInfo:info3];
//把原有的shortcutItems拿出來,把動(dòng)態(tài)的放進(jìn)去
NSArray *items = @[item1, item2, item3];
NSArray *existingItems = [UIApplication sharedApplication].shortcutItems;
NSArray *updatedItems = [existingItems arrayByAddingObjectsFromArray:items];
//塞回去
[UIApplication sharedApplication].shortcutItems = updatedItems;
最后我們來看一下效果:
看上去是不是非常和諧热康?其實(shí)我告訴你沛申,我們已經(jīng)踩到了坑里了
我在運(yùn)行中發(fā)現(xiàn):
NSArray *existingItems = [UIApplication sharedApplication].shortcutItems;
所獲得的existingItems并不是我們之前設(shè)置在info.plist里面的,而是上一次
[UIApplication sharedApplication].shortcutItems = updatedItems;
賦值給他的姐军,又因?yàn)槲易宰髀斆鞯淖隽艘淮?/p>
NSArray *updatedItems = [existingItems arrayByAddingObjectsFromArray:items];
所以我們每運(yùn)行一次铁材,shortcutItems中的元素個(gè)數(shù)就會(huì)多3個(gè),
那為什么展示出來沒有問題呢奕锌?
仔細(xì)看剛剛發(fā)的那張效果圖著觉,我擦,只有4個(gè)惊暴,對(duì)了饼丘,這個(gè)就是表象上不出錯(cuò)的原因,在API上并沒有寫shortcutItems有任何個(gè)數(shù)限制缴守,也沒有寫快捷窗口的個(gè)數(shù),但是實(shí)際上镇辉,最多只能顯示4個(gè)屡穗,而且shortcutItems這個(gè)里面的對(duì)象恐怕是早已被系統(tǒng)默默的存到了某個(gè)plist里了,每當(dāng)程序啟動(dòng)時(shí)忽肛,會(huì)向系統(tǒng)要app的Bundle Identifier對(duì)應(yīng)的shortcutItems村砂,并非我們事先想要的info.plist中的items,當(dāng)然以上只是我從現(xiàn)象做出的合理猜測(cè)屹逛,我們并不需要關(guān)心info.plist中的那些靜態(tài)item础废,只需要?jiǎng)討B(tài)創(chuàng)建的item直接打包賦值過去
[UIApplication sharedApplication].shortcutItems = @[item1, item2, item3];
至于只展示4個(gè)的問題汛骂,這個(gè)我們無能為力了,系統(tǒng)做了限制评腺。
②.Item點(diǎn)擊回調(diào)
當(dāng)app在后臺(tái)的時(shí)候UIApplication提供了一個(gè)回調(diào)方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0);
我們依據(jù)這個(gè)回調(diào)中的shortcutItem的type和userinfo來做出不同的事件處理,而最后的completionHandler在API的說明中我們看到當(dāng)應(yīng)用并非在后臺(tái)帘瞭,而是直接重新開進(jìn)程的時(shí)候,直接返回No蒿讥,那么這個(gè)時(shí)候蝶念,我們的回調(diào)會(huì)放在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UIApplication又給我們一個(gè)從launchOptions中獲取這個(gè)shortcutItem的key--UIApplicationLaunchOptionsShortcutItemKey,所以在這2個(gè)都進(jìn)行對(duì)shortcutItem的操作后芋绸,我們這個(gè)功能算是完成了
在didFinishLaunchingWithOptions中媒殉,由于某些客戶端會(huì)有啟動(dòng)動(dòng)畫,所以這邊加了3秒摔敛,具體因程序而異
UIApplicationShortcutItem *item = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf)
{
[strongSelf actionWithShortcutItem:item];
}
});
在performActionForShortcutItem回調(diào)中
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler
{
if (shortcutItem)
{
[self actionWithShortcutItem:shortcutItem];
}
if (completionHandler)
{
completionHandler(YES);
}
}
最后就是統(tǒng)一處理actionWithShortcutItem的地方廷蓉,由于我這個(gè)demo中所有的type對(duì)應(yīng)的行為都一樣的,所以我這邊沒有對(duì)type做區(qū)分马昙,甚至所以的item可以用同一個(gè)type
-(void)actionWithShortcutItem:(UIApplicationShortcutItem *)item
{
if (item.userInfo)
{
NSLog(@"%@",item.userInfo[@"url"]);
}
}
好了桃犬,3D Touch的第一個(gè)功能就介紹到這里 Demo。