@@ -8,13 +8,16 @@ import {
88 withMainApplication ,
99} from '@expo/config-plugins' ;
1010
11+ import { mergeContents } from '@expo/config-plugins/build/utils/generateCode' ;
12+
1113import {
1214 addImports ,
1315 appendContentsInsideDeclarationBlock ,
1416} from '@expo/config-plugins/build/android/codeMod' ;
1517import {
1618 addObjcImports ,
1719 insertContentsInsideObjcFunctionBlock ,
20+ insertContentsInsideSwiftFunctionBlock ,
1821} from '@expo/config-plugins/build/ios/codeMod' ;
1922import type { IntercomPluginProps , IntercomRegion } from './@types' ;
2023import { withIntercomPushNotification } from './withPushNotifications' ;
@@ -92,20 +95,40 @@ const withIntercomAndroid: ConfigPlugin<IntercomPluginProps> = (
9295const 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 : / i m p o r t E x p o / ,
107+ offset : 1 ,
108+ } ) . contents
109+ : addObjcImports ( stringContents , [ '<IntercomModule.h>' ] ) ;
96110
97111 // Remove previous code
98- stringContents = stringContents . replace (
99- / \s * \[ I n t e r c o m M o d u l e i n i t i a l i z e : @ " ( .* ) " w i t h A p p I d : @ " ( .* ) " ] ; / 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 * \[ I n t e r c o m M o d u l e i n i t i a l i z e : @ " ( .* ) " w i t h A p p I d : @ " ( .* ) " ] ; / g,
115+ ''
116+ )
117+ . replace ( / \s * I n t e r c o m M o d u l e \. i n i t i a l i z e \( ( .* ) , w i t h A p p I d : ( .* ) \) / 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