$ yarn add @transistorsoft/capacitor-background-fetch
$ npx cap sync
$ npm install --save @transistorsoft/capacitor-background-fetch
$ npx cap sync
$ pod --version
// if < 1.10.0
$ sudo gem install cocoapods
-
Open your XCode project with
npx cap open ios
. -
Select the root of your project. Select Capabilities tab. Enable Background Modes and enable the following mode:
-
Background fetch
-
Background processing (:new: iOS 13+; Only if you intend to use
BackgroundFetch.scheduleTask
)
- Open your
Info.plist
and add the key "Permitted background task scheduler identifiers"
- Add the required identifier
com.transistorsoft.fetch
.
- If you intend to execute your own custom tasks via
BackgroundFetch.scheduleTask
, you must add those custom identifiers as well. For example, if you intend to execute a customtaskId: 'com.transistorsoft.customtask'
, you must add the identifiercom.transistorsoft.customtask
to your "Permitted background task scheduler identifiers", as well.
com.transistorsoft.
.
BackgroundFetch.scheduleTask({
taskId: 'com.transistorsoft.customtask',
delay: 60 * 60 * 1000 // In one hour (milliseconds)
});
Apple now requires apps provide a Privacy Manifest for "sensitive" APIs which could be abused for "fingerprinting" a user for malicious marketing activity.
If your app does not yet have a Privacy Manifest (PrivacyInfo.xcprivacy
), create one now:
ℹ️ Click here for detailed instructions...
- In XCode,
File -> New -> File...
:
- Be sure to enable your
Targets: [x] YourApp
:
It's best to edit this file's XML manually.
- 📂
ios/App/PrivacyInfo.xcprivacy
- Add the following block within the
NSPrivacyAccessedAPITypes
<array>
container:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyAccessedAPITypes</key>
<array>
<!-- [1] background_fetch: UserDefaults -->
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryUserDefaults</string>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>CA92.1</string>
</array>
</dict>
</array>
</dict>
</plist>
- Edit your
AppDelegate.swift
and add the following code:
import UIKit
import Capacitor
+import TSBackgroundFetch
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
+ // [capacitor-background-fetch]
+ let fetchManager = TSBackgroundFetch.sharedInstance();
+ fetchManager?.didFinishLaunching();
return true
}
+ // [capacitor-background-fetch]
+ func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
+ print("BackgroundFetchPlugin AppDelegate received fetch event");
+ let fetchManager = TSBackgroundFetch.sharedInstance();
+ fetchManager?.perform(completionHandler: completionHandler, applicationState: application.applicationState);
+ }