How to use the .mm file in Qt Project for IOS?
-
wrote on 20 Mar 2017, 19:21 last edited by
Hello
I refer to this post regarding getting the UUID on IOS.
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?
-
wrote on 20 Mar 2017, 19:29 last edited by
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
-
wrote on 20 Mar 2017, 19:55 last edited by
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? -
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?wrote on 20 Mar 2017, 20:29 last edited by SchluchtiThat'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 ;)
-
wrote on 21 Mar 2017, 16:30 last edited by
Thanks
It works perfect.
1/5