Qt World Summit: Register Today!

iOS UDID or similar

  • Hi guys,

    I'm looking for a way to retrieve a unique device identifier on iOS devices. On Android I use one of the following methods and would like to ifdef a similar one for iOS:

    QString PhoneClient::getUDID()
        QAndroidJniEnvironment env;
        jclass contextClass = env->FindClass("android/content/Context");
        jfieldID fieldId = env->GetStaticFieldID(contextClass, "TELEPHONY_SERVICE", "Ljava/lang/String;");
        jstring telephonyManagerType = (jstring) env->GetStaticObjectField(contextClass, fieldId);
        jclass telephonyManagerClass = env->FindClass("android/telephony/TelephonyManager");
        jmethodID methodId = env->GetMethodID(contextClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
        QAndroidJniObject qtActivityObj = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",  "activity", "()Landroid/app/Activity;");
        jobject telephonyManager = env->CallObjectMethod(qtActivityObj.object<jobject>(), methodId, telephonyManagerType);
        methodId = env->GetMethodID(telephonyManagerClass, "getDeviceId", "()Ljava/lang/String;");
        jstring jstr = (jstring) env->CallObjectMethod(telephonyManager, methodId);
        jsize len = env->GetStringUTFLength(jstr);
        char* buf_devid = new char[32];
        env->GetStringUTFRegion(jstr, 0, len, buf_devid);
        QString imei(buf_devid);
        delete buf_devid;
        return imei;
    QString PhoneClient::getAndroidID()
        QAndroidJniObject myID = QAndroidJniObject::fromString("android_id");
        QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
        QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;");
        QAndroidJniObject contentR = appctx.callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
        QAndroidJniObject result = QAndroidJniObject::callStaticObjectMethod("android/provider/Settings$Secure","getString", "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;",contentR.object<jobject>(), myID.object<jstring>());
        QString androidID = result.toString();
        return androidID;

  • Hi, if you're on iOS 7.0 or later a unique device identifier (like the iPhone's UDID that iTunes shows) or no longer available using Swift or Objective-C.

    The alternative is a "semi-permament" unique device identifier, called Vendor ID, in Objective-C: UIDevice identifierForVendor] more here

    (You can still get the real UDID the hard way by installing a profile, for example by using Diawi

  • We used a similar approach as mentioned by @hskoglund. Incorporate a .mm file in your project to define an Objective-C++ method such as:

    QString UserPreferenceUtility::getMacAddress()
        NSString* mac_address = @"00-00-00-00-00-00-00-01";
        if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
            mac_address = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        return QString::fromNSString(mac_address);

    We are calling it the MAC address, just because we're using as the equivalent of a MAC address, which we use on the non-iOS side for unique identification.

  • Hi, I have an similar problem i mean exactly same problem. But I dont get the answers ? You guys , have you been trying to solve problem on Qt Creator ? Because i didnt find any code example works with only iOS like java code which works on android. Also can anybody show me solution or guidance for this ?

  • Can somebody help me?

  • @ceyhun The example I posted above is exactly how you would do it on iOS. It's an Objective C++ method that is included in a .mm file. This is by definition for iOS only.

  • This post is deleted!

  • @metaDom converted Your result to UUID:

    QCryptographicHash hash(QCryptographicHash::Sha1);
    auto id = QUuid::fromRfc4122(hash.result().left(16));

Log in to reply