Skip to content

Expo: iOS background delivery is implemented in a brittle way #52

Description

@jhogervorst

I see that the setup of background delivery for iOS in your Expo plugin was done in a brittle way (by manually patching the app delegate file):

const withTerraBackgroundDelivery: ConfigPlugin = (config) => {
config = withAppDelegate(config, (delegateConfig) => {
const { contents } = delegateConfig.modResults;
if (delegateConfig.modResults.language === 'swift') {
if (!delegateConfig.modResults.contents.includes('import TerraiOS')) {
delegateConfig.modResults.contents =
`import TerraiOS\n` + delegateConfig.modResults.contents;
}
if (
!delegateConfig.modResults.contents.includes(
'Terra.setUpBackgroundDelivery()'
)
) {
const regex =
/return super.application\(application, didFinishLaunchingWithOptions: launchOptions\)/;
delegateConfig.modResults.contents =
delegateConfig.modResults.contents.replace(
regex,
(match) => `Terra.setUpBackgroundDelivery()\n ${match}`
);
}
} else {
if (!contents.includes('#import <TerraiOS/TerraiOS-Swift.h>')) {
delegateConfig.modResults.contents = contents.replace(
'#import "AppDelegate.h"',
'#import "AppDelegate.h"\n#import <TerraiOS/TerraiOS-Swift.h>'
);
}
if (!contents.includes('[Terra setUpBackgroundDelivery];')) {
const regex =
/- \(BOOL\)application:\(UIApplication \*\)application didFinishLaunchingWithOptions:\(NSDictionary \*\)launchOptions\s*\{\n/;
delegateConfig.modResults.contents =
delegateConfig.modResults.contents.replace(
regex,
(match) => `${match} [Terra setUpBackgroundDelivery];\n`
);
}
}
return delegateConfig;
});
return config;
};

I believe that Expo has a better way (docs) for this.

I actually implemented that in my custom Terra Expo plugin, which I created before you guys offered one: app delegate, config.

Maybe this could be improved in your Expo plugin. (Feel free to reuse my code!)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions