Qt for iOS

Qt iOS 端口允許在 iOS 設備中運行 Qt 應用程序,譬如 iphone、ipad 和 iPod Touch。

支持的配置

支持下列配置。

目標平颱 體係結構 構建環境
iOS 14, 15, 16 armv8 ( arm64 ) Xcode 13 (iOS 15 SDK), Xcode 14 (iOS 16 SDK)

快速入門

Qt supports you in building, testing, and deploying applications for iOS. Qt applications are typically defined using the CMake or qmake build tools. Both tools can generate an .xcodeproj file that can then be loaded and built from the command line, or with Xcode. Qt Creator also directly supports building, running, debugging, and profiling CMake and qmake projects for iOS.

The minimum deployment target for Qt applications is specified in 支持平颱 .

設置開發環境

You can download the Qt installers from the 下載 頁麵。更多信息,見 Qt 快速入門 .

安裝 Qt 前,首先需要安裝 Xcode。可以找到它在 Mac App Store here .

注意: As recommended by Apple, you should always use the latest Xcode version when building your applications for the App Store. In practice this means you also need the latest version of macOS to develop apps with Qt, due to Xcode's system requirements.

For running Qt applications on your Mac or in the simulator that comes with Xcode, this is all you need. However, for running applications on a mobile device and/or publishing your applications in the App Store, you must join the Apple 開發者程序 , and set up developer certificates and provisioning profiles. The easiest solution is to use a profile that takes any App ID (a * ).

Before building any Qt applications, you should test that Xcode is set up correctly, for example, by running one of the standard Xcode application templates on your device.

Building Applications from the Command Line

Use CMake or qmake to define how to build your iOS application. Both CMake and qmake can generate an xcodeproj file, which can then be loaded and built from the command line.

使用 CMake

The qt-cmake convenience script located in <Qt-dir>/<version>/ios/bin/ will take care of setting up the toolchain and correct architectures for you.

使用 qt-cmake convenience script:

<Qt-dir>/<version>/ios/bin/qt-cmake <source-dir>
					

Using the generated xcodeproj file, you can either use Xcode to build your application or run xcodebuild from the command line. For a list of available targets and schemes for your application, run the following command:

xcodebuild -list -project <your-app>.xcodeproj
					

Then, run xcodebuild build , passing in your application details:

xcodebuild build -allowProvisioningUpdates -project <your-app>.xcodeproj -scheme <your-scheme> -configuration Debug -destination "generic/platform=iOS" -destination-timeout 1 ENABLE_ONLY_ACTIVE_RESOURCES=NO
					

使用 qmake

First, define how to build the application using qmake. Then, use the generated xcodeproj file to build the application, either in Xcode or from the command line.

qmake <your-app>.pro
					

qmake creates a wrapper Makefile that in turns calls xcodebuild , so you can run make to build your application:

make -j8
					

Note that you must re-import the project if its setup changes, for example, when adding or removing source files.

Customizing Xcode project settings

The QMAKE_MAC_XCODE_SETTINGS qmake variable can be used to customize Xcode settings, for example:

development_team.name = DEVELOPMENT_TEAM
development_team.value = <your-team-id>
QMAKE_MAC_XCODE_SETTINGS += development_team
					

Other qmake variables are also useful:

QMAKE_TARGET_BUNDLE_PREFIX = com.<your-company>
QMAKE_BUNDLE = <your-app>
					

采用 Qt Creator 構建應用程序

You can find information on how to set up and run Apple mobile device applications in Qt Creator's manual:

As mentioned previously, you must have Xcode installed.

在 Qt 應用程序中使用 Objective-C 代碼

Clang, the compiler used for applications on Apple Platforms, allows mixing C++ and Objective-C code. To enable this mode use the .mm extension for the relevant source files and add them to your project as usual.

With CMake:

target_sources(myapp PRIVATE objc_code.mm)
					

With qmake:

SOURCES += objc_code.mm
					

You can then use Objective-C frameworks from Apple's Developer Library in your Qt applications.

To expose functionality to the rest of your application, without having to rename all your source files, declare helper functions in a header, and implement the functionality in an Objective-C++ source file:

// objc_code.h
QString localizedHostName();
// objc_code.mm
#include <Foundation/NSHost.h>
QString localizedHostName()
{
    return QString::fromNSString(NSHost.currentHost.localizedName);
}
					

iOS 範例

In Qt Creator, tested examples on iOS can be looked up. Use the ios keyword to search for examples in the Qt Creator Welcome mode. Note that some examples may have limited functionality.

已知工作於 iOS 設備的範例列錶,拜訪 Qt for iOS 範例 .

下列主題提供瞭關於 Qt for iOS 的更多細節: