How to register for an event that reminds app on specified time in iOS

前端 未结 4 1486
-上瘾入骨i
-上瘾入骨i 2021-01-19 05:03

I have requirement to initiate sync on every night at 1 AM or every 2 weeks. How will I achieve this in iOS? Is there a way in iOS that my application can say remind me at t

4条回答
  •  自闭症患者
    2021-01-19 05:28

    Background Fetch is an opportunity provided by OS (iOS 7 onwards) to the apps who requested to perform an operation when in background, however leaving on OS to decide the timing. The OS will silently awake your app (actually a handler method) in background after learning about the user's usage of an app.

    Feature usage: As of now, this feature has been introduced in iOS7 to enhance usability of social media, newspaper etc daily/frequent/heavy content refreshing apps.

    Note- its just a request that is not guaranteed to be fulfilled but an attempt of underlying OS to better user experience)

    From coding/implementation perspective, there are primarily 2-3 key steps -

    A) Turn ON Background Modes for Background Fetch attribute (Select App Target > Capabilities > Background Modes > Background Fetch) enter image description here

    B) Implement this app delegate method -

    - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);
    

    C) Define your fetch interval, typically when app launches - i.e appDidFinishLaunchingWithOptions

    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    

    Since it's an event fired by OS automatically, how to test this behavior during development -

    Well, you can simulate/test the background fetch behavior in your app through XCode 5+ using menu Debug > Simulate Background Fetch

    enter image description here

    Here is the link to the tutorial with sample project.

    Also, you can checkout session 204 from Apple's WWDC 2013 videos - What's New with Multitasking

    EDIT -

    As of today (iOS 7.0.3), in lack of proper official documentation, the practical usage tells that -

    1. The enforced minimumBackgroundFetchInterval is between than 5 to 15 minutes (anything less than it is not honored).
    2. The background fetch doesn't happen when device is locked.
    3. The background fetch is triggered immediately after the device is unlocked by user.

    However, in upcoming versions, I feel this feature would be enhanced and giving more power to developers/users.

提交回复
热议问题