Skip to content

Commit 0d11945

Browse files
authored
fix(config-plugin): Improve plugin to support swift (#277)
1 parent 095b38f commit 0d11945

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

intercom-react-native.podspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Pod::Spec.new do |s|
1717
s.source_files = "ios/**/*.{h,m,mm}"
1818
s.resource_bundles = { 'IntercomFramework' => ['ios/assets/*'] }
1919

20+
s.pod_target_xcconfig = { "DEFINES_MODULE" => "YES" }
21+
2022
s.dependency "React-Core"
2123
s.dependency "Intercom", '~> 18.6.1'
2224
end

src/expo-plugins/index.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import {
88
withMainApplication,
99
} from '@expo/config-plugins';
1010

11+
import { mergeContents } from '@expo/config-plugins/build/utils/generateCode';
12+
1113
import {
1214
addImports,
1315
appendContentsInsideDeclarationBlock,
1416
} from '@expo/config-plugins/build/android/codeMod';
1517
import {
1618
addObjcImports,
1719
insertContentsInsideObjcFunctionBlock,
20+
insertContentsInsideSwiftFunctionBlock,
1821
} from '@expo/config-plugins/build/ios/codeMod';
1922
import type { IntercomPluginProps, IntercomRegion } from './@types';
2023
import { withIntercomPushNotification } from './withPushNotifications';
@@ -92,20 +95,40 @@ const withIntercomAndroid: ConfigPlugin<IntercomPluginProps> = (
9295
const appDelegate: ConfigPlugin<IntercomPluginProps> = (_config, props) =>
9396
withAppDelegate(_config, (config) => {
9497
let stringContents = config.modResults.contents;
95-
stringContents = addObjcImports(stringContents, ['<IntercomModule.h>']);
98+
const isSwift = config.modResults.language === 'swift';
99+
100+
stringContents = isSwift
101+
? mergeContents({
102+
src: stringContents,
103+
newSrc: 'import intercom_react_native.IntercomModule',
104+
comment: '//',
105+
tag: 'Intercom header',
106+
anchor: /import Expo/,
107+
offset: 1,
108+
}).contents
109+
: addObjcImports(stringContents, ['<IntercomModule.h>']);
96110

97111
// Remove previous code
98-
stringContents = stringContents.replace(
99-
/\s*\[IntercomModule initialize:@"(.*)" withAppId:@"(.*)"];/g,
100-
''
101-
);
102-
103-
stringContents = insertContentsInsideObjcFunctionBlock(
104-
stringContents,
105-
'application didFinishLaunchingWithOptions:',
106-
`[IntercomModule initialize:@"${props.iosApiKey}" withAppId:@"${props.appId}"];`,
107-
{ position: 'tailBeforeLastReturn' }
108-
);
112+
stringContents = stringContents
113+
.replace(
114+
/\s*\[IntercomModule initialize:@"(.*)" withAppId:@"(.*)"];/g,
115+
''
116+
)
117+
.replace(/\s*IntercomModule\.initialize\((.*), withAppId: (.*)\)/g, '');
118+
119+
stringContents = isSwift
120+
? insertContentsInsideSwiftFunctionBlock(
121+
stringContents,
122+
'application(_:didFinishLaunchingWithOptions:)',
123+
`IntercomModule.initialize("${props.iosApiKey}", withAppId: "${props.appId}")`,
124+
{ position: 'tailBeforeLastReturn' }
125+
)
126+
: insertContentsInsideObjcFunctionBlock(
127+
stringContents,
128+
'application didFinishLaunchingWithOptions:',
129+
`[IntercomModule initialize:@"${props.iosApiKey}" withAppId:@"${props.appId}"];`,
130+
{ position: 'tailBeforeLastReturn' }
131+
);
109132

110133
config.modResults.contents = stringContents;
111134
return config;

0 commit comments

Comments
 (0)