Skip to content

Commit f682dbd

Browse files
committed
Merge branch 'expo-plugin-swift'
2 parents 06b7b30 + 4c82b1e commit f682dbd

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## 4.2.8 — 2025-05-02
4+
* [Expo][iOS] Add support for swift (`AppDelegate.swift`).
5+
36
## 4.2.7 — 2024-12-07
47
* [Android] Check if RN headless-task is running before calling `headlessJsTaskContext.finshTask(taskId)`
58
.

expo/plugin/src/iOSPlugin.ts

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,87 @@
11
import {
22
ConfigPlugin,
3-
withAppDelegate
3+
IOSConfig,
4+
WarningAggregator,
5+
withAppDelegate,
46
} from "@expo/config-plugins";
5-
import { mergeContents } from "@expo/config-plugins/build/utils/generateCode";
67

8+
import { AppDelegateProjectFile } from '@expo/config-plugins/build/ios/Paths';
9+
10+
import { mergeContents } from "@expo/config-plugins/build/utils/generateCode";
711

812
type Props = {}
913

1014
export const iOSPlugin: ConfigPlugin<Props> = (config, props={}) => {
11-
config = applyAppDelegate(config, props);
12-
return config;
13-
};
1415

15-
const applyAppDelegate:ConfigPlugin<Props> = (config, props) => {
16-
return withAppDelegate(config, (config) => {
17-
config.modResults.contents = applyDidFinishLaunchingWithOptions(config.modResults.contents);
18-
config.modResults.contents = applyAppDelegateImport(config.modResults.contents);
19-
return config;
20-
});
21-
}
16+
return withAppDelegate(config, (config) => {
17+
18+
const fileInfo = IOSConfig.Paths.getAppDelegate(config.modRequest.projectRoot);
2219

23-
const applyDidFinishLaunchingWithOptions = (src: string) => {
24-
let newSrc = [];
25-
newSrc.push(
26-
" [[TSBackgroundFetch sharedInstance] didFinishLaunching];"
27-
);
20+
console.log(`[react-native-background-fetch] configuring AppDelegate (${fileInfo.language})`);
2821

29-
newSrc = newSrc.filter(Boolean);
22+
switch (fileInfo.language) {
23+
case 'objc':
24+
case 'objcpp':
25+
config.modResults.contents = modifyObjcAppDelegate(config.modResults.contents);
26+
break;
27+
case 'swift':
28+
config.modResults.contents = modifySwiftAppDelegate(config.modResults.contents);
29+
break;
30+
default:
31+
throw new Error(`[react-native-background-fetch] Cannot configure AppDelegate for language "${fileInfo.language}"`);
32+
}
33+
34+
return config;
35+
});
36+
37+
};
38+
39+
const modifyObjcAppDelegate = (src:string) => {
40+
// 1. import TSBackgroundFetch
41+
src = mergeContents({
42+
tag: "react-native-background-fetch-import",
43+
src,
44+
newSrc: "#import <TSBackgroundFetch/TSBackgroundFetch.h>",
45+
anchor: "@implementation AppDelegate",
46+
offset: -1,
47+
comment: "//",
48+
}).contents;
3049

31-
return mergeContents({
32-
tag: "react-native-background-fetch-didFinishLaunchingWithOptions",
50+
// 2. TSBackgroundFetch.sharedInstance().didFinishLaunching();
51+
src = mergeContents({
52+
tag: "react-native-background-fetch-didFinishLaunching",
3353
src,
34-
newSrc: newSrc.join("\n"),
54+
newSrc: " [[TSBackgroundFetch sharedInstance] didFinishLaunching];",
3555
anchor: "didFinishLaunchingWithOptions:launchOptions];",
3656
offset: -1,
3757
comment: "//",
3858
}).contents;
39-
}
4059

41-
const applyAppDelegateImport = (src: string) => {
42-
const newSrc = [];
43-
newSrc.push(
44-
"#import <TSBackgroundFetch/TSBackgroundFetch.h>",
45-
);
60+
return src;
61+
}
4662

47-
return mergeContents({
63+
const modifySwiftAppDelegate = (src:string) => {
64+
// 1. import TSBackgroundFetch
65+
src = mergeContents({
4866
tag: "react-native-background-fetch-import",
4967
src,
50-
newSrc: newSrc.join("\n"),
51-
anchor: /#import "AppDelegate\.h"/,
52-
offset: 1,
68+
newSrc: "import TSBackgroundFetch",
69+
anchor: /@UIApplicationMain/,
70+
offset: -1,
71+
comment: "//",
72+
}).contents;
73+
74+
// 2. TSBackgroundFetch.sharedInstance().didFinishLaunching();
75+
src = mergeContents({
76+
tag: "react-native-background-fetch-didFinishLaunching",
77+
src,
78+
newSrc: " TSBackgroundFetch.sharedInstance().didFinishLaunching();",
79+
anchor: /return super.*didFinishLaunchingWithOptions.*/,
80+
offset: -1,
5381
comment: "//",
5482
}).contents;
83+
84+
return src;
5585
}
5686

5787
export default iOSPlugin;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-background-fetch",
3-
"version": "4.2.7",
3+
"version": "4.2.8",
44
"description": "iOS & Android BackgroundFetch API implementation for React Native",
55
"scripts": {
66
"build": "yarn run build:expo",
@@ -25,8 +25,8 @@
2525
},
2626
"homepage": "https://transistorsoft.github.io/react-native-background-fetch",
2727
"devDependencies": {
28+
"@expo/config-plugins": "^10.0.2",
2829
"rimraf": "^3.0.2",
29-
"@expo/config-plugins": "^4.1.0",
3030
"typescript": "^4.6.2"
3131
}
3232
}

0 commit comments

Comments
 (0)