iOS的背景获取自定义间隔
问题描述:
我读到背景苹果所有的文档获取,目前我使用的是这样的:iOS的背景获取自定义间隔
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:minimumBackgroundFetchInterval];
我让操作系统来决定何时执行后台抓取,但如果我把它像这个:
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:21600];
这是否意味着每6h会发生一次fetch?
答
不,这意味着你的建议到iOS设备上的至少6小时后台之前应经过提取所进行的,但对于这个属性状态的文档 -
秒的最小数目必须经过之前可以启动另一个 后台提取。此值仅供参考,并且 未指示取回 操作之间预期的确切时间量。
因此,执行后台提取可能会超过六个小时,但可能不会更少。 iOS还会记录您通过完成处理程序返回的值,以指示是否有新数据来尝试确定当前可能存在应用程序新数据的时间。
答
我在iOS10和iPhone6Plus上做了一些实验,给出了“UIApplicationBackgroundFetchIntervalMinimum”间隔。 (当然我援引一些网络相关的方法给iOS的一个暗示,应用程序是真正的工作......并调用completionHandler(UIBackgroundFetchResultNewData);)
我得到:(运行所有的夜晚)
00:35 01:03 01:31 01:59 02:27 02:55 03:13 03:23 03:51 04:19 04:35 05:04 05:25 05 :59 06:27 06:56
因此,Delta从10分钟到34分钟不等。
答
代码的广告请求:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// You must invoke setMinimumBackgroundFetchInterval:.
[application setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum];
// default, iOS decides. seems between 5 and 15 minutes.
// you cannot go below that time.
/* ADC:
(You can also enable this support by including the UIBackgroundModes key with the fetch value in your app’s Info.plist file.) Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches. The system must balance your app’s need to fetch content with the needs of other apps and the system itself. After assessing that information, the system gives time to apps when there are good opportunities to do so.
When a good opportunity arises, the system WAKES or LAUNCHES your app into the background and calls the app delegate’s application:performFetchWithCompletionHandler: method. Use that method to check for new content and initiate a download operation if content is available.
*/
// UIApplicationState state = application.applicationState;
NSString * s = [NSString stringWithFormat:@"background=%d (didFinishLaunchingWithOptions)", application.applicationState == UIApplicationStateBackground];
return YES;
}
回电:
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
{
NSInteger n = application.applicationIconBadgeNumber;
application.applicationIconBadgeNumber = n+1;
// we invoke an sync method, so we should call handler AFTER getting answer...
BOOL isIpad = [self isIpad];
NSString * ID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSString* msg = [NSString stringWithFormat: @"PFCH=%ld-ipad%d-%@", (long)n, isIpad, ID];
// call a method on my server to send me back a mail. I add an ID for every device I tested in a battery of iPhone/iPads.
[self getTime: msg];
// if ok...
completionHandler(UIBackgroundFetchResultNewData);
// or completionHandler(UIBackgroundFetchResultNoData);
// or completionHandler(UIBackgroundFetchResultFailed);
}
另一种方法:
-(void) getTime:(NSString*)msg
{
NSString* urlS = [NSString stringWithFormat: @"https://www.ingconti.com/PFCH.php?msg=%@", msg];
NSURL * URL = [NSURL URLWithString: urlS];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString * s = [[NSString alloc]initWithData:data encoding: NSUTF8StringEncoding];
NSLog(@"%@", s);
}
];
[task resume];
}
-(BOOL)isIpad;
{
UIUserInterfaceIdiom deviceType = [[UIDevice currentDevice] userInterfaceIdiom];
if (deviceType == UIUserInterfaceIdiomPad)
return YES;
return NO;
}
我没有使用服务器作为我的设备可以处于休眠状态,我想在我的Mac上回复邮件。
服务器上的PHP代码简单地解析GET并发回给我一个带有设备ID的邮件,这样我就可以在我的Mac上解析结果。
你可以发布你写的代码来实现吗? 在我的测试中,在第一个小时内,我的频率与您的频率类似,但在过去一小时之前,我的电话直到三四个小时才收到。 – OverMind 2016-09-28 13:19:24
您是否尝试过调用没有网络相关的东西? – hyouuu 2017-04-29 01:08:09