From 49f6daee8652542115e296b8789b5a893be06e10 Mon Sep 17 00:00:00 2001 From: Thibault Malbranche Date: Mon, 14 Apr 2025 17:23:09 +0200 Subject: [PATCH] fix(config-plugin): Improve plugin to support swift --- intercom-react-native.podspec | 2 ++ src/expo-plugins/index.ts | 47 ++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/intercom-react-native.podspec b/intercom-react-native.podspec index 2195b08e..962eab99 100644 --- a/intercom-react-native.podspec +++ b/intercom-react-native.podspec @@ -17,6 +17,8 @@ Pod::Spec.new do |s| s.source_files = "ios/**/*.{h,m,mm}" s.resource_bundles = { 'IntercomFramework' => ['ios/assets/*'] } + s.pod_target_xcconfig = { "DEFINES_MODULE" => "YES" } + s.dependency "React-Core" s.dependency "Intercom", '~> 18.6.1' end diff --git a/src/expo-plugins/index.ts b/src/expo-plugins/index.ts index 979293df..b9725cf1 100644 --- a/src/expo-plugins/index.ts +++ b/src/expo-plugins/index.ts @@ -8,6 +8,8 @@ import { withMainApplication, } from '@expo/config-plugins'; +import { mergeContents } from '@expo/config-plugins/build/utils/generateCode'; + import { addImports, appendContentsInsideDeclarationBlock, @@ -15,6 +17,7 @@ import { import { addObjcImports, insertContentsInsideObjcFunctionBlock, + insertContentsInsideSwiftFunctionBlock, } from '@expo/config-plugins/build/ios/codeMod'; import type { IntercomPluginProps, IntercomRegion } from './@types'; import { withIntercomPushNotification } from './withPushNotifications'; @@ -92,20 +95,40 @@ const withIntercomAndroid: ConfigPlugin = ( const appDelegate: ConfigPlugin = (_config, props) => withAppDelegate(_config, (config) => { let stringContents = config.modResults.contents; - stringContents = addObjcImports(stringContents, ['']); + const isSwift = config.modResults.language === 'swift'; + + stringContents = isSwift + ? mergeContents({ + src: stringContents, + newSrc: 'import intercom_react_native.IntercomModule', + comment: '//', + tag: 'Intercom header', + anchor: /import Expo/, + offset: 1, + }).contents + : addObjcImports(stringContents, ['']); // Remove previous code - stringContents = stringContents.replace( - /\s*\[IntercomModule initialize:@"(.*)" withAppId:@"(.*)"];/g, - '' - ); - - stringContents = insertContentsInsideObjcFunctionBlock( - stringContents, - 'application didFinishLaunchingWithOptions:', - `[IntercomModule initialize:@"${props.iosApiKey}" withAppId:@"${props.appId}"];`, - { position: 'tailBeforeLastReturn' } - ); + stringContents = stringContents + .replace( + /\s*\[IntercomModule initialize:@"(.*)" withAppId:@"(.*)"];/g, + '' + ) + .replace(/\s*IntercomModule\.initialize\((.*), withAppId: (.*)\)/g, ''); + + stringContents = isSwift + ? insertContentsInsideSwiftFunctionBlock( + stringContents, + 'application(_:didFinishLaunchingWithOptions:)', + `IntercomModule.initialize("${props.iosApiKey}", withAppId: "${props.appId}")`, + { position: 'tailBeforeLastReturn' } + ) + : insertContentsInsideObjcFunctionBlock( + stringContents, + 'application didFinishLaunchingWithOptions:', + `[IntercomModule initialize:@"${props.iosApiKey}" withAppId:@"${props.appId}"];`, + { position: 'tailBeforeLastReturn' } + ); config.modResults.contents = stringContents; return config;