Skip to content

Improvement to make code self-contained in Plugin #1

Description

@shazron

Instead of adding the code in didFinishLaunchingWithOptions(), consider adding it in your plugin's method (add the method if its missing):

- (void) pluginInitialize();

This code:

NSLog(@"initializing DBAccountManager");
DBAccountManager *accountManager =
[[DBAccountManager alloc] initWithAppKey:@"irylk73l2cucd1v" secret:@"vw8ld6rdebdqayi"];
[DBAccountManager setSharedManager:accountManager];
DBAccount *account = [accountManager.linkedAccounts objectAtIndex:0];
if (account) {
DBFilesystem *filesystem = [[DBFilesystem alloc] initWithAccount:account];
NSLog(@"App linked successfully from didFinishLaunchingWithOptions!");
[DBFilesystem setSharedFilesystem:filesystem];
}

Can be in your plugin as:

- (void) pluginInitialize {
    NSLog(@"initializing DBAccountManager");
    DBAccountManager *accountManager =
    [[DBAccountManager alloc] initWithAppKey:@"irylk73l2cucd1v" secret:@"vw8ld6rdebdqayi"];
    [DBAccountManager setSharedManager:accountManager];

    DBAccount *account = [accountManager.linkedAccounts objectAtIndex:0];
    if (account) {
        DBFilesystem *filesystem = [[DBFilesystem alloc] initWithAccount:account];
        NSLog(@"App linked successfully from didFinishLaunchingWithOptions!");
        [DBFilesystem setSharedFilesystem:filesystem];
    }
}

Don't forget to add the required import headers as well at the top of your plugin.

#import <Dropbox/Dropbox.h>

From the Plugin Dev Guide: "There is no designated initializer for plugins. Instead, plugins should use the pluginInitialize method for their start-up logic."

Example use: https://github.com/apache/cordova-ios/blob/9e47ed817fdad4669a8ce92908eb63320dda593a/CordovaLib/Classes/CDVLocalStorage.m#L34-L41

To load the plugin at startup, you can add an:

onload="true"

... attribute to the feature's param tag: https://github.com/apache/cordova-ios/blob/951b6cb2f6d920b2cd28dd9598274331ad5a0d47/CordovaLibTests/CordovaLibApp/config.xml#L57

If you don't add that, the plugin is lazily loaded (load on first use).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions