react-native-background-fetch
Version:
iOS & Android BackgroundFetch API implementation for React Native
68 lines • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.iOSPlugin = void 0;
const config_plugins_1 = require("@expo/config-plugins");
const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode");
const iOSPlugin = (config, props = {}) => {
return (0, config_plugins_1.withAppDelegate)(config, (config) => {
const fileInfo = config_plugins_1.IOSConfig.Paths.getAppDelegate(config.modRequest.projectRoot);
console.log(`[react-native-background-fetch] configuring AppDelegate (${fileInfo.language})`);
switch (fileInfo.language) {
case 'objc':
case 'objcpp':
config.modResults.contents = modifyObjcAppDelegate(config.modResults.contents);
break;
case 'swift':
config.modResults.contents = modifySwiftAppDelegate(config.modResults.contents);
break;
default:
throw new Error(`[react-native-background-fetch] Cannot configure AppDelegate for language "${fileInfo.language}"`);
}
return config;
});
};
exports.iOSPlugin = iOSPlugin;
const modifyObjcAppDelegate = (src) => {
// 1. import TSBackgroundFetch
src = (0, generateCode_1.mergeContents)({
tag: "react-native-background-fetch-import",
src,
newSrc: "#import <TSBackgroundFetch/TSBackgroundFetch.h>",
anchor: "@implementation AppDelegate",
offset: -1,
comment: "//",
}).contents;
// 2. TSBackgroundFetch.sharedInstance().didFinishLaunching();
src = (0, generateCode_1.mergeContents)({
tag: "react-native-background-fetch-didFinishLaunching",
src,
newSrc: " [[TSBackgroundFetch sharedInstance] didFinishLaunching];",
anchor: "didFinishLaunchingWithOptions:launchOptions];",
offset: -1,
comment: "//",
}).contents;
return src;
};
const modifySwiftAppDelegate = (src) => {
// 1. import TSBackgroundFetch
src = (0, generateCode_1.mergeContents)({
tag: "react-native-background-fetch-import",
src,
newSrc: "import TSBackgroundFetch",
anchor: /@UIApplicationMain/,
offset: -1,
comment: "//",
}).contents;
// 2. TSBackgroundFetch.sharedInstance().didFinishLaunching();
src = (0, generateCode_1.mergeContents)({
tag: "react-native-background-fetch-didFinishLaunching",
src,
newSrc: " TSBackgroundFetch.sharedInstance().didFinishLaunching();",
anchor: /return super.*didFinishLaunchingWithOptions.*/,
offset: -1,
comment: "//",
}).contents;
return src;
};
exports.default = exports.iOSPlugin;
//# sourceMappingURL=iOSPlugin.js.map