iOS开发常看

iOS 动态更新App图标和App内评分

2018-02-02  本文已影响148人  hanryChen

iOS10.3苹果新增了一些比较实用的功能,主要有:

开发者可以随时更新App的图标,不再需要通过更新App实现
允许App内评分及留言
SiriKit 允许第三方应用使用 Siri 进行支付

因为我们应用目前没有用到Siri功能,所以这里主要还是讲讲动态修改App图标和App内评分,以及那些需要注意的地方和坑点。

动态修改App图标

修改App图标的方法在UIApplication的UIAlternateApplicationIcons分类里,该分类很简单,只有两个属性和一个方法。来看看官方文档怎么写的吧

/* The value of this property is YES only when the system allows you to change the icon of your app. To declare your app's alternate icons, include them in the CFBundleIcons key of your app's Info.plist file. /
翻译
/
这个属性仅在系统允许你更新你的App icon时为YES,你要声明更新的icon在你App的info.plist文件里设置,以CFBundleIcons为key对应的值 */

@property (readonly, nonatomic) BOOL supportsAlternateIcons;

/*

Parameters
alternateIconName

The name of the alternate icon, as declared in the CFBundleAlternateIcons key of your app's Info.plist file. Specify nil if you want to display the app's primary icon, which you declare using the CFBundlePrimaryIcon key. Both keys are subentries of the CFBundleIcons key in your app's Info.plist file.

completionHandler

A handler to be executed with the results. After attempting to change your app's icon, the system reports the results by calling your handler. (The handler is executed on a UIKit-provided queue, and not necessarily on your app's main queue.) The handler has no return value and takes the following parameter:

error

On success, the value of this parameter is nil. If an error occurred, this parameter contains the error object indicating what happened and the value of the alternateIconName property remains unchanged.

Discussion

Use this method to change your app's icon to its primary icon or to one of its alternate icons. You can change the icon only if the value of the supportsAlternateIcons property is YES.

You must declare your app's primary and alternate icons using the CFBundleIcons key of your app's Info.plist file. For information about how to configure alternate icons for your app, see the description of the CFBundleIcons key in Information Property List Key Reference.
/
翻译
/

参数
alternateIconName

替补icon的名称,是你App的info.plist文件以CFBundleAlternateIcons为key的值。设置成nil会显示App的原始icon,即CFBundlePrimaryIcon为key对应名称的icon。这些key都在你App的info.plist文件的CFBundleIcons下级。

completionHandler

处理结果的回调. 在尝试更新你App的icon之后,系统会通过该回调返回结果. (该回调在UIKit提供的队列上,而不是主队列) 这个回调不需要返回值并且回带来以下的参数值:

error

如果调用成功,参数的值为nil。如果发生错误,该参数包含携带错误信息的对象,并且alternateIconName属性的值不会发生改变。

说明

调用这个方法去更新你App的icon成原始图片或者其中一个替补图标。只有在supportsAlternateIcons属性值为YES时才能更新icon。

你必须在你App的info.plist文件的CFBundleIcons键值对里设置你App的原始图标和替补图标。想了解更多有关如何设置你App替补icon的信息,请在属性列表键值对信息查看CFBundleIcons键的描述。
*/

- (void)setAlternateIconName:(nullable NSString *)alternateIconName completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler;

/*
When the system is displaying one of your app's alternate icons, the value of this property is the name of the alternate icon (from your app's Info.plist file). When the system is displaying your app's primary icon, the value of this property is nil.
/
翻译
/

当系统显示你App的其中一个替补图标时,这个属性的值是你替补图标的名称(该名称在你App的info.plist文件里设置)。当系统显示你App的原始图标时,该属性的值为nil。
*/

@property (nullable, readonly, nonatomic) NSString *alternateIconName;

看完官方文档之后,我们对更新图标已经有了大概的了解。要更新图标就要现在info.plist里添加key:CFBundleIcons,下级再添加替补图标的key:CFBundleAlternateIcons和原始图标的key:CFBundlePrimaryIcon。接下来我们就开始修改info.plist吧。


图标相关的plist设置

CFBundleIcons:一个字典,包含所有AppIcon信息。包含CFBundlePrimaryIcon和CFBundleAlternateIcons。
CFBundlePrimaryIcon:优先以Assets.xcassets中的APPicon为准,如果在Assets.xcassets里设置了AppIcon,那么CFBundlePrimaryIcon里面的配置就会被忽略。否则会以CFBundlePrimaryIcon配置的icon为AppIcon。
CFBundleAlternateIcons:一个数组,配置待更新图标的名称。
UIPrerenderedIcon:指定App的图标是否包含闪光效果,如果图标已经有这个效果,就把这个属性设置为YES以防止系统再次添加相同效果。如果设置为NO(默认值),iOS系统会自动添加这个效果。
CFBundlePrimaryIcon和CFBundleAlternateIcons设置的图标都不能放在Assets.xcassets中。

info.plist配置好之后,就可以通过代码更新图标了

if (@available(iOS 10.3, *)) {
        if ([[UIApplication sharedApplication] supportsAlternateIcons]) {
            NSString *name = @"NewAppIcon";
            [[UIApplication sharedApplication] setAlternateIconName:name completionHandler:^(NSError * _Nullable error) {
                NSLog(@"error === %@",error);
            }];
        }
    }
更新图标

ok,图标成功更新,但是还有一点小问题,弹窗提示的文字是英文的,我们需要把它改成中文。还是在info.plist里面,添加CFBundleAllowMixedLocalizations键,值设置为YES。


更新图标
App内评分

App内评分也很简单,导入StoreKit框架,在需要的地方写上如下的代码就行了:

if (@available(iOS 10.3, *)) {
        [SKStoreReviewController requestReview];
    }

代码虽然不多,但是按照习惯咱们还是看一遍官方文档吧。

/*

Discussion

Although you should call this method when it makes sense in the user experience flow of your app, the actual display of a rating/review request view is governed by App Store policy. Because this method may or may not present an alert, it's not appropriate to call it in response to a button tap or other user action.

Note

When you call this method while your app is still in development mode, a rating/review request view is always displayed so that you can test the user interface and experience. However, this method has no effect when you call it in an app that you distribute using TestFlight.
When you call this method in your shipping app and a rating/review request view is displayed, the system handles the entire process for you. In addition, you can continue to include a persistent link in the settings or configuration screens of your app that deep-links to your App Store product page. To automatically open a page on which users can write a review in the App Store, append the query parameter action=write-review to your product URL.
/
翻译
/

说明

尽管你应该在用户体验过程中调用这个方法,这个评分视图是由App Store策略管理的。因为这个方法可能也可能不弹出提醒,这个方法不适合在响应一个按钮或者其他的用户操作时调用。

注释

当你的App在开发模式时你调用这个方法,评分视图会展示给你去测试用户交互和体验。然而,当你使用从TestFlight分发的App时这个方法不会生效。
当你在你的App调用这个方法时,会显示一个评分视图,系统会为你处理整个过程。另外,你也能继续在你App的设置和配置界面包含一个持久的链接,跳转到App Store中你App的产品页面。为了自动打开一个页面,用户可以在App Store中编写回复,需要将参数action=write-review拼接到你App url的后面。
To automatically open a page on which users can write a review in the App Store, append the query parameter action=write-review to your product URL.
*/

应用内评分

App内评分弹窗的实现虽然简单,但需要注意几点:

app内评分弹窗提示一年只允许弹三次(不区分版本)。
如果用户在在系统设置里面关掉app内评分弹窗提示,app内好评弹窗将在未来的一年内无法展示。

上一篇下一篇

猜你喜欢

热点阅读