Customizing iOS's application:didFinishLaunchi

2019-03-27 23:11发布

问题:

I'm newish to Cordova and am wondering if there is a way to tweak the platform code generated by Cordova/Ionic without hindering the development process.

The specific requirement is to integrate the Facebook SDK to the iOS app in order to support Facebook Mobile App Install ads. Integration is straightforward: it only requires adding a line of code to application:didFinishLaunchingWithOptions: in AppDelegate.m and adding the Facebook iOS framework to the Xcode project.

Currently the entire plaforms directory is excluded from source control, as it is generated by Cordova during build. If I am to tweak AppDelegate.m, I will have to add it to source control. Then won't subsequent changes to the Ionic app result in merge conflicts with the Xcode project? How can I integrate my small changes to the Xcode project without breaking the process?

NOTE: I did look for a plugin as a solution, but the plugin I found comes with complications of its own. And it appears that Cordova does not provide hooks in application:didFinishLaunchingWithOptions: anyway.

回答1:

You should create your own plugin instead of making changes into the AppDelegate.m

You can listen the UIApplicationDidFinishLaunchingNotification on the pluginInitialize an put the code there

- (void)pluginInitialize
{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishLaunching:) name:UIApplicationDidFinishLaunchingNotification object:nil];

}

- (void)finishLaunching:(NSNotification *)notification
{
    // Put here the code that should be on the AppDelegate.m
}

The plugin.xml need the onload true to call the pluginInitialize

<feature name="yourPluginName">
    <param name="ios-package" value="yourPluginClass" />
    <param name="onload" value="true" />
</feature>


回答2:

You could use this plugin for that: https://github.com/katzer/cordova-plugin-app-event



回答3:

You can make additions to the app delegate file, that change will not go away unlike files inside www folder. You can make changes inside the Xcode project folder.