关于UIApplicationShortcutItem注意

2016-11-11  本文已影响338人  Abekkkkk

看了很多文章的介绍,最后还是看官方文档最清楚。

1.动态设置和静态设置
<key>UIApplicationShortcutItems</key>
    <array>
        <dict>
            <key>UIApplicationShortcutItemIconFile</key>
            <string>open-favorites</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>Favorites</string>
            <key>UIApplicationShortcutItemType</key>
            <string>com.mycompany.myapp.openfavorites</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>key1</key>
                <string>value1</string>
            </dict>
        </dict>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeCompose</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>New Message</string>
            <key>UIApplicationShortcutItemType</key>
            <string>com.mycompany.myapp.newmessage</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>key2</key>
                <string>value2</string>
            </dict>
        </dict>
    </array>
2.区别

静态设置是在应用安装的时候完成加载的,而动态设置需要在运行到对应代码时(runtime) 才加载,所以同时有静态加载的Item和动态加载的Item时,静态加载的Item会排在前面。

3.运用

文档推荐对可以直接使用的一些功能进行静态设置,而对于需要达到一些要求之后才能使用的Item就进行动态加载,并且可能一些静态加载的Item在App使用之后可能出现功能或者显示的变化,可以通过动态加载的方式进行更新。

    UIApplicationShortcutItem *anExistingShortcutItem = [existingShortcutItems objectAtIndex:anIndex];

    NSMutableArray <UIApplicationShortcutItem *> *updatedShortcutItems = [existingShortcutItems mutableCopy];

    UIMutableApplicationShortcutItem *aMutableShortcutItem = [anExistingShortcutItem mutableCopy];

    [aMutableShortcutItem setLocalizedTitle: @"New Title"];

    [updatedShortcutItems replaceObjectAtIndex:anIndex withObject: aMutableShortcutItem];

    [[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];

4.扩展:UIApplicationShortcutWidget

iOS 10中添加了UIApplicationShortcutWidget这个key,用于在桌面使用3D Touch时显示widget. 这个key只要写在Info.plist中就可以了,它的值就设置为对应的widget的bound id.

参考:Information Property List Key Reference

上一篇 下一篇

猜你喜欢

热点阅读