Get device email in Android

Hi everyone,

I just published a cordova plugin to get accounts registred on device.
It’s really usefull to get the user email :slight_smile:

If you want to take a look, it’s here : https://github.com/loicknuchel/cordova-device-accounts

Any feedback is welcome !!!

6 Likes

Very nice, looks good! Any thoughts on doing an iOS version as well? Maybe even a ngcordova wrapper? :smile:

I don’t have an iPhone or a mac book… :frowning:
So, the iPhone version would be difficult to me… But I will be very pleased to merge an implementation if somebody want to do it !

For the ngCordova wrapper, it could be great but this project is not mature enought I think… (no tests, api not really eprouved…).

But I can share the service I use to wrap it in my app :

// for Accounts plugin : hhttps://github.com/loicknuchel/cordova-device-accounts
.factory('AccountsSrv', function($q, $window, $ionicPlatform){
  'use strict';
  var service = {
    getAccounts: getAccounts,
    getByType: getByType,
    getEmails: getEmails,
    getEmail: getEmail
  };

  function getAccounts(){
    var defer = $q.defer();
    pluginReady(function(){
      $window.plugins.DeviceAccounts.get(function(accounts){
        defer.resolve(accounts);
      }, function(error){
        defer.reject(error);
      });
    });
    return defer.promise;
  }

  function getByType(type){
    var defer = $q.defer();
    pluginReady(function(){
      $window.plugins.DeviceAccounts.getByType(type, function(accounts){
        defer.resolve(accounts);
      }, function(error){
        defer.reject(error);
      });
    });
    return defer.promise;
  }

  function getEmails(){
    var defer = $q.defer();
    pluginReady(function(){
      $window.plugins.DeviceAccounts.getEmails(function(emails){
        defer.resolve(emails);
      }, function(error){
        defer.reject(error);
      });
    });
    return defer.promise;
  }

  function getEmail(){
    var defer = $q.defer();
    pluginReady(function(){
      $window.plugins.DeviceAccounts.getEmail(function(email){
        defer.resolve(email);
      }, function(error){
        defer.reject(error);
      });
    });
    return defer.promise;
  }

  function pluginReady(fn){
    $ionicPlatform.ready(function(){
      if($window.plugins && $window.plugins.DeviceAccounts){
        fn();
      } else {
        console.log('pluginNotFound:DeviceAccounts');
      }
    });
  }

  return service;
})

It can be used like this :

AccountsSrv.getEmail().then(function(email){
  // some work with first user email registered on device
});
3 Likes

This looks good and I am going to use it in my app.

Thanks for plugin! Please can you give example app with this module?

Hi,

I didn’t have time for that right now but it couldn’t be difficult to integrate it…
Let me know if you have troubles.

@loicknuchel Please suggest me any cordova plugin to get accounts registred on device(IOS).

Didn’t find any plugin to do that, that’s why I created this plugin.
But I only use Android right now…

BTW, I’m not sure this feature is even possible on iOS…

It seems this plugin does not work for me:

$ cordova -v
4.3.0
$ cordova plugin
com.ionic.keyboard 1.0.4 "Keyboard"
org.apache.cordova.console 0.2.13 "Console"
org.apache.cordova.device 0.3.0 "Device"
org.apache.cordova.globalization 0.3.4 "Globalization"
org.loicknuchel.cordova.deviceaccounts 0.0.1 "Device Accounts"

android:targetSdkVersion="21"

Would you help me with?

Thank you.

Hi @vlikin, what is your problem with the plugin ?

PS: it’s a Android only plugin

Yes, for android.
It seems the plugin is not attached to plugings.
.DeviceAccounts is undefined.

Can someone please tell me the steps to port this to iOS. I am willing to compile it and submit it to the community.

Hi @sukhsingh,

I’m not sure if it’s possible to do this on iOS but if you can you just have to follow the API called by JS : https://github.com/loicknuchel/cordova-device-accounts/blob/master/www/DeviceAccounts.js

A class DeviceAccounts with methods getDeviceAccounts and getDeviceAccountsByType which returns an array of objects with fields type and name.

Feel free to send a PR to the repo.

i get this error when debugging on emulator ReferenceError: AccountsSrv is not defined. is there a way around this yet?

Hi @EasyHero, AccountSrv is not defined in the plugin.
If you want to use it, you should copy/paste my sample in your project as your own code.

Hi @loicknuchel
I want you help me into plugin get device cordova accounts in ionic with method app.js. What about you share a controller or factory as share you below.

Thanks

Best regards

I have added plugin, plugin exists status is true.
but, it never hits success or failure functions. No return at all. why is that?

Same problem, please help us !

Does anyone have this working? I am just getting an empty array for accounts.