iOS后台获取自定义间隔

时间:2015-02-07 10:34:03

标签: ios objective-c background-fetch

我阅读了有关背景提取的所有Apple文档,目前我正在使用它:

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:minimumBackgroundFetchInterval];

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

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:21600];

这是否意味着每6小时会进行一次获取?

3 个答案:

答案 0 :(得分:14)

我在iOS10和iPhone6Plus上做了一些实验,给出了“UIApplicationBackgroundFetchIntervalMinimum”间隔。 (当然我调用了一些与网络相关的方法来给iOS一个提示,即应用程序真正起作用......并调用completionHandler(UIBackgroundFetchResultNewData);)

我得到了(跑了一整夜)

0时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 六点56

所以Delta从10到34分钟不等。

答案 1 :(得分:4)

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

  

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

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

答案 2 :(得分:2)

要求代码广告:

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