|
1 | 1 | import { |
2 | 2 | ConfigPlugin, |
3 | | - withAppDelegate |
| 3 | + IOSConfig, |
| 4 | + WarningAggregator, |
| 5 | + withAppDelegate, |
4 | 6 | } from "@expo/config-plugins"; |
5 | | -import { mergeContents } from "@expo/config-plugins/build/utils/generateCode"; |
6 | 7 |
|
| 8 | +import { AppDelegateProjectFile } from '@expo/config-plugins/build/ios/Paths'; |
| 9 | + |
| 10 | +import { mergeContents } from "@expo/config-plugins/build/utils/generateCode"; |
7 | 11 |
|
8 | 12 | type Props = {} |
9 | 13 |
|
10 | 14 | export const iOSPlugin: ConfigPlugin<Props> = (config, props={}) => { |
11 | | - config = applyAppDelegate(config, props); |
12 | | - return config; |
13 | | -}; |
14 | 15 |
|
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); |
22 | 19 |
|
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})`); |
28 | 21 |
|
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; |
30 | 49 |
|
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", |
33 | 53 | src, |
34 | | - newSrc: newSrc.join("\n"), |
| 54 | + newSrc: " [[TSBackgroundFetch sharedInstance] didFinishLaunching];", |
35 | 55 | anchor: "didFinishLaunchingWithOptions:launchOptions];", |
36 | 56 | offset: -1, |
37 | 57 | comment: "//", |
38 | 58 | }).contents; |
39 | | -} |
40 | 59 |
|
41 | | -const applyAppDelegateImport = (src: string) => { |
42 | | - const newSrc = []; |
43 | | - newSrc.push( |
44 | | - "#import <TSBackgroundFetch/TSBackgroundFetch.h>", |
45 | | - ); |
| 60 | + return src; |
| 61 | +} |
46 | 62 |
|
47 | | - return mergeContents({ |
| 63 | +const modifySwiftAppDelegate = (src:string) => { |
| 64 | + // 1. import TSBackgroundFetch |
| 65 | + src = mergeContents({ |
48 | 66 | tag: "react-native-background-fetch-import", |
49 | 67 | 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, |
53 | 81 | comment: "//", |
54 | 82 | }).contents; |
| 83 | + |
| 84 | + return src; |
55 | 85 | } |
56 | 86 |
|
57 | 87 | export default iOSPlugin; |
0 commit comments