获得未读微博数据的开发思路

1. 开启timer60秒请求一次微博数据。

2. 将timer添加到runloop上,不论主线程是否正在其他事件,都要抽时间处理一下timer。

3. 微博未读数:如果有未读的微博条数,要设置提醒数字,如果未读数为0,得清空提醒数字。

开发代码:

- (void)viewDidLoad

{

// 获得未读数

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];

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);

}];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 13,984评论 0 15
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,501评论 2 4
  • 网络解析:AFNnetworking详解 一、什么事AFN□在iOS开发中,使用比较广泛□全称是AFNetwork...
    Bo_cake阅读 899评论 0 1
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,797评论 1 9
  • 访问网络的基本流程:5点 HTTP协议 2.通信过程 --------------请求的方法-----get 和 ...
    光明程辉阅读 1,031评论 0 2