Skip to main content

How to Add codes in IOS native files using expo in AppDelegate.m

Adding import statement in AppDelegate.m using expo

Inside your app.plugin.js file, add the following code.

const { withAppDelegate } = require('expo/config-plugins');


module.exports = function myCustomExpoFunctionName(config) {
config = withAppDelegate(config, (config) => {
if (config.modResults.contents.includes('MyCustom/Package.h')) {
return config;
}
const importLine = '#import <React/RCTLinkingManager.h>';

config.modResults.contents = config.modResults.contents.replace(
importLine,
`${importLine}
// Add your import statement here Ex: #import <MyCustom/Package.h>
`,
);

return config;
});
return config;
};

Calling a method in AppDelegate.m using expo inside didFinishLaunchingWithOptions method

Inside your app.plugin.js file, add the following code.

const { withAppDelegate } = require('expo/config-plugins');

module.exports = function myCustomExpoFunctionName(config) {
config = withAppDelegate(config, (config) => {
const didFinishLaunchingWithOptionsLine = '[FIRApp configure];';

config.modResults.contents = config.modResults.contents.replace(
didFinishLaunchingWithOptionsLine,
`${didFinishLaunchingWithOptionsLine}
// Add your method call here Ex: [self logAppLaunch];
`,
);
return config;
});
return config;
};

Adding a new method in AppDelegate.m using expo

Inside your app.plugin.js file, add the following code.


const { withAppDelegate } = require('expo/config-plugins');

module.exports = function myCustomExpoFunctionName(config) {
config = withAppDelegate(config, (config) => {
const newMethod = `
// Add your new method here
- (void)logAppLaunch {
NSLog(@"The app has launched successfully.");
}
`;

config.modResults.contents = config.modResults.contents.replace(
'@end',
`${newMethod}
@end`,
);

return config;
});
return config;
};