Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. iOS UDID or similar
Forum Updated to NodeBB v4.3 + New Features

iOS UDID or similar

Scheduled Pinned Locked Moved Solved Mobile and Embedded
8 Posts 6 Posters 6.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • metaDomM Offline
    metaDomM Offline
    metaDom
    wrote on last edited by
    #1

    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;
    }
    
    Aleksey_K 0A 1 Reply Last reply
    1
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      1
      • D Offline
        D Offline
        DRoscoe
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ceyhun
          wrote on last edited by
          #4

          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 ?

          D 1 Reply Last reply
          0
          • C Offline
            C Offline
            ceyhun
            wrote on last edited by
            #5

            Can somebody help me?

            1 Reply Last reply
            0
            • C ceyhun

              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 ?

              D Offline
              D Offline
              DRoscoe
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Alexeporubay
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                -1
                • metaDomM metaDom

                  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;
                  }
                  
                  Aleksey_K 0A Offline
                  Aleksey_K 0A Offline
                  Aleksey_K 0
                  wrote on last edited by Aleksey_K 0
                  #8

                  @metaDom converted Your result to UUID:

                  QCryptographicHash hash(QCryptographicHash::Sha1);
                  hash.addData(getAndroidID().toLocal8Bit());
                  auto id = QUuid::fromRfc4122(hash.result().left(16));
                  
                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved