1 // 2 // HVWUnreadCountParam.h 3 // HVWWeibo 4 // 5 // Created by hellovoidworld on 15/2/11. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWBaseParam.h"10 11 @interface HVWUnreadCountParam : HVWBaseParam12 13 /** uid true int64 需要獲取消息未讀數的用戶UID,必須是當前登錄用戶。*/14 @PRoperty(nonatomic, assign) int uid;15 16 /** false boolean 未讀數版本。0:原版未讀數,1:新版未讀數。默認為0。 */17 @property(nonatomic, assign) BOOL unread_message;18 19 @end
1 // 2 // HVWUnreadCountResult.h 3 // HVWWeibo 4 // 5 // Created by hellovoidworld on 15/2/11. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h>10 11 @interface HVWUnreadCountResult : NSObject12 13 /** int 新微博未讀數 */14 @property(nonatomic, assign) int status;15 16 /** int 新粉絲數 */17 @property(nonatomic, assign) int follower;18 19 /** int 新評論數 */20 @property(nonatomic, assign) int cmt;21 22 /** int 新私信數 */23 @property(nonatomic, assign) int dm;24 25 /** int 新提及我的微博數 */26 @property(nonatomic, assign) int mention_status;27 28 /** int 新提及我的評論數 */29 @property(nonatomic, assign) int mention_cmt;30 31 /** int 微群消息未讀數 */32 @property(nonatomic, assign) int group;33 34 /** int 私有微群消息未讀數 */35 @property(nonatomic, assign) int private_group;36 37 /** int 新通知未讀數 */38 @property(nonatomic, assign) int notice;39 40 /** int 新邀請未讀數 */41 @property(nonatomic, assign) int invite;42 43 /** int 新勛章數 */44 @property(nonatomic, assign) int badge;45 46 /** int 相冊消息未讀數 */47 @property(nonatomic, assign) int photo;48 49 /** 聊天未讀數 */50 @property(nonatomic, assign) int msgbox;51 52 /** 獲取“消息”未讀數 */53 - (int) messageUnreadCount;54 55 56 /** 獲取“發現”未讀數 */57 - (int) discoverUnreadCount;58 59 /** 獲取“我”未讀數 */60 - (int) profileUnreadCount;61 62 /** 總未讀數 */63 - (int) totalUnreadCount;64 65 @end66
1 // 2 // HVWUnreadCountResult.m 3 // HVWWeibo 4 // 5 // Created by hellovoidworld on 15/2/11. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWUnreadCountResult.h"10 11 @implementation HVWUnreadCountResult12 13 /** 獲取“消息”未讀數 */14 - (int) messageUnreadCount {15 return self.cmt + self.dm + self.mention_status + self.cmt + self.group + self.private_group + self.notice + self.invite;16 }17 18 /** 獲取“發現”未讀數 */19 - (int) discoverUnreadCount {20 return 0;21 }22 23 /** 獲取“我”未讀數 */24 - (int) profileUnreadCount {25 return self.follower + self.photo + self.badge + self.msgbox;26 }27 28 /** 總未讀數 */29 - (int) totalUnreadCount {30 return self.status + [self messageUnreadCount] + [self discoverUnreadCount] + [self profileUnreadCount];31 }32 33 @end
1 // 2 // HVWUnreadCountTool.h 3 // HVWWeibo 4 // 5 // Created by hellovoidworld on 15/2/11. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWBaseTool.h"10 #import "HVWUnreadCountParam.h"11 #import "HVWUnreadCountResult.h"12 13 @interface HVWUnreadCountTool : HVWBaseTool14 15 /** 獲取未讀消息數 */16 + (void) unreadCountWithParameters:(HVWUnreadCountParam *)parameters success:(void (^)(HVWUnreadCountResult *result))success failure:(void (^)(NSError *error))failure;17 18 @end
1 // 2 // HVWUnreadCountTool.m 3 // HVWWeibo 4 // 5 // Created by hellovoidworld on 15/2/11. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWUnreadCountTool.h"10 11 @implementation HVWUnreadCountTool12 13 /** 獲取未讀消息數 */14 + (void) unreadCountWithParameters:(HVWUnreadCountParam *)parameters success:(void (^)(HVWUnreadCountResult *result))success failure:(void (^)(NSError *error))failure {15 [self getWithUrl:@"https://rm.api.weibo.com/2/remind/unread_count.json" parameters:parameters resultClass:[HVWUnreadCountResult class] success:success failure:failure];16 }17 18 @end
1 // 2 // HVWTabBarViewController.m 3 // HVWWeibo 4 // 5 // Created by hellovoidworld on 15/1/31. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWTabBarViewController.h" 10 #import "HVWHomeViewController.h" 11 #import "HVWMessageViewController.h" 12 #import "HVWDiscoverViewController.h" 13 #import "HVWProfileViewController.h" 14 #import "HVWNavigationViewController.h" 15 #import "HVWTabBar.h" 16 #import "HVWComposeViewController.h" 17 #import "HVWUnreadCountTool.h" 18 #import "HVWUnreadCountParam.h" 19 #import "HVWAccountInfoTool.h" 20 #import "HVWAccountInfo.h" 21 22 @interface HVWTabBarViewController () <HVWTabBarDelegate> 23 24 /** 首頁控制器 */ 25 @property(nonatomic, strong) HVWHomeViewController *homeVC; 26 27 /** 消息控制器 */ 28 @property(nonatomic, strong) HVWMessageViewController *messageVC; 29 30 /** 發現控制器 */ 31 @property(nonatomic, strong) HVWDiscoverViewController *discoverVC; 32 33 /** 我控制器 */ 34 @property(nonatomic, strong) HVWProfileViewController *profileVC; 35 36 /** 上一次選擇的tabBarItem */ 37 @property(nonatomic, strong) UITabBarItem *lastSelectedTabBarItem; 38 39 @end 40 41 @implementation HVWTabBarViewController 42 43 - (void)viewDidLoad { 44 [super viewDidLoad]; 45 // Do any additional setup after loading the view. 46 47 // 使用自定義的TabBar 48 HVWTabBar *hvwTabBar = [[HVWTabBar alloc] init]; 49 hvwTabBar.hvwTabBarDelegate = self; 50 // 重設tabBar,由于tabBar是只讀成員,使用KVC相當于直接修改_tabBar 51 [self setValue:hvwTabBar forKey:@"tabBar"]; 52 53 // 添加子控制器 54 [self addAllChildViewControllers]; 55 56 // 設置未讀消息獲取 57 [self setupUnreadCount]; 58 } 59 60 /** 添加所有子控制器 */ 61 - (void) addAllChildViewControllers { 62 // 首頁 63 HVWHomeViewController *homeVC = [[HVWHomeViewController alloc] init]; 64 [self addChildViewController:homeVC WithTitle:@"首頁" image:@"tabbar_home" seletectedImage:@"tabbar_home_selected"]; 65 self.homeVC = homeVC; 66 67 // 打開app,設置首頁為上一次選擇的tabBarItem 68 self.lastSelectedTabBarItem = homeVC.tabBarItem; 69 70 // 消息 71 HVWMessageViewController *messageVC = [[HVWMessageViewController alloc] init]; 72 [self addChildViewController:messageVC WithTitle:@"消息" image:@"tabbar_message_center" seletectedImage:@"tabbar_message_center_selected"]; 73 self.messageVC = messageVC; 74 75 // 發現 76 HVWDiscoverViewController *discoverVC = [[HVWDiscoverViewController alloc] init]; 77 [self addChildViewController:discoverVC WithTitle:@"發現" image:@"tabbar_discover" seletectedImage:@"tabbar_discover_selected"]; 78 self.discoverVC = discoverVC; 79 80 // 我 81 HVWProfileViewController *profileVC = [[HVWProfileViewController alloc] init]; 82 [self addChildViewController:profileVC WithTitle:@"我" image:@"tabbar_profile" seletectedImage:@"tabbar_profile_selected"]; 83 self.profileVC = profileVC; 84 } 85 86 /** 添加tab子控制器 */ 87 - (void) addChildViewController:(UIViewController *) viewController WithTitle:(NSString *) title image:(NSString *) imageName seletectedImage:(NSString *) selectedImageName { 88 89 // 設置隨機背景色 90 // viewController.view.backgroundColor = RandomColor; 91 92 // 設置標題,直接設置title可以同時設置tabBarItem和navigationItem的title 93 // viewController.tabBarItem.title = title; 94 // viewController.navigationItem.title = title; 95 viewController.title = title; 96 97 // 設置圖標 98 viewController.tabBarItem.image = [UIImage imageWithNamed:imageName]; 99 100 // 被選中時圖標101 UIImage *selectedImage = [UIImage imageWithNamed:selectedImageName];102 // 如果是iOS7,不要渲染被選中的tab圖標(iOS7中會自動渲染成為藍色)103 if (iOS7) {104 selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];105 }106 viewController.tabBarItem.selectedImage = selectedImage;107 108 // 添加子控制器109 HVWNavigationViewController *nav = [[HVWNavigationViewController alloc] initWithRootViewController:viewController];110 [self addChildViewController:nav];111 }112 113 #pragma mark - HVWTabBarDelegate114 /** “+”按鈕點擊代理方法 */115 - (void)tabBarDidComposeButtonClick:(HVWTabBar *)tabBar {116 HVWComposeViewController *composeView = [[HVWComposeViewController alloc] init];117 118 // tabBarController不是由navigationController彈出來的,沒有navigationController119 // [self.navigationController pushViewController:vc animated:YES];120 // HVWLog(@"%@", self.navigationController); // null121 122 // 為了使用導航欄,使用NavigationController包裝一下123 HVWNavigationViewController *nav = [[HVWNavigationViewController alloc] initWithRootViewController:composeView];124 // 使用modal方式彈出125 [self presentViewController:nav animated:YES completion:nil];126 }127 128 /** 選中了某個tabBarItem */129 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {130 // 如果是首頁131 UINavigationController *nav = (UINavigationController *)self.selectedViewController;132 133 if ([[nav.viewControllers firstObject] isKindOfClass:[HVWHomeViewController class]]) { // 如果是“首頁”item被點擊134 if (self.lastSelectedTabBarItem == item) { // 重復點擊135 [self.homeVC refreshStatusFromAnother:NO];136 } else { // 跳轉點擊137 [self.homeVC refreshStatusFromAnother:YES];138 }139 } else {140 // item.badgeValue = nil;141 }142 143 }144 145 #pragma mark - unread count146 /** 設置未讀消息獲取 */147 - (void) setupUnreadCount {148 // 第一次要先獲取一次149 [self fetchUnreadCount];150 151 // 設置定時器,每隔一段時間獲取一次152 NSTimer *unreadTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(fetchUnreadCount) userInfo:nil repeats:YES];153 154 // 要設置系統分配資源,不然用戶操作的時候會阻塞155 [[NSRunLoop mainRunLoop] addTimer:unreadTimer forMode:NSRunLoopCommonModes];156 }157 158 /** 設置刷新未讀消息定時任務 */159 - (void) fetchUnreadCount {160 // 設置參數161 HVWUnreadCountParam *param = [[HVWUnreadCountParam alloc] init];162 param.uid = [HVWAccountInfoTool accountInfo].uid.intValue;163 164 165 // 發送請求166 [HVWUnreadCountTool unreadCountWithParameters:param success:^(HVWUnreadCountResult *result) {167 HVWLog(@"現在有%d未讀消息", [result totalUnreadCount]);168 // 未讀"微博"數169 if (result.status) {170 self.homeVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", result.status];171 } else {172 self.homeVC.tabBarItem.badgeValue = nil;173 }174 175 // 未讀"消息"數176 if ([result messageUnreadCount]) {177 self.messageVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", [result messageUnreadCount]];178 } else {179 self.messageVC.tabBarItem.badgeValue = nil;180 }181 182 // 未讀"發現"數183 if ([result discoverUnreadCount]) {184 self.discoverVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", [result discoverUnreadCount]];185 } else {186 self.discoverVC.tabBarItem.badgeValue = nil;187 }188 189 // 未讀“我”數190 if ([result profileUnreadCount]) {191 self.profileVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", [result profileUnreadCount]];192 } else {193 self.profileVC.tabBarItem.badgeValue = nil;194 }195 196 // 設置app圖標,(iOS8或以上要先獲得授權,在AppDelegate里面做了) 197 [UIapplication sharedApplication].applicationIconBadgeNumber = [result totalUnreadCount];198 } failure:^(NSError *error) {199 HVWLog(@"獲取未讀消息數失敗,error:%@", error);200 }];201 }202 203 @end
1 // 2 // AppDelegate.m 3 // UIWebViewdDemo 4 // 5 // Created by hellovoidworld on 15/1/30. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "AppDelegate.h"10 #import "HVWControllerTool.h"11 #import "SDImageCache.h"12 #import "SDWebImageManager.h"13 14 @interface AppDelegate ()15 16 @end17 18 @implementation AppDelegate19 20 21 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {22 // Override point for customization after application launch.23 24 // 啟動后顯示狀態欄25 UIApplication *app = [UIApplication sharedApplication];26 app.statusBarHidden = NO;27 28 // 設置window29 self.window = [[UIWindow alloc] init];30 self.window.frame = [UIScreen mainScreen].bounds;31 [self.window makeKeyAndVisible];32 33 // 設置根控制器34 [HVWControllerTool chooseRootViewController];35 36 // 允許修改app圖標角標37 if (iOS8) {38 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];39 [[UIApplication sharedApplication] registerUserNotificationSettings:settings];40 }41 42 return YES;43 }44 45 - (void)applicationWillResignActive:(UIApplication *)application {46 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.47 // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.48 }49 50 - (void)applicationDidEnterBackground:(UIApplication *)application {51 // 設置讓app在后臺也能運行52 UIBackgroundTaskIdentifier taskID = [application beginBackgroundTaskWithExpirationHandler:^{53 [application endBackgroundTask:taskID];54 }];55 }56 57 - (void)applicationWillEnterForeground:(UIApplication *)application {58 // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.59 }60 61 - (void)applicationDidBecomeActive:(UIApplication *)application {62 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.63 }64 65 - (void)applicationWillTerminate:(UIApplication *)application {66 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.67 }68 69 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {70 // 清除緩存71 [[SDImageCache sharedImageCache] clearMemory];72 73 // 停止下載74 [[SDWebImageManager sharedManager] cancelAll];75 }76 77 78 @end79
1 // HVWHomeViewController.m 2 /** 刷新數據 */ 3 - (void) refreshStatusFromAnother:(BOOL)isFromAnother { 4 if (!isFromAnother) { // 重復點擊首頁item 5 // 滾動到頂部 6 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 7 [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 8 9 if (self.tabBarItem.badgeValue.intValue) { // 有新消息10 // 刷新數據11 [self refreshLatestWeibo:self.refreshControl];12 }13 } else {14 15 }16 }
新聞熱點
疑難解答