Ios Swift 3-即使应用程序在后台运行功能
我的iOS应用程序有问题。 我想每天运行一个函数,即使我的应用程序不在IPhone的前台。我试图使用Ios Swift 3-即使应用程序在后台运行功能,ios,swift,timer,background,swift3,Ios,Swift,Timer,Background,Swift3,我的iOS应用程序有问题。 我想每天运行一个函数,即使我的应用程序不在IPhone的前台。我试图使用NSTimer对象,但如果我的应用程序在后台,它就不起作用 我怎样才能做到这一点 注意:我的功能将触发一个通知,该通知根据当天的不同而有所不同。您不能这样做,因为您不能在后台运行,事实上,您的应用程序甚至可能没有运行。因此,重新思考您的架构。例如,在接下来的30天内设置30个通知,以防你的应用程序在此期间从未运行。这就是通知的含义:由系统代表您发送的消息,该消息始终在运行。您不能这样做,因为您不能
NSTimer
对象,但如果我的应用程序在后台,它就不起作用
我怎样才能做到这一点
注意:我的功能将触发一个通知,该通知根据当天的不同而有所不同。您不能这样做,因为您不能在后台运行,事实上,您的应用程序甚至可能没有运行。因此,重新思考您的架构。例如,在接下来的30天内设置30个通知,以防你的应用程序在此期间从未运行。这就是通知的含义:由系统代表您发送的消息,该消息始终在运行。您不能这样做,因为您不能在后台运行,事实上,您的应用程序甚至可能没有运行。因此,重新思考您的架构。例如,在接下来的30天内设置30个通知,以防你的应用程序在此期间从未运行。这就是通知的含义:由系统代表您发送的消息,它始终在运行。Paulw11是正确的。您可以使用后台刷新和本地通知。后台提取将被随机调用。每次调用时,本地通知都将重置。这段代码是OBJC,有点旧,但概念是一样的
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
BackgroundFetch * fetch = [[BackgroundFetch alloc] init];
[fetch fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
} successDictionary:^(NSDictionary *responseDict) {
// Schedule local notification, in this case it is 9am
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 9];
[components setMinute: 0];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
notif.fireDate = dateToFire;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.alertBody = [NSString stringWithFormat:@"%@: %@", responseDict[@"city"], responseDict[@"temp"]];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
保罗11是正确的。您可以使用后台刷新和本地通知。后台提取将被随机调用。每次调用时,本地通知都将重置。这段代码是OBJC,有点旧,但概念是一样的
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
BackgroundFetch * fetch = [[BackgroundFetch alloc] init];
[fetch fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
} successDictionary:^(NSDictionary *responseDict) {
// Schedule local notification, in this case it is 9am
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 9];
[components setMinute: 0];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
notif.fireDate = dateToFire;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.alertBody = [NSString stringWithFormat:@"%@: %@", responseDict[@"city"], responseDict[@"temp"]];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
您可以使用后台刷新,但无法设置执行时间。您可以将其与一些逻辑结合起来,在所需时间内安排本地通知。否则,您可以使用推送通知为什么即使应用程序未运行,您也需要每天运行一次?真正这样做的需要非常少。您可以使用后台刷新,但无法设置执行时间。您可以将其与一些逻辑结合起来,在所需时间内安排本地通知。否则,您可以使用推送通知为什么即使应用程序未运行,您也需要每天运行一次?真正需要这样做是非常罕见的。@ThBM:欢迎来到StackOverflow。这看起来是正确的答案。如果它解决了您的问题,请将其标记为已接受的答案。@ThBM:欢迎使用StackOverflow。这看起来是正确的答案。如果它解决了您的问题,请将其标记为已接受的答案。