I've added a +1 button in my app:
I've used this code:
<div class="g-plusone" data-size="tall" data-href="GOOGLE PLAY STORE LINK TO MY APP"></div>
and
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
I've also allowed the api:
index.html:
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://apis.google.com">
config.xml:
<allow-navigation href="https://apis.google.com" />
The problem:
This works on the browser (ionic serve
) but It doesn't work on the app...
When I click nothing happens... (no errors...)
Anyway I can make this work on the app? (ionic run
)
More information/debug info:
- If I've clicked the +1 button on the web it doesn't show me the red button on the app (Read means I've already shared that link)(It doesn't know who I am..)
- I don't want to make a login/signup, just a +1 button...
If I add:
<allow-navigation href="*" />
in config.xml, it asks me to login when I click the +1 button: (It shouldn't)
This means the +1 button doesn't work because is "in an anonymous browser", not authenticated with the OS...
I also created a demo pure Android app following this instructions: https://developers.google.com/+/mobile/android/recommend It works perfectly... (I can +1 the link...)
My possibilites:
Some way to make a native Android view with the +1 button appear on the webview.
Make a fake +1 button and when it's clicked it calls a plugin that makes some king of request/click on the real +1 button....
Any suggestion on how to do it?
Are any of these two possibilities possible?
Thanks for your help!
If you don't want to handle the login, you can build a plugin to add a native
PlusOneButton
. I'm working on it right now, I'll try to release it this afternoon, but as moderators don't like "link only asnwers" I'll explain here how to build the plugin, and I'll add the link once I finish developing it.First of all, read the guide about how to build a cordova plugin, I'm not going to enter into that level of detail.
Your CordovaPlugin subclass will need this variables:
Your execute method should be like this:
Finally add the
onResume
method to keep the button status up to date.The plugin will have to add the
com.google.android.gms:play-services-plus
dependencyYou can do that adding this line to the plugin.xml
EDIT: The plugin is ready https://github.com/jcesarmobile/plusOneButtonPlugin
To install it
To use it:
Or
Your problem is that the user isn't logged in with their Google account inside the cordova browser, so obviously, you see a login prompt when clicking the +1 button.
I understand you want to access the global Google account connected to the user's phone/OS and use this one inside your app. First, your app needs credentials to access this data, then those need to be accessible from within the cordova browser. There's this plugin which does that for you.
In your initialization code, you can then call
window.plugins.googleplus.login(...)
, which will prompt the user for confirmation (if necessary) and log in.Make sure that the +1 button is initialized after logging in, so the state is indicated correctly. For instance (assuming you're using jQuery):