zoukankan      html  css  js  c++  java
  • 应用程序添加角标和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文件,没有声音 循环播放!

      

  • 相关阅读:
    Linux Netcat命令
    clang-format
    keytool
    ip
    Linux iptables
    Linux yum源完全配置
    Makefile
    CMake
    HSTS
    开源镜像
  • 原文地址:https://www.cnblogs.com/daomul/p/4695887.html
Copyright © 2011-2022 走看看