gpt4 book ai didi

ios - 如果应用程序已终止,将调用 performFetchWithCompletionHandler

转载 作者:可可西里 更新时间:2023-11-01 03:29:51 25 4
gpt4 key购买 nike

要找到一个明确的答案是非常困难的;在 Apple 文档中找不到它,并且在搜索过去的问题后找不到明确的是/否。

问题很简单 - 如果应用程序请求在 N 次后执行后台提取,则用户终止该应用程序。操作系统是否仍会在后台启动应用程序以执行后台获取?

最佳答案

好吧,后台模式又一次引起了困惑。没有冒犯其他试图提供帮助的人,但是这比看起来要复杂

首先: This 已经过时了,正如香肠在评论中猜测的那样。我确实知道这一点,因为有关 VoIP 应用程序的部分仍在解释执行此操作的“旧方法”,即使用定期调用的处理程序。我针对 this answer 对此进行了一些调查,所以我建议你去读一下。这个案例的重要教训是,iOS 区分应用程序是由用户终止还是由系统终止,此外它还在手机是否重启方面发挥作用。

因此,总结一下(和您的问题),您基本上想知道上述过时文档的这一部分是否仍然逐字正确:

In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system. When password protection is enabled on the device, the system does not launch an app in the background before the user first unlocks the device.

Apple: Understanding When Your App Gets Launched into the Background

我彻底调查了其余的文档,但没有找到任何明确的答案,所以不幸的是归结为 dan 已经建议的内容:测试它。不过,我的直觉是文档在这方面仍然是正确的(正如所说,VoIP 的东西不是)。我这么说是因为设置应用程序中的用户界面调用了“后台应用程序刷新”功能,所以用户可能应该明白,当他们将具有此权限的应用程序“推出”后台时(即主页按钮 -> 将其滑出)。对于普通用户,应用程序要么退出(根本不在任务管理器中),要么在前台(使用它们),要么在后台(它们在任务管理器中,另一个应用程序在前台和/或手机被锁定) .


要真正测试这一点,您必须编写一个应用程序并在每种情况下实际携带它(我假设至少两天)。首先当它在后台时(操作系统应该定期让它获取,你可能知道这也可以在 Xcode 中触发)然后当它强制退出时。问题是验证它是否获取了东西。我会选择一个可以通过 iTunes 共享的日志文件。我为此输入了一些代码:

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"We're awake! Booyah!");

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request = [NSMutableURLRequest new];
request.HTTPMethod = @"GET";
request.URL = [NSURL URLWithString:@"https://www.google.com"];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
NSDate *now = [NSDate date];
NSString *toLog = [NSString stringWithFormat:@"%@ - fetched\n",
[now description]];
[self updateTestDocumentWithString:toLog];
NSLog(@"Yay, done!");
completionHandler(UIBackgroundFetchResultNewData);
}];
[task resume];
}

- (void)updateTestDocumentWithString:(NSString *)toAppend {
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [[docDir stringByAppendingPathComponent:@"logfile.txt"] copy];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
if (![[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]) {
NSLog(@"We're effed...");
return;
}
}
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
if (!file) {
NSLog(@"We're effed again...");
return;
}
[file seekToEndOfFile];
// ensure this is never nil
[file writeData:[toAppend dataUsingEncoding:NSUTF8StringEncoding]];
[file closeFile];
}

这将进入应用委托(delegate),并且不要忘记在应用的 plist 中添加 Application supports iTunes file sharing bool 值设置。我会让它在我的开发设备上运行一段时间并检查日志文件,最终在这里报告。您也可以自己测试一下。

关于ios - 如果应用程序已终止,将调用 performFetchWithCompletionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44828303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com