Samarth Kadam
Samarth Kadam

Reputation: 11

Expo Prebuild for React Native ios

I've noticed a behavior when running expo prebuild in my React Native project using Expo. For the iOS platform, it completely replaces the AppDelegate.mm file and regenerates new icons. However, on Android, I don’t see as many changes.

The issue I'm facing is that whenever I modify AppDelegate.mm (e.g., for setting up a native module) and then run expo prebuild again, all my changes are lost.

Is this the expected behavior? If so, what’s the recommended approach to persist changes in AppDelegate.mm while using Expo prebuild? Also, why doesn’t Android seem to have as many changes compared to iOS?

Would appreciate any insights on this. Thanks!

Upvotes: 1

Views: 77

Answers (1)

K.pen
K.pen

Reputation: 343

Create a custom Expo config plugin (plugin.js) This ensures that your modifications are applied every time expo prebuild runs.

const { withDangerousMod } = require('@expo/config-plugins');
const fs = require('fs');
const path = require('path');
        
module.exports = function withCustomAppDelegate(config) {
          return withDangerousMod(config, ['ios', (config) => {
            const appDelegatePath = path.join(config.modRequest.platformProjectRoot, 'MyApp', 'AppDelegate.mm');
        
        let content = fs.readFileSync(appDelegatePath, 'utf-8');
        
        // Example: Add a custom import or native module setup
        if (!content.includes('#import "MyNativeModule.h"')) {
          content = content.replace(
            '#import <React/RCTBridgeModule.h>',
            `#import <React/RCTBridgeModule.h>\n#import "MyNativeModule.h"`
          );
        }
    
        fs.writeFileSync(appDelegatePath, content);
        return config;
      }]);
    };

Upvotes: 0

Related Questions