热门搜索 :
考研考公
您的当前位置:首页正文

关于UIApplicationShortcutItem注意

来源:东饰资讯网
1.动态设置和静态设置
  • 动态设置就是在代码中添加或更新shortcutItem.相关的类有
    UIApplicationShortcutItemUIApplicationShortcutIcon

  • 静态设置是指在Info.plist文件中,用键值对的方式添加. 需要自己添加,xcode貌似不会自动提示key. - -|||

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

Top