8

我阅读了有关后台获取的所有 Apple 文档,目前我正在使用它:

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:minimumBackgroundFetchInterval];

我让操作系统决定何时执行后台获取,但如果我这样设置:

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:21600];

这是否意味着 fetch 每 6 小时发生一次?

4

3 回答 3

18

我在 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 分钟不等。

于 2016-07-18T05:25:21.467 回答
6

要求的代码广告:

- (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 上解析结果。

于 2017-09-30T06:18:20.940 回答
5

不,这意味着您建议 iOS 在执行后台提取之前至少要经过六个小时,但是此属性的文档指出 -

在可以启动另一个后台提取之前必须经过的最小秒数。此值仅供参考,并不表示提取操作之间预期的确切时间量。

因此,执行后台提取可能需要六个多小时,但可能不会更少。iOS 还会记录您通过完成处理程序返回的值,该值指示是否有新数据,以尝试确定您的应用程序可能有新数据的一天中的时间。

于 2015-02-07T10:40:11.210 回答