iOS share extension in chrome - google-chrome

Just added share extension to my app.
It's working fine in safari.
When launching it from Chrome the "ExtensionPreprocessingJS.js" isn't called.
as a result the following code is not invoked:
MyExtensionJavaScriptClass.prototype = {
run: function(arguments) {
// Pass the baseURI of the webpage to the extension.
arguments.completionFunction({"baseURI": document.baseURI});
},
Please advise.

That behavior is documented as being a Safari option, not something that applies to every web browser.
In Share extensions (on both platforms) and Action extensions (iOS only), you can give users access to web content by asking Safari to run a JavaScript file and return the results to the extension.
[Emphasis mine]

Try to put/replace/add this to your info.plist of extension
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsText</key>
<true/>
<key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
</dict>
<key>NSExtensionJavaScriptPreprocessingFile</key>
<string>DemoPreprocessor</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>

No need for that complicated stuff. This works both in Google Chrome and Safari:
override func viewDidLoad() {
super.viewDidLoad()
for item in extensionContext!.inputItems {
if let attachments = item.attachments {
for itemProvider in attachments! {
itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (object, error) -> Void in
if object != nil {
println(object) //This is your URL
}
})
}
}
}
}

Related

Chrome manifest V3 extensions and externally_connectable documentation

After preparing the migration of my chrome manifest V2 extension to manifest V3 and reading about the problems with persistent service workers I prepared myself for a battle with the unknown. My V2 background script uses a whole bunch of globally declared variables and I expected I need to refactor that.
But to my great surprise my extension background script seems to work out of the box without any trouble in manifest V3. My extension uses externally_connectable. The typical use case for my extension is that the user can navigate to my website 'bla.com' and from there it can send jobs to the extension background script.
My manifest says:
"externally_connectable": {
"matches": [
"*://localhost/*",
"https://*.bla.com/*"
]
}
My background script listens to external messages and connects:
chrome.runtime.onMessageExternal.addListener( (message, sender, sendResponse) => {
log('received external message', message);
});
chrome.runtime.onConnectExternal.addListener(function(port) {
messageExternalPort = port;
if (messageExternalPort && typeof messageExternalPort.onDisconnect === 'function') {
messageExternalPort.onDisconnect(function () {
messageExternalPort = null;
})
}
});
From bla.com I send messages to the extension as follows
chrome.runtime.sendMessage(EXTENSION_ID, { type: "collect" });
From bla.com I receive messages from the extension as follows
const setUpExtensionListener = () => {
// Connect to chrome extension
this.port = chrome.runtime.connect(EXTENSION_ID, { name: 'query' });
// Add listener
this.port.onMessage.addListener(handleExtensionMessage);
}
I tested all scenarios including the anticipation of the famous service worker unload after 5 minutes or 30 seconds inactivity, but it all seems to work. Good for me, but something is itchy. I cannot find any documentation that explains precisely under which circumstances the service worker is unloaded. I do not understand why things seem to work out of the box in my situation and why so many others experience problems. Can anybody explain or refer to proper documentation. Thanks in advance.

iOS: Excluding certain URLs when running webview

I am using Swift and have web-based content (an external webpage) embedded in WebView for my native app. On that webpage, again, which is in-app, I need to make one link open in Safari and not in the App. HTML target _blank code on the webpage doesn’t work (I wish it was that easy), looking for the right code to do it in Swift.
I have used this code for uiwebview:
#IBOutlet var news: UIWebView!
var theURL = "http://"
override func viewDidLoad() {
super.viewDidLoad()
loadWebPage()
}
func loadWebPage(){
let requestURL = NSURL (string: theURL)
let URLrequest = NSURLRequest (URL: requestURL!)
news.loadRequest(URLrequest)
I have used this code for WKNaviagtionDelegate:
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (navigationAction.navigationType == WKNavigationType.LinkActivated && !navigationAction.request.URL!.host!.lowercaseString.hasPrefix("http://")) {
UIApplication.sharedApplication().openURL(navigationAction.request.URL!)
decisionHandler(WKNavigationActionPolicy.Cancel)
} else {
decisionHandler(WKNavigationActionPolicy.Allow)
}
Best, Drew
Because you need to create a button that looks like a link.
that should do the trick:
UIApplication.sharedApplication().openURL(NSURL(string: "http://...")!)
If the button is actually on a webpage in WebView... the link is not really controlled by the App unless you manipulate the link in iOS. It looks like the HTML target="_new" tag on the button might work in a later version of iOS. It's a bug in iOS 7 and was fixed in 7.0.3. Try a higher iOS version target for the App with the HTML target tag on the button.
How to open Safari from a WebApp in iOS 7

iOS open link with maps apps

I'm working on an event app and I'm trying to add a "Get directions" button that opens up directions in either Apple Maps or Google Maps. I'm happy to use Apple maps for now because it's easy to embed with http://maps.apple.com/?q=XYZ().
My app is displaying an HTML website with a UIWebView, so this may be the problem, but when you press the link it actually opens within the UIWebView and displays Google Maps randomly but accurately. Is there a function I can put into my HTML code that forces the link to open in the native Apple or Google Maps?
COMPLETE ANSWER
CODE:
- (IBAction)getDirectionsAction:(id)sender {
NSURL *googleURL = [[NSURL alloc]
initWithString:[NSString stringWithFormat:#"comgooglemaps://?daddr=%#", #"44.294349,-70.326973"]];
NSURL *googleWebURL =
[[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://www.maps.google.com/maps?daddr=%#",
#"44.294349,-70.326973"]];
NSURL *appleURL = [NSURL URLWithString:#"http://maps.apple.com/?daddr=311+East+Buckfield+Road+Buckfield+Maine"];
NSURL *wazeURL = [NSURL URLWithString:#"waze://?ll=44.294349,-70.326973&navigate=yes"];
// Lets try the Waze app first, cuz we like that one the most
if ([[UIApplication sharedApplication] canOpenURL:wazeURL]) {
[[UIApplication sharedApplication] openURL:wazeURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// Lets try the Apple Maps app second
if ([[UIApplication sharedApplication] canOpenURL:appleURL]) {
[[UIApplication sharedApplication] openURL:appleURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// If Apple Maps is unsuccessful, let's try the Google Maps app
if ([[UIApplication sharedApplication] canOpenURL:googleURL]) {
[[UIApplication sharedApplication] openURL:googleURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// Uh, oh...Well, then lets launch it from the web then.
else {
[[UIApplication sharedApplication] openURL:googleWebURL
options:#{}
completionHandler:^(BOOL success){
}];
}
}
PLIST PROPERTIES:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>http://maps.apple.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>http://maps.google.com/</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
</dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>waze</string>
<string>comgooglemaps</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>For Use for directions</string>
BUTTON
1X
2X
3X
Instead of using the http protocol in your URL, try using the maps protocol instead, so that your link looks something like:
maps://maps.apple.com/?q=Test+Search
You can also open it in Google Maps, if the user has it installed, by using a URL such as:
comgooglemaps://?q=Test+Search
Though you might get unexpected results if the user doesn't have it installed.
If this doesn't work, it's possible to use the UIWebView delegate method webView:shouldStartLoadWithRequest:navigationType: to intercept links and open the Maps application properly.
Here you can find info on the Google Maps URL Scheme
UIApplication.sharedApplication().openURL(NSURL(string:
"comgooglemaps://?q=XYZ")!)
Will enforce Google Maps to be used.
You might want to do
UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)
first, to ensure that Google Maps is available. If not, simply call your regular
http://maps.apple.com/?q=XYZ()
to open apple maps.

How can I pass data between two Chrome apps?

I have created two Chrome apps and I want to pass some data (string format) from one Chrome app to another Chrome app. Appreciate if someone can help me with showing the correct way of doing this?
It's an RTFM question.
From Messaging documentation (note that it mentions extensions, but it works for apps):
In addition to sending messages between different components in your extension, you can use the messaging API to communicate with other extensions. This lets you expose a public API that other extensions can take advantage of.
You need to send messages using chrome.runtime.sendMessage (using app ID) and receive them using chrome.runtime.onMessageExternal event. If required, long-lived connections can also be established.
// App 1
var app2id = "abcdefghijklmnoabcdefhijklmnoab2";
chrome.runtime.onMessageExternal.addListener(
// This should fire even if the app is not running, as long as it is
// included in the event page (background script)
function(request, sender, sendResponse) {
if(sender.id == app2id && request.data) {
// Use data passed
// Pass an answer with sendResponse() if needed
}
}
);
// App 2
var app1id = "abcdefghijklmnoabcdefhijklmnoab1";
chrome.runtime.sendMessage(app1id, {data: /* some data */},
function(response) {
if(response) {
// Installed and responded
} else {
// Could not connect; not installed
// Maybe inspect chrome.runtime.lastError
}
}
);

Issue with CDialog::DoModal() when called from firebreath plugin

I have my ActiveX COM component developed in VC6. I have created the firebreath plugin over it to be able to call the COM API from different browsers. I have one API in ActiveX component which pops up the CDialog UI, On Google Chrome browser dlg.DoModal() function is failing. Issue is only with Chrome it simply crashes at this call, in other browsers its working perfectly
On Windows 7 its working with Google Chrome as well issue is with Windows XP.
Please provide me some feedback on this issue.
I am attaching some code snippets here to give some idea of what I am trying to do
Firebreath Plugin code (Plugin name is FBTest):
bool FBTest::onWindowAttached(FB::AttachedEvent *evt, FB::PluginWindow *piw)
{
// The window is attached; act appropriately
try {
/* Now that we have the plugin window, create the ActiveX container
window as a child of the plugin, then create the ActiveX control
as a child of the container.
*/
FB::PluginWindowWin* pwnd = piw->get_as<FB::PluginWindowWin>();
if(pwnd != NULL)
{
HWND hWnd = pwnd->getHWND();
if(hWnd)
{
// Create the ActiveX control container
RECT rc;
::GetClientRect(hWnd, &rc);
m_fbTestWin.Create(hWnd, &rc, 0, WS_VISIBLE|WS_CHILD);
CComPtr<IUnknown> spControlTest;
//ETESTPROGID is prog id of activex component
HRESULT hrTest = m_fbTestWin.CreateControlEx(ETESTPROGID, NULL, NULL, &spControlTest, GUID_NULL, NULL);
if(SUCCEEDED(hrTest) && (spControlTest != NULL))
{
spControlTest.QueryInterface(&m_eTestAxCtl);
g_eTestAxCtl = m_eTestAxCtl;
if (m_eTestAxCtl)
{
//TODO: should we throw a FB exception here?
}
}
}
}
} catch(...) {
//TODO: should we throw a FB exception here?
}
return false;
}
void FBTest::TestFunc()
{
//hThread = (HANDLE)_beginthreadex(NULL, 0,&FBTest::Start, (void*)&m_eTestAxCtl, 0, &ThreadId);
if(m_eTestAxCtl)
{
try {
long nCode = -1;
//This is call to API of Activex component which will popup the dialog
HRESULT hr = m_eTestAxCtl->TestFunc();
//return nCode;
}
catch(...) {
}
}
}
Activex Component code :
STDMETHODIMP CTest::TestFunc()
{
//CTestDlg is ATL Dialog Object
CTestDlg TestDlg;
//At this call Google chrome is crashing
if(!TestDlg.DoModal())
return S_FALSE;
return S_OK;
}
I am calling TestFunc() API of plugin from one HTML page and its showing me the dailog in IE and firefox browsers but Chrome is crashing..
Please help.
Since you've given no info on when or how you're calling it, it's hard to be sure, but my guess is that you're calling DoModal on the main thread. That will cause the main thread to block.
You must never block the main thread in a plugin.
Try calling it on a different thread.