Dror 'Yitzhakov
Dror 'Yitzhakov

Reputation: 280

Get a unique device id using phonegap

I am developing an application using phonegap for both ios and android. My application is going to have a free trial of 5 items, for every additional item the user will have to pay.

My problem is hat i cant get any unique identifier of the device using my app, so a user can use the 5 free items , then uninstall and reinstall the App and get aditional 5 free items.

I have tried using the device.udid provided by phonegap api , but it changes every time i reinstall it.

Any ideas?

Upvotes: 2

Views: 9646

Answers (3)

Cliff Ribaudo
Cliff Ribaudo

Reputation: 9039

Get the MAC address of the device and do a SHA1 hash on it to obfuscate. There is open source code out there for getting it for Android and iOS devices.

And this type of solution works regardless of if you continue to use PhoneGap or write native code in the future. Also, Apple has no issues with it as far as the App review process is concerned.

UPDATE: This WAS the right answer up to and through iOS 6, however for all versions of the iOS SDK GREATER THAN 6, this is NO LONGER POSSIBLE. Any attempt to use the low level calls which formerly worked now ALWAY return a MAC Address of 2. Advertising ID and the AdSupport.framework is the only way to. If you have a jailbroken phone and can get root privileges, it might still work. Not sure.

Upvotes: 5

Cruceo
Cruceo

Reputation: 6834

Personally, I find the best way for a unique Android ID to is to just generate a random String ranging from 16-26 alphanumeric characters, check it doesn't exist on your server, and then save it in the local preferences.

I've experimented with other ways to get a unique ID (IE. TelephonyManager), but all of the ones I'm aware of have the chance of returning null (which isn't good at all). If anyone else has a better way that cannot potentially return null, I would love to hear it.

Upvotes: 1

Soumyadip Das
Soumyadip Das

Reputation: 1791

use device IMEI as follows--

Activity File:

public class Welcome extends Activity {

    private TextView display;
    private Button checkAgain;
    private TelephonyManager telephonyManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        display = (TextView) findViewById(R.id.display);
        checkAgain = (Button) findViewById(R.id.buttonCheckAgain);

        display.setText("");

        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        updateDisplay();

        checkAgain.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                updateDisplay();
            }
        });

    }

    private void updateDisplay() {
        display.setText(getDeviceID(telephonyManager));
        System.out.println("Display upadated");
    }

    private String getDeviceID(TelephonyManager phonyManager) {

        String id = phonyManager.getDeviceId();
        if (id == null) {
            id = "not available";
        }

        int phoneType = phonyManager.getPhoneType();
        switch (phoneType) {
        case TelephonyManager.PHONE_TYPE_NONE:
            return "NONE: " + id;

        case TelephonyManager.PHONE_TYPE_GSM:
            return "GSM: IMEI=" + id;

        case TelephonyManager.PHONE_TYPE_CDMA:
            return "CDMA: MEID/ESN=" + id;

        default:
            return "UNKNOWN: ID=" + id;
        }
    }
}

and you need this permission:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Upvotes: 1

Related Questions