The results we want to achieve are as follows:
We use the system comes with, in fact, the code is very small, on the button we want to display, the following sentence code can be:
Self.tabBarItem.badgeValue = @ "1";
Set the code for both the icon and the button:
/** * get unread */-(void) setupunreadcount{ //Get Unread Specific method //Eventually return a number such as: int count=10; Set reminder number (unread on Weibo) nsstring *status = [NSString stringwithformat:@ "%d", count]; if ([Status isequaltostring:@ "0"]) {//If it is 0, you have to clear the number Self.tabBarItem.badgeValue = nil;
App icon Display [uiapplication sharedapplication].applicationiconbadgenumber = 0; } else {//not 0 case Self.tabBarItem.badgeValue = status; [UIApplication sharedapplication].applicationiconbadgenumber = Status.intvalue; }}
Of course, the setting of the display of this number, it should be timed to change. How do you make this method a timed call?
-(void) viewdidload{ [Super Viewdidload]; Get unread Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:60 target:self selector: @selector ( Setupunreadcount) Userinfo:nil Repeats:yes]; The main thread also takes time to process the timer (regardless of whether the main thread is in another event) [[Nsrunloop Mainrunloop] Addtimer:timer formode:nsrunloopcommonmodes];}
After finishing the above operation, we can see the effect. But one problem is that when our app goes backstage, the timer no longer works and becomes a temporary state. So why can a music app run in the background all the time? In fact, there is already a way to run the background in the appdelegate, we can do something in this method.
/** * call */-(void) Applicationdidenterbackground when the app enters the background: (UIApplication *) application{ /** * Status of the app * 1. Death Status: No app opened * 2. Foreground operation Status * 3. Background pause Status: Stop all animations, timers, multimedia, networking operations, and it's hard to do anything else * 4. Background running status * ////to the operating system to apply for background running qualification, how long it can last, is indeterminate uibackgroundtaskidentifier task = [application beginbackgroundtaskwithexpirationhandler:^{ //When the background run time of the application has ended (expired), it will call this block //rush to end the task [ Application Endbackgroundtask:task]; }]; Set the background mode in INFO.PLST: Required background modes = = APP plays audio or streams audio/video using AirPlay //engage in a 0kb MP3 text Pieces, no sound //loop playback// previous background mode only 3 kinds //Maintain network connection// Multimedia application //VOIP: Network Phone}
In the above code, we see the need to set the background mode in INFO.PLST: Required background modes = = APP plays audio or streams audio/video using AirPlay,
Finally make a 0kb MP3, no sound, loop play can.
iOS bottom button and app icon show unread messages