Safari extension iOS - messaging from app to extension - ios

With iOS 15, Apple had made it possible to create Safari extension for iOS as well.
I'm currently in the process of bringing an existing macOS Safari extension to iOS.
My problem is that there doesn't seem to be the option to send a message from the host app to the extension. It is possible to answer a request from the extension through the
beginRequest(with:) function. However, I actively need to send a message to the extension after an user action. In macOS, this can be done through the dispatchMessage(withName:toExtensionWithIdentifier:userInfo:completionHandler:) function as described here: https://developer.apple.com/documentation/safariservices/safari_web_extensions/messaging_between_the_app_and_javascript_in_a_safari_web_extension. This function is unfortunately not available for iOS.
Would greatly appreciate any ideas for solving this problem.

You can enable permission ["nativeMessaging"] in manifest file of the safari extension
Use appGroup to store data between the extension and your application.
In SafariWebExtensionHandler, you can response appGroup Data to the message called from any extension javascript file.

Related

Can an ios host app implement more than one keyboard extension?

I have a request to implement a keyboard extension for an established ios app that already appears to have a keyboard extension. I've been reading docs, but I am having trouble discerning if a host app can have more than one implementation of a keyboard extension point.
After creating a demo app with two keyboard extensions I can confirm this works - at least in within a dev environment. I have no reason to believe this wouldn't pass app store submission.

How to open iOS app from within extension?

My app is registered for URL Schemes:
But I have no idea how to open that app, since I cannot use UIApplication.sharedApplication() in extension. Is it possible at all?
Apple's documentation says:
A Today widget (and no other app extension type) can ask the system to open its containing app by calling the openURL:completionHandler: method of the NSExtensionContext class.

Enable Application cache in WKWebView

I am aware that offline application cache is not supported in iOS WKWebView.
This is enabled in Safari, so I searched webkit project for the responsible code & found this
WKPreferences
- (void)_setOfflineApplicationCacheIsEnabled:(BOOL)offlineApplicationCacheIsEnabled;
Anyone familiar with this method? is it possible to enable app cache in iOS by accessing this private methods? (I am not going to ship the app to Appstore)
Update 2022
According to one of the comments below, this hack doesn't work anymore.
Yes, we can enable App cache by accessing private API
Create a category for WKPreferences and add to following method signature.
#interface WKPreferences (MyPreferences)
- (void)_setOfflineApplicationCacheIsEnabled:(BOOL)offlineApplicationCacheIsEnabled;
#end
(I tried performSelector:withObject: but it didn't work. No idea why)
After initializing the WKWebView, enable the appcache by calling the above method in the following object
[_wkWebView.configuration.preferences _setOfflineApplicationCacheIsEnabled:YES];
It will create the ApplicationCache.db file in the Cache directory and allow the web app to work offline.
Warning :
2.5. Apps that use non-public APIs will be rejected
According to this tweet from at Apple, as of iOS 10, App Cache is now supported in WKWebView:
https://twitter.com/andersca/status/743259582252879872
...as does this WebKit bug report:
https://bugs.webkit.org/show_bug.cgi?id=152490
I've tested this in WKWebView using this site:
http://webdbg.com/test/appcache/
and can confirm it works as expected both in the iOS Simulator and on devices running iOS 10.

Is some method called in the host app when today extension starts?

I'm wondering if some method called in the host app when user opens app's today extension?
I use Flurry analytics framework to get statistics about my app usage and it's very important for me. But since I've added an app extension in the last update, the most users use my app only through extension. So now my statistics is horribly wrong because it shows only users that open host app.
I asked from Flurry support can I run analytics code in today extension and they answered no. So I want to start analytics code in my host app when today extension starts. Is this possible?
No, Your today extension is a separate app and not part of your main app. You could write some analytics to a shared resource and push it Flurry when the user decides to open de main app.

IOS Action Extension Sample Code

i am new to IOS development ,IOS 8 have app extension feature, i have gone through Apple document ,i got some idea what is app extension but i want do sample non UI action extension app example. i tried ui based extension app ,it's working fine.and also i tried non ui base app but it not working. please can any one explain non ui extension with sample code.
just take simple case Host app will send x and y values to extension , extension need to receive that two values and do some manipulation in extension app and send result to host app and host need to display on ui.

Resources