如何在iOS中创建每分钟一次的后台事件
我想做一个后台服务,比如,如果用户每分钟打开并关闭一个应用程序,就会显示一个动作视图,该视图也会从服务器端播放该视频。怎么可能呢?您可以在3天内完成此操作如何在iOS中创建每分钟一次的后台事件,ios,objective-c,iphone,ios7,cocos2d-iphone,Ios,Objective C,Iphone,Ios7,Cocos2d Iphone,我想做一个后台服务,比如,如果用户每分钟打开并关闭一个应用程序,就会显示一个动作视图,该视图也会从服务器端播放该视频。怎么可能呢?您可以在3天内完成此操作 本地通知-此时将显示本地通知,如果应用程序在后台终止或处于活动状态,则会显示本地通知。横幅会出现,如果您单击它,您可以决定在appdelegate中执行什么操作-这需要用户的可交互性才能工作 推送通知-同上,但通知来自服务器 NSTimer-仅当类在内存中时才起作用 创建一个NSTimer对象作为实例变量,以及一个整数来计算计数器 @imp
@implementation myClass {
NSTimer *myTimer, int counter;
}
-viewDidLoad{
counter = 61;
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkTimer) userInfo:nil repeats:YES];
}
-(void)checkTimer{
if(counter <=1){
counter = 61;
}
else {
counter --;
//do your video playing work here
}
}
@实现myClass{
NSTimer*myTimer,int计数器;
}
-viewDidLoad{
计数器=61;
myTimer=[NSTimer scheduledTimerWithTimeInterval:1.0目标:自选择器:@selector(checkTimer)userInfo:nil repeats:YES];
}
-(无效)校验计时器{
如果(计数器您可以在3天内完成此操作
本地通知-此时将显示本地通知,如果应用程序终止、处于后台或处于活动状态,则会显示本地通知。将出现一条横幅,如果您单击它,您可以决定将在appdelegate中执行什么操作-这需要用户的可交互性才能工作
推送通知-同上,但通知来自服务器
NSTimer-仅当类在内存中时才起作用
创建一个NSTimer对象作为实例变量,以及一个整数来计算计数器
@implementation myClass {
NSTimer *myTimer, int counter;
}
-viewDidLoad{
counter = 61;
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkTimer) userInfo:nil repeats:YES];
}
-(void)checkTimer{
if(counter <=1){
counter = 61;
}
else {
counter --;
//do your video playing work here
}
}
@实现myClass{
NSTimer*myTimer,int计数器;
}
-viewDidLoad{
计数器=61;
myTimer=[NSTimer scheduledTimerWithTimeInterval:1.0目标:自选择器:@selector(checkTimer)userInfo:nil repeats:YES];
}
-(无效)校验计时器{
如果(计数器[[UIApplication sharedApplication]取消所有本地通知]
NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateComponent12 = [gregCalendar12 components:NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:[NSDate date]];
[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:46];
UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:@"u got message!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSCalendarUnitMinute;
notification12.applicationIconBadgeNumber+=1;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];
[[UIApplication sharedApplication]取消所有本地通知]
NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateComponent12 = [gregCalendar12 components:NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:[NSDate date]];
[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:46];
UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:@"u got message!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSCalendarUnitMinute;
notification12.applicationIconBadgeNumber+=1;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];
什么???不清楚你在问什么。非常混乱。事实上,我想要像警报通知一样的背景通知。你能说一下如何在-(void)applicationidentinterbackground:(UIApplication*)应用程序中播放视频,…状态吗?NSDate*alarmTime=[[NSDate date]dateByAddingTimeInterval:5.0];UIApplication*app=[UIApplication sharedApplication];UILocalNotification*notifyAlarm=[[UILocalNotification alloc]init];如果(notifyAlarm){notifyAlarm.fireDate=alarmTime;notifyAlarm.timeZone=[NSTimeZone defaultTimeZone];notifyAlarm.repeatInterval=10;notifyAlarm.soundName=@'';notifyAlarm.alertBody=@“我在这个概念上是新手”;[app scheduleLocalNotification:notifyAlarm];}以上代码每隔5秒发送通知的方式与我想替换视图的方式相同,比如想播放视频什么???不清楚您在这里询问的是什么。非常混乱。实际上,我想要后台通知,比如警报通知。您可以说如何在-(void)applicationidentinterbackground:(UIApplication*)中播放视频吗应用程序,…状态?NSDate*alarmTime=[[NSDate date]dateByAddingTimeInterval:5.0];UIApplication*app=[UIApplication sharedApplication];UILocalNotification*notifyAlarm=[[UILocalNotification alloc]init];if(notifyAlarm){notifyAlarm.fireDate=alarmTime;notifyAlarm.timeZone=[NSTimeZone defaultTimeZone];notifyAlarm.repeatInterval=10;notifyAlarm.soundName=@“;notifyAlarm.alertBody=@“我是这个概念的新手”;[app scheduleLocalNotification:notifyAlarm];}以上代码每5秒发送通知的方式与我想替换视图的方式相同,如想播放视频。如果我关闭应用程序,是否可能会弹出视频?不,视频不会弹出,但会出现通知或横幅,如果用户与该横幅交互,则u可以决定将发生什么,但此时应用程序处于活动状态,因此代码必须部署在app delegate中。这仅适用于推送/本地通知。是否有其他方式显示视频?那么,警报是如何出现的,而设置警报一段时间后,它将以相同的方式弹出?它是在iOS中弹出视频吗?据我所知,在应用程序终止时或在后台无法播放视频是吗如果我关闭我的应用程序,视频是否会弹出?不,视频不会弹出,但会出现通知或横幅,如果用户与该横幅交互,则您可以决定将发生什么,但此时应用程序处于活动状态,因此代码必须部署在应用程序委托中。这仅适用于推送/本地通知是否有其他方式显示视频?那么警报是如何出现的,当设置警报一段时间后,它将以相同的方式弹出?它是在iOS中弹出视频吗?据我所知,在应用程序终止时或在后台无法播放视频