How to use the .mm file in Qt Project for IOS?



  • Hello

    I refer to this post regarding getting the UUID on IOS.

    Re: iOS UDID or similar

    I am thoroughly confused on the steps to be taken in order to do this.

    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);
    }
    

    Where am I supposed to put this? Is this to be put in a new .mm file and incorporate in the project or else what?

    I tried to find out how to do that but it seems there is hardly any documentation on this.

    Can someone please elaborate on the exact steps to do this?



  • Just put it in the ios section of your pro file.

    Here you have a sample pro file that contains a *.mm file: https://github.com/gympulsr/qt-pushnotifications/blob/master/qt-pushnotifications.pro



  • Thanks Schluchti

    That answers the basic question of adding the .mm file to the project.

    But it does not end here... the code given in the refered post is not a complete .mm type code. Please excuse me as i am totally ignorant about the .mm files. But what i do understand is that QString is not a cocoa code but a Qt Code..

    So If I just copy the code from the refered post to a macaddress.mm file, I get two errors

    error: unknown type name 'QString'
    QString UserPreferenceUtility::getMacAddress()
    ^
    and
    error: use of undeclared identifier 'UserPreferenceUtility'
    QString UserPreferenceUtility::getMacAddress()
    ^
    I know I am not doing things the right way... but what is the right way?



  • @TheCrowKaka

    That's probably because you are missing the header file. It should work the following way:

    Create a userpreferenceutility.h file with the following content:

    #ifndef USERPREFERENCEUTILITY_H
    #define USERPREFERENCEUTILITY_H
    
    #include <QObject>
    #include <QString>
    
    class UserPreferenceUtility{
        Q_OBJECT
    public:
        UserPreferenceUtility(QObject* parent = 0);
       ~UserPreferenceUtility();
       QString getMacAddress();
    };
    
    #endif /*USERPREFERENCEUTILITY_H*/
    

    Then add the file to the ios section of your pro file:

    ios{
        HEADERS += $$PWD/path_to_your_file/userpreferenceutility.h \
    }
    

    Next, implement the userpreferenceutility.mm:

    #include "userpreferenceutility.h"
    
    UserPreferenceUtility::UserPreferenceUtility(QObject *parent)
            : QObject(parent)
     {
     }
    
    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);
    }
    
    UserPreferenceUtility::~UserPreferenceUtility(){
    }
    

    And add the file also to the ios section of your pro file. That means you pro file should now look like similar to this:

    ios{
        HEADERS += $$PWD/path_to_your_file/userpreferenceutility.h \
        OBJECTIVE_SOURCES +=   $$PWD/path_to_your_file/userpreferenceutility.h \
    }
    

    Just typed that by heart, so there might be compiling errors ;)



  • Thanks

    It works perfect.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.