应用程序添加角标和tabBar添加角标,以及后台运行时显示

1、设置角标的代码:

     // 从后台取出来的数据可能是int型的不能直接给badgeValue(string类型的),需要通过description转

     NSString *count = [responseObject[@"count"] description];

         if ([count isEqualToString:@"0"]) {

             self.tabBarItem.badgeValue = nil; //设置tabBar的角标

             [UIApplication sharedApplication].applicationIconBadgeNumber = 0;// 设置应用程序的角标

         }else

         {

             self.tabBarItem.badgeValue = count;

             [UIApplication sharedApplication].applicationIconBadgeNumber = status.intValue;

         }

 

2、在给应用程序发送角标设置的时候,可能会报以下错误:

  “Attempting to badge the application icon but haven't received permission from

  这是因为一切都是iOS8捣的鬼。您如果把模拟器换成iOS7.1或者更早的,就不会有这个问题。而现在在iOS8中要实现badge、alert和sound等都需要用户同意才能,因为这些都算做Notification“通知”,为了防止有些应用动不动给用户发送“通知”骚扰用户,所以在iOS8时,要“通知”必须要用户同意才行。

  下面代码的这个“通知设置”,主要是定义“通知类型”。同时我哦们把所有的通知类型都囊括进来,这样,我们就不需要以后一个一个的去设置alert和sound了。

  这段代码是在AppDelegate.m的 didFinishLaunchingWithOptions中实现:

  float sysVersion=[[UIDevice currentDevice]systemVersion].floatValue;

     if (sysVersion>=8.0) {

          UIUserNotificationType type=UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;

          UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:type categories:nil];

          [[UIApplication sharedApplication]registerUserNotificationSettings:setting];

      }

 

3、当然说到角标,肯定会有一个关于后台运行的问题,以前我们程序的后台模式只有3种才允许后台长时间,例如保持网络连接、多媒体应用、 VOIP:网络电话(现在很多了,比如蓝牙之类的)

  (1 那现在是什么情况呢?比如说我们要获取角标的数据,通过定时器(代码如下),如果是进入后台运行状态可能会导致定时器暂停

    //定时获得未读数据  

      NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(getUnreadCount) userInfo:nil repeats:YES];

      // !!主线程也会抽时间处理一下timer不管主线程是否正在执行其他事件操作——不加的话执行其他操作可能定时器会被忽略不执行

      [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

  (2 解决方法

    首先,我们还应该知道app的状态:  1.死亡状态:没有打开app  2.前台运行状态   3.后台暂停状态:停止一切动画、定时器、多媒体、联网操作,很难再作其他操作  4.后台运行状态。

  那么进入后台运行状态我们可以在在AppDelegate.m中调用applicationDidEnterBackground:

  /*

   * 程序进入后台的时候调用

   */

  - (void)applicationDidEnterBackground:(UIApplication *)application {

      // 向操作系统申请后台运行的资格,能维持多久,是不确定的  

      UIBackgroundTaskIdentifier task = [application beginBackgroundTaskWithExpirationHandler:^{

             // 当申请的后台运行时间已经结束(过期),就会调用这个block 

            // 过期则需要结束任务

            [application endBackgroundTask:task];

      }];

  当然这种方法是暂时性的,过期的话我们就没办法了吗?不!同时我们还可以假装是音频软件:在Info.plst中设置后台模式:Required background modes == App plays audio or streams audio/video using AirPlay,然后搞一个0kb的MP3文件,没有声音 循环播放!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在UniApp中,可以通过以下步骤在TabBar添加角标: 1. 在App.vue中定义一个计算属性,用于获取需要显示角标的数量: ``` computed: { badgeCount() { return this.$store.state.badgeCount; } } ``` 2. 在TabBar.vue中,使用uni-icons组件添加角标图标,并根据计算属性的值控制角标是否显示: ``` <template> <view> <view class="tabbar"> <view v-for="(item, index) in tabbarList" :key="index" class="tabbar-item" :class="currentIndex === index ? 'active' : ''" @click="changeTabbar(index)" > <view class="icon"> <uni-icons :type="item.icon" size="24"></uni-icons> <!-- 添加角标 --> <view v-if="index === 2 && badgeCount > 0" class="badge">{{badgeCount}}</view> </view> <view class="title">{{item.text}}</view> </view> </view> </view> </template> <style> .badge { position: absolute; top: -6px; right: -6px; width: 16px; height: 16px; background-color: red; color: #fff; font-size: 12px; border-radius: 50%; display: flex; align-items: center; justify-content: center; } </style> ``` 在这个例子中,我们假设需要在第三个Tab添加角标,并且角标的数量通过Vuex状态管理实现。当badgeCount的值大于0角标显示出来。 3. 在Vuex中定义state和mutations用于管理角标数量: ``` const store = new Vuex.Store({ state: { badgeCount: 0 }, mutations: { increment(state) { state.badgeCount++; }, decrement(state) { state.badgeCount--; } } }); ``` 4. 在需要更新角标数量的地方,调用Vuex的mutations方法更新状态: ``` this.$store.commit('increment'); ``` 这样,在TabBar上就可以动态地显示角标了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值