Skip to content

fix(config-plugin): Improve plugin to support swift #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions intercom-react-native.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 35 additions & 12 deletions src/expo-plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import {
withMainApplication,
} from '@expo/config-plugins';

import { mergeContents } from '@expo/config-plugins/build/utils/generateCode';

import {
addImports,
appendContentsInsideDeclarationBlock,
} from '@expo/config-plugins/build/android/codeMod';
import {
addObjcImports,
insertContentsInsideObjcFunctionBlock,
insertContentsInsideSwiftFunctionBlock,
} from '@expo/config-plugins/build/ios/codeMod';
import type { IntercomPluginProps, IntercomRegion } from './@types';
import { withIntercomPushNotification } from './withPushNotifications';
Expand Down Expand Up @@ -92,20 +95,40 @@ const withIntercomAndroid: ConfigPlugin<IntercomPluginProps> = (
const appDelegate: ConfigPlugin<IntercomPluginProps> = (_config, props) =>
withAppDelegate(_config, (config) => {
let stringContents = config.modResults.contents;
stringContents = addObjcImports(stringContents, ['<IntercomModule.h>']);
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, ['<IntercomModule.h>']);

// 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;
Expand Down