Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt Creator and iOS



  • Hi. I've been developing an app for Android, now I need to add support for iOS. I would like to keep using Qt Creator, so I can keep a single code base. I'm trying to mix C++ and Objective-C to use some native features or frameworks. I'm a little bit confused about the anatomy of a Qt app for iOS. I've been looking for some article like this one for Android

    https://blog.qt.io/blog/2013/07/23/anatomy-of-a-qt-5-for-android-application/

    but couldn't find anything. I'm also having trouble coding with Qt Creator. Let's say I have this code:

    #include <QtGui>
    #include <QtGui/5.5.0/QtGui/qpa/qplatformnativeinterface.h>
    
    #import <UIKit/UIKit.h>
    #import <FBSDKCoreKit/FBSDKCoreKit.h>
    #import <FBSDKLoginKit/FBSDKLoginKit.h>
    
    void MyController::facebookLogin(){
    
    	QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
    
    	UIView *view = static_cast<UIView *>(nativeInterface->nativeResourceForWindow("uiview", qApp->topLevelWindows().at(0)));
    
    	UIViewController *qtController = [[view window] rootViewController];
    
    	FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    	[login
    	  logInWithReadPermissions: @[@"public_profile",@"email"]
    			fromViewController: qtController
    					   handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    	  if (error) {
    		NSLog(@"Process error");
    	  } else if (result.isCancelled) {
    		NSLog(@"Cancelled");
    	  } else {
    		NSLog(@"Logged in");
    	  }
    	}];
    
    }
    

    I can compile it, but Qt Creator puts a red underline on every Objective-C code. It cannot parse headers imported from Facebook SDK also.

    Is there any way to work with Objective-C on Qt Creator or should I move to XCode? In that case, how can I keep my XCode project synchronized with my .pro file?

    Thank you.


  • Lifetime Qt Champion

    Hi,

    Since you are mixing both you're using Objective-C++ so is your file ending in mm ? Did you add it to OBJECTIVE_SOURCES in your pro file ?



  • Hi. Yes, I did. Here's part of my pro file:

    TEMPLATE = app
    
    QT += qml quick widgets
    
    SOURCES += mycontroller.cpp 
    HEADERS += mycontroller.h
    
    ios {
    
    	QMAKE_CXXFLAGS += -std=c++11 -stdlib=libc++ -F$$PWD/ios
    	QMAKE_LFLAGS += -stdlib=libc++ -F$$PWD/ios
    
    	OBJECTIVE_SOURCES += mycontroller_ios.mm
    
    	QMAKE_INFO_PLIST = ios/Info.plist
    
    	LIBS += -lz -framework UIKit -framework FBSDKCoreKit -framework FBSDKLoginKit
    
    }

  • Lifetime Qt Champion

    Where's the Facebook SDK located ?



  • Inside the ios folder. I already had a subfolder for the android tree, so I wanted to keep the pattern and created a subfolder for ios. As I said before, I can compile it, so I guess qmake is working properly.


  • Lifetime Qt Champion

    Sorry, I may have misunderstood your problem. Did you mean that the you "only" have problem with auto-completion and such with Qt Creator ?



  • Yes. Check it out:


  • Lifetime Qt Champion

    IIRC, you should rather have LIBS += -F $ $PWD/iOS (minus the spaces)



  • Compilation works, but the editor still highlights Objective-C code... =(

    ios {
    
    	QMAKE_CXXFLAGS += -std=c++11 -stdlib=libc++
    	QMAKE_LFLAGS += -stdlib=libc++
    
    	OBJECTIVE_SOURCES += mycontroller_ios.mm
    
    	QMAKE_INFO_PLIST = ios/Info.plist
    
    	LIBS += -lz -F$$PWD/ios -framework UIKit -framework FBSDKCoreKit -framework FBSDKLoginKit
    
    }

  • Lifetime Qt Champion

    Did you try using the clang code model ?



  • Hi. What do you mean by that?


  • Lifetime Qt Champion

    Qt Creator provides two code models (the beast that analyses your projects and provides all the nice stuff like auto-completion of your classes etc.) The default and the clang implementation that you can activate through the About Plugins menu.



  • You nailed it!

    For those interested:

    http://doc.qt.io/qtcreator/creator-clang-codemodel.html

    After enabling the plugin, go to

    Preferences > C++ > Code Model

    I changed both "Objective C" and "Objective C++" to clang. After that, the highlighting is gone.

    It seems ok now. I can't test it properly, because I just upgraded to XCode 7 and broke Qt Creator's support for iOS, but it feels like to right solution.

    Thank you.


  • Lifetime Qt Champion

    What did break ?



  • Apparently, Qt Creator is not compatible with XCode 7 toolchain. After upgrading, the compiler for iOS went missing.


  • Lifetime Qt Champion

    What version of Qt Creator are you using ? IIRC Qt Creator 3.5.1 and Qt 5.5.1 don't suffer from that problem.



  • I'm using Qt 5.5.0 with Qt Creator 3.4.2. Maybe that's why, but I've already came to terms with using XCode. It works fine.

    Thank you.


Log in to reply