Is there something I need to take into account about the plugin instance lifetime, or can I assume that this object exists all the time my web view is visible?

It seems from looking at a few plugins, some people tend to use some kind of singleton manager to store data in their PhoneGap plugins. Why not just store it in the PhoneGap plugin itself?

The reason for asking is that it seems the only proper way to communicate with the web view is through the [self writeJavaScript:message] method. If I register myself to receive some notification, I want to be sure my plugin instance exist.

有帮助吗?

解决方案

The plugin lives as long as the web view is alive.

The proper way to initialize variables or register for callbacks is in the

- (CDVPlugin*) initWithWebView:(UIWebView*)theWebView {
   self = [super initWithWebView:theWebView];
   if (self) {
       _variable = [[Variable alloc] init]; 
       [Classname addListener:self];
   }
   return self;
}

method. That method is run for all plugins.

Don't forget to clean up after yourself in the dealloc method.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top