Keep the extension alive for awhile?

We would need to keep a Safari App Extension alive for some time. Is there some API for that, or does anyone have experience with it?


The rationale is that when the user is working with our container application, we need the extension to reflect some changes and do appropriate processing at its side (like, change the toolbar icon, change some icons injected to the HTML page, etc). We have a communication channel through which the container application could let the extension know what to do, but of course, so that this works, the extension must be running.


Alas we can't simply use +[SFSafariApplication dispatchMessageWithName:toExtensionWithIdentifier:userInfo:completionHandler:], for it would activate Safari; but we need our application to stay active whilst this happens.


For the moment the only work-around we found is to send a message from an injected script to the extension code often enough, so that Safari (or launchd or whomever controls this) would not kill the extension. That would probably work (would need thorough testing), but is pretty darn ugly, not speaking of the question which frequency is “often enough”.


Can anyone see a better solution? Thanks!

Replies

How long are you looking to keep your extension alive while the containing app is open?


Any operations keep the extension alive for ~15 seconds, and having a popover will keep your extension alive for longer than that (but that behavior may change in the future).


Hope this helps!

It actually would depend on the user: as long as he is working in our container application. It might be a minute; it might be a couple of hours.


Actually... hm, thinking of this, it might be even better if the container application could somehow make sure the extension runs without activating Safari. I.e., we would need an API which


(a) would return a FAIL if the Safari is currently not running (it would not try to launch it);

(b) would return a FAIL if there is no extension with a given ID in Safari;

(c) would return a FAIL if there is such an extension, but it is disabled;

(d) if there's a given extension and Safari runs, it would first make sure the extension process runs too, and then it would send a given message to it, again, without activating Safari.


Can we achieve this goal anyhow with current APIs? Self-evidently we can do (a) using the NSRunningApplication API and (b)/(c) through SFSafariExtensionManager; but so far I found no solution for (d). Is there any?


In future, it would be nice if you could add some options to achieve (a)-(d) directly through the +[SFSafariApplication dispatchMessageWithName:...] API.


Thanks a lot,

OC

>In future, it would be nice if


As always, feel free to make such requests via a bug report, link below, adding your report # to your thread for reference, thanks & good luck.

As you discovered, B and C are already covered.


A radar about A & D would be very helpful, especially with some use cases that you are trying to achieve from your extension.


Thanks!

I believe I have explained it above (see “The rationale...”), or is there a problem with that?