创建定时器,每隔
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" );
}];
{
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" );
}];
}