即使用户最小化了应用,我也需要在应用中运行NSTimer。计时器用于每隔15秒点击一次Web服务,4分钟后我需要执行某些导航。怎么办呢?
我知道此问题也已提前提出,但我未能找到合适的答案。
答案 0 :(得分:3)
你需要一个可能的理由。 如果它适用于导航应用程序,VOIP,或仅用于后台下载(在某些情况下),则可能。 但除此之外,你不能这样做。
答案 1 :(得分:1)
在继续之前,请考虑为什么当应用程序在后台运行时,运行计时器并不容易。这是因为它清空了手机的电池。如果你这样做,你会得到不满意的顾客。哪个苹果公司并不在乎,但苹果公司得到了不满意的顾客,他们非常关心这些顾客。在设置 - >下电池Apple将告诉用户哪个应用程序在过去24小时/ 6天内使用了多少能量,因此人们可以找到您的应用程序,如果它在后台浪费能源并将其丢弃。
答案 2 :(得分:0)
您可以将应用配置为在后台运行,但无法控制iOS在后台调用应用的频率。
我知道这是一个非常糟糕的解决方案和滥用服务,但您可以启动后台文件下载并使您的服务器发送数据的速度非常慢,大约需要4分钟。传输完成后,iOS将向您的应用报告。如果您的应用未终止,iOS也会向您的应用报告下载进度。但如果应用程序不在前台,则无法在传输完成时打开应用程序。
- 使用创建配置对象 backgroundSessionConfigurationWithIdentifier:方法 NSURLSessionConfiguration。
- 设置配置对象的值 sessionSendsLaunchEvents属性为YES。
- 如果您的应用开始转移 虽然它在前台,但建议你也设置 配置对象的自由选择属性为YES。
- 配置 适当的配置对象的任何其他属性。
- 使用 用于创建NSURLSession对象的配置对象。
醇>当与后台会话关联的所有任务都是 完成后,系统重新启动已终止的应用程序(假设是 sessionSendsLaunchEvents属性设置为YES,用户确实如此 不强制退出应用程序)并调用应用程序委托 应用:handleEventsForBackgroundURLSession:completionHandler: 方法
答案 3 :(得分:0)
AppDelegate.h //使用本地通知获取一些函数调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self getNotificationPermission];
return YES;
}
此方法用于许可
-(void)getNotificationPermission{
// Accept Action
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"Accept";
acceptAction.title = @"Yes i am OK";
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO;
// Reject Action
UIMutableUserNotificationAction *rejectAction = [[UIMutableUserNotificationAction alloc] init];
rejectAction.identifier = @"Reject";
rejectAction.title = @"On i am Not OK";
rejectAction.activationMode = UIUserNotificationActivationModeBackground;
rejectAction.destructive = YES;
rejectAction.authenticationRequired = YES;
// Reply Action
UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc] init];
replyAction.identifier = @"Reply";
replyAction.title = @"Reply";
replyAction.activationMode = UIUserNotificationActivationModeForeground;
replyAction.destructive = NO;
replyAction.authenticationRequired = YES;
// Email Category
UIMutableUserNotificationCategory *emailCategory = [[UIMutableUserNotificationCategory alloc] init];
emailCategory.identifier = @"Email";
[emailCategory setActions:@[acceptAction,rejectAction,replyAction] forContext:UIUserNotificationActionContextDefault];
[emailCategory setActions:@[acceptAction,rejectAction] forContext:UIUserNotificationActionContextMinimal];
// Download Action
UIMutableUserNotificationAction *downloadAction = [[UIMutableUserNotificationAction alloc] init];
downloadAction.identifier = @"Download";
downloadAction.title = @"Download";
downloadAction.activationMode = UIUserNotificationActivationModeForeground;
downloadAction.destructive = NO;
downloadAction.authenticationRequired = YES;
UIMutableUserNotificationAction *cancelAction = [[UIMutableUserNotificationAction alloc] init];
cancelAction.identifier = @"Cancel";
cancelAction.title = @"Cancel";
cancelAction.activationMode = UIUserNotificationActivationModeForeground;
cancelAction.destructive = YES;
cancelAction.authenticationRequired = YES;
// Image Category
UIMutableUserNotificationCategory *imageCategory = [[UIMutableUserNotificationCategory alloc] init];
imageCategory.identifier = @"Image";
[imageCategory setActions:@[downloadAction,cancelAction] forContext:UIUserNotificationActionContextDefault];
[imageCategory setActions:@[downloadAction,cancelAction] forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:emailCategory,imageCategory, nil];
UIUserNotificationType notificarionType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
// Register notification types and categories
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificarionType categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
// This line of code is required for remote push notification
//[[UIApplication sharedApplication] registerForRemoteNotifications];
}
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
//UIUserNotificationType notificationtype = [notificationSettings types];
}
// You can check the current user notifiartions settings whenever you need it
-(void)getReadyForNotification{
//UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
//[self checkSettings:settings];
}
// When appliation is in foregrounf this methid will get caled if you have scheduled a local notification
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
//CLRegion *region = notification.region;
if ([notification.category isEqualToString:@"Image"]) {
NSLog(@"Image category notification is presented to the user");
}else if([notification.category isEqualToString:@"Email"]){
NSLog(@"Email category notification is presented to the user");
}
}
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{
if ([identifier isEqualToString:@"Accept"]) {
[self printAlert:@"Accept"];
}else if ([identifier isEqualToString:@"Reject"]) {
[self printAlert:@"Reject"];
}else if ([identifier isEqualToString:@"Reply"]) {
[self printAlert:@"Reply"];
}else if ([identifier isEqualToString:@"Download"]) {
[self printAlert:@"Download"];
}else if ([identifier isEqualToString:@"Cancel"]) {
[self printAlert:@"Cancel"];
}
completionHandler();
}
-(void)printAlert:(NSString *)title
{
NSLog(@"%@ button is selected",title);
if ([title isEqualToString:@"Accept"])
{
}
else if ([title isEqualToString:@"Reject"])
{
}
}
// Callback for remote notification
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{
NSLog(@"handleActionWithIdentifier forRemoteNotification");
}
在ViewController上设置本地通知
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"How you are Feeling Today ?";
localNotification.category = @"Email"; // This should match categories identifier which we have defined in App delegate
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
使用此代码,如果用户在前景中选择通知,则在每个周期的时间段内获取本地通知,然后在您想要的特定阶段或屏幕中打开应用。这段代码也没有违反苹果指南。