13----

通过创建定时器每5秒检查服务器上的新好友动态,并在有更新时显示未读通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建定时器,每隔 5 秒向服务器询问是否有新的好友动态
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timeClick:) userInfo:nil repeats:YES];

- ( void )timeClick:( NSTimer *)timer
{
   
AppDelegate *delegate = ( AppDelegate *)[ UIApplication sharedApplication ]. delegate ;
   
NSString *uid = delegate. sinaweibo . userID ;
   
// 判断,如果当前还没有登录,则不做任何事情
   
if (uid. length == 0 )
    {
       
return ;
    }
   
   
NSMutableDictionary *params = [ NSMutableDictionary dictionaryWithObject :uid forKey : @"uid" ];
   
// 访问接口,进行网络请求
    [
DataService requestWithURL : unread_count params :params httpMethod : @"GET" finishDidBlock :^( AFHTTPRequestOperation *operation, id result) {
       
if ( badgeView == nil )
        {
           
badgeView = [[ ThemeImageView alloc ] initWithFrame : CGRectMake ( kScreenWidth / 5 - 32 , 0 , 32 , 32 )];
           
badgeView . imgName = @"number_notify_9.png" ;
            [
_tabBarView addSubview : badgeView ];
           
           
// 显示未读数的 label
           
badgeLabel = [[ ThemeLabel alloc ] initWithFrame : badgeView . bounds ];
           
badgeLabel . textAlignment = NSTextAlignmentCenter ;
           
badgeLabel . font = [ UIFont systemFontOfSize : 13.0 ];
           
badgeLabel . colorName = @"Timeline_Notice_color" ;
            [
badgeView addSubview : badgeLabel ];
        }
       
// 请求成功
       
NSNumber *status = [result objectForKey : @"status" ];
       
NSInteger unRead = [status integerValue ];
       
       
if (unRead > 0 )
        {
           
// 有未读微博
           
badgeView . hidden = NO ;
           
           
// 当未读数大于 99 时,显示 99
           
if (unRead >= 99 )
            {
                unRead =
99 ;
            }
           
           
badgeLabel . text = [ NSString stringWithFormat : @"%ld" ,unRead];
        }
       
else
        {
           
// 没有未读微博
           
badgeView . hidden = YES ;
        }
    }
failureBlock :^( AFHTTPRequestOperation *operation, NSError *error) {
       
// 请求失败
       
NSLog ( @"unread error" );
    }];
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值