アイコンバッチ

アイコンバッチを表示する方法を検索すると以下の用法が表示される 

[UIApplication sharedApplication].applicationIconBadgeNumber = 5;

 この通りやっても、表示されなかった。iOS9からは通知に関する設定を行わないといけなくなったようだ。


IOS9からは、ユーザーの認証が必要になったみただ。

下記のコードをAppDelegate.m 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 配下に実装する必要がある。

UIUserNotificationType types = UIUserNotificationTypeBadge;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:settings];

 初回実行時に、「〜( アプリ名)〜は通知を送信します

よろしいですか?」が表示されるようになる。許可する/しない の設定は、

後から変更できる。設定→通知→該当のアプリ。通知を許可をオン、Appアイコンにバッチを表示をオン。

これで、アイコンにバッチが表示されるようにはなったけど、

iOS10からは、非推奨のメソッドになっている!?


 iOS10でコンパイルエラーが出ないように書き直してみた。こんなのでいいのかなぁ?

AppDelegate.m

 

#import <UserNotifications/UserNotifications.h>   を冒頭で追加する必要あり



-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted){ NSLog(@"承認された"); }else{ NSLog(@"承認されなかった"); } }]; return YES; }

この例の場合、アイコンバッチだけの確認になっています。

[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge + 

UNAuthorizationOptionAlert

)  とすれば、複数の条件が申請できるみたい。

 

granted には、許可に認否結果が渡ってくる。

初回許可しても、後々設定から不許可にした場合も、grantedには NOが渡ってくる。


バッチの数量はいつくまで、表示できるのか?

6桁まではそのまま表示される。

7桁以上の場合、中間の桁が ... 表示となり省略されるようだ。

f:id:nabe_shodai:20170619000247p:plain  f:id:nabe_shodai:20170619000300p:plain

 


参考

iOS9でバッジが表示されない時の対処法
https://trueman-developer.blogspot.jp/2015/12/ios9.html

 

iOS User Notifications framework」シリーズまとめhttp://dev.classmethod.jp/smartphone/iphone/user-notifications-framework-16/

 

iOS 10 User Notifications Framework実装まとめhttp://qiita.com/mshrwtnb/items/3135e931eedc97479bb5