星空网 > 软件开发 > 操作系统

1014

/**
 *  当app进入后台时调用
 */
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /**
     *  app的状态
     *  1.死亡状态:没有打开app
     *  2.前台运行状态
     *  3.后台暂停状态:停止一切动画、定时器、多媒体、联网操作,很难再作其他操作
     *  4.后台运行状态
     */
    // 向操作系统申请后台运行的资格,能维持多久,是不确定的
    UIBackgroundTaskIdentifier task = [application beginBackgroundTaskWithExpirationHandler:^{
        // 当申请的后台运行时间已经结束(过期),就会调用这个block
        
        // 赶紧结束任务
        [application endBackgroundTask:task];
    }];
    
    // 在Info.plist中设置后台模式:Required background modes == App plays audio or streams audio/video using AirPlay
    // 搞一个0kb的MP3文件,没有声音
    // 循环播放
    
    // 以前的后台模式只有3种
    // 保持网络连接
    // 多媒体应用
    // VOIP:网络电话
}

 

----------------------------------------------------------------------------------------------------

- (void)test {

    // 获得未读数
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(setupUnreadCount) userInfo:nil repeats:YES];
    // 主线程也会抽时间处理一下timer(不管主线程是否正在其他事件)
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}

 


/**
 *  获得未读数
 */
- (void)setupUnreadCount
{
//    HWLog(@"setupUnreadCount");
//    return;
    // 1.请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    // 2.拼接请求参数
    HWAccount *account = [HWAccountTool account];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"access_token"] = account.access_token;
    params[@"uid"] = account.uid;
    
    // 3.发送请求
    [mgr GET:@"https://rm.api.weibo.com/2/remind/unread_count.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
        // 微博的未读数
//        int status = [responseObject[@"status"] intValue];
        // 设置提醒数字
//        self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", status];
        
        // @20 --> @"20"
        // NSNumber --> NSString
        // 设置提醒数字(微博的未读数)
        NSString *status = [responseObject[@"status"] description];  // 用 description 将  NSNumber 轻易转为 字符串对象
        if ([status isEqualToString:@"0"]) { // 如果是0,得清空数字
            self.tabBarItem.badgeValue = nil;
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        } else { // 非0情况
            self.tabBarItem.badgeValue = status;
            [UIApplication sharedApplication].applicationIconBadgeNumber = status.intValue;
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        HWLog(@"请求失败-%@", error);
    }];
}

----------------------------------------------------------------------------------------------------

 




原标题:1014

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

陆运缅甸专线:https://www.goluckyvip.com/tag/37639.html
无货源店群:https://www.goluckyvip.com/tag/3764.html
陆运双清:https://www.goluckyvip.com/tag/37640.html
陆运泰国专线:https://www.goluckyvip.com/tag/37641.html
陆运新加坡:https://www.goluckyvip.com/tag/37642.html
陆运专线俄罗斯:https://www.goluckyvip.com/tag/37643.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流