SwiftUI 和 DJI UX SDK 5(测试版)
SwiftUI and DJI UX SDK 5 (Beta)
我正在尝试将 UX SDK 5(Beta 0.4.1)集成到我的 SwiftUI 应用程序中。
首先我想显示 DUXBetaConnectionWidget
、DUXBetaFPVWidget
和 DUXBetaCompassWidget
。
我已经构建了两个包装器 classes.
第一个是UIViewControllerRepresantable
struct BetaDjiView<DjiWidget: DUXBetaBaseWidget>: UIViewControllerRepresentable {
typealias UIViewControllerType = BetaViewController<DjiWidget>
func makeUIViewController(context: Context) -> BetaViewController<DjiWidget> {
return BetaViewController<DjiWidget>()
}
func updateUIViewController(_ uiViewController: BetaViewController<DjiWidget>, context: Context) { }
}
第二个 class 是另一个 UIViewController
包装器,它实际上包含 DJI“Widgets”。
class BetaViewController<T: DUXBetaBaseWidget> : UIViewController {
var djiWidget = T()
override func viewDidLoad() {
super.viewDidLoad()
addChild(djiWidget)
djiWidget.install(in: self)
makeConstraints()
}
func makeConstraints() {
NSLayoutConstraint.activate([
djiWidget.view.topAnchor.constraint(equalTo: view.topAnchor),
djiWidget.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
djiWidget.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
djiWidget.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
}
在我的 ContentView
中,我正在像这样实例化小部件:
var body: some View {
GeometryReader { geometry in
ZStack {
BetaDjiView<DUXBetaFPVWidget>().frame(width: geometry.size.width, height: geometry.size.height)
VStack {
HStack {
BetaDjiView<DUXBetaConnectionWidget>().frame(width: 25, height: 25)
Spacer()
}
Spacer()
HStack {
BetaDjiView<DUXBetaCompassWidget>().frame(width: 75, height: 75)
Spacer()
}
}.padding()
}
}
}
与 SDKManager 中的产品的连接处于活动状态。
让我抓狂的奇怪部分来了:
我将 phone 连接到遥控器,打开遥控器,然后打开无人机。我让遥控器和无人机连接。
当我通过 Xcode 启动应用程序时一切正常。正在显示摄像头源并且连接图标变为绿色。
然后我从 Xcode 停止应用程序并直接从 phone 启动应用程序。该应用程序出现,但连接图标没有变化并保持红色,也没有显示相机画面。
当我将日志流式传输到 Mac 的控制台时,我可以看到与产品的连接就在那里。
这会是什么?非常感谢任何帮助。
编辑 1
这是我的 Podfile
platform :ios, '11.0'
target 'DjiMobileSdkTest2' do
use_frameworks!
pod 'iOS-Color-Picker'
pod 'DJI-UXSDK-iOS-Beta', '0.4.1'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |configuration|
target.build_settings(configuration.name)['ARCHS'] = '$(ARCHS_STANDARD)'
end
end
end
编辑 2
另一个额外的观察:我从 phone 中完全删除了该应用程序,然后从 Xcode 中重新启动它,我确认对话框要求允许使用蓝牙和位置,这是我想要的.然后该应用程序将再次没有反应并保持其初始状态。只有当我通过 Xcode 重新启动时,连接图标才会变绿并且显示视频脚。
编辑 3
我观察到:重新启动我的 phone 然后通过点击应用程序图标直接启动应用程序一切正常:应用程序启动,连接到无人机,连接图标变为绿色,并且相机提要也正在更新。我能够重复几次。
终于成功了!
如 this post 中所述,我不得不将 AppDelegate
及其 didFinishLaunchingWithOptions
生命周期重新引入我的应用程序。
这是之前的样子:
@main
struct MyApp: App {
var someClass = SomeClass()
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { (newScenePhase) in
switch newScenePhase {
case .active:
print("active")
someClass.doSomethingThatWouldNotWorkBefore()
case .background:
print("background")
case .inactive:
print("inactive")
@unknown default:
print("default")
}
}
}
}
这就是它的样子:
import SwiftUI
class AppDelegate: UIResponder, UIApplicationDelegate {
var someClass = SomeClass()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
someClass.doSomethingThatWouldNotWorkBefore()
return true
}
}
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { (newScenePhase) in
switch newScenePhase {
case .active:
print("active")
case .background:
print("background")
case .inactive:
print("inactive")
@unknown default:
print("default")
}
}
}
}
就这么简单 ;-)
我仍在努力找出确切的区别是什么,以及它与应用程序启动和生命周期、内存管理和“那些东西”有何关系。
我正在尝试将 UX SDK 5(Beta 0.4.1)集成到我的 SwiftUI 应用程序中。
首先我想显示 DUXBetaConnectionWidget
、DUXBetaFPVWidget
和 DUXBetaCompassWidget
。
我已经构建了两个包装器 classes.
第一个是UIViewControllerRepresantable
struct BetaDjiView<DjiWidget: DUXBetaBaseWidget>: UIViewControllerRepresentable {
typealias UIViewControllerType = BetaViewController<DjiWidget>
func makeUIViewController(context: Context) -> BetaViewController<DjiWidget> {
return BetaViewController<DjiWidget>()
}
func updateUIViewController(_ uiViewController: BetaViewController<DjiWidget>, context: Context) { }
}
第二个 class 是另一个 UIViewController
包装器,它实际上包含 DJI“Widgets”。
class BetaViewController<T: DUXBetaBaseWidget> : UIViewController {
var djiWidget = T()
override func viewDidLoad() {
super.viewDidLoad()
addChild(djiWidget)
djiWidget.install(in: self)
makeConstraints()
}
func makeConstraints() {
NSLayoutConstraint.activate([
djiWidget.view.topAnchor.constraint(equalTo: view.topAnchor),
djiWidget.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
djiWidget.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
djiWidget.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
}
在我的 ContentView
中,我正在像这样实例化小部件:
var body: some View {
GeometryReader { geometry in
ZStack {
BetaDjiView<DUXBetaFPVWidget>().frame(width: geometry.size.width, height: geometry.size.height)
VStack {
HStack {
BetaDjiView<DUXBetaConnectionWidget>().frame(width: 25, height: 25)
Spacer()
}
Spacer()
HStack {
BetaDjiView<DUXBetaCompassWidget>().frame(width: 75, height: 75)
Spacer()
}
}.padding()
}
}
}
与 SDKManager 中的产品的连接处于活动状态。
让我抓狂的奇怪部分来了:
我将 phone 连接到遥控器,打开遥控器,然后打开无人机。我让遥控器和无人机连接。
当我通过 Xcode 启动应用程序时一切正常。正在显示摄像头源并且连接图标变为绿色。
然后我从 Xcode 停止应用程序并直接从 phone 启动应用程序。该应用程序出现,但连接图标没有变化并保持红色,也没有显示相机画面。
当我将日志流式传输到 Mac 的控制台时,我可以看到与产品的连接就在那里。
这会是什么?非常感谢任何帮助。
编辑 1
这是我的 Podfile
platform :ios, '11.0'
target 'DjiMobileSdkTest2' do
use_frameworks!
pod 'iOS-Color-Picker'
pod 'DJI-UXSDK-iOS-Beta', '0.4.1'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |configuration|
target.build_settings(configuration.name)['ARCHS'] = '$(ARCHS_STANDARD)'
end
end
end
编辑 2
另一个额外的观察:我从 phone 中完全删除了该应用程序,然后从 Xcode 中重新启动它,我确认对话框要求允许使用蓝牙和位置,这是我想要的.然后该应用程序将再次没有反应并保持其初始状态。只有当我通过 Xcode 重新启动时,连接图标才会变绿并且显示视频脚。
编辑 3
我观察到:重新启动我的 phone 然后通过点击应用程序图标直接启动应用程序一切正常:应用程序启动,连接到无人机,连接图标变为绿色,并且相机提要也正在更新。我能够重复几次。
终于成功了!
如 this post 中所述,我不得不将 AppDelegate
及其 didFinishLaunchingWithOptions
生命周期重新引入我的应用程序。
这是之前的样子:
@main
struct MyApp: App {
var someClass = SomeClass()
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { (newScenePhase) in
switch newScenePhase {
case .active:
print("active")
someClass.doSomethingThatWouldNotWorkBefore()
case .background:
print("background")
case .inactive:
print("inactive")
@unknown default:
print("default")
}
}
}
}
这就是它的样子:
import SwiftUI
class AppDelegate: UIResponder, UIApplicationDelegate {
var someClass = SomeClass()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
someClass.doSomethingThatWouldNotWorkBefore()
return true
}
}
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { (newScenePhase) in
switch newScenePhase {
case .active:
print("active")
case .background:
print("background")
case .inactive:
print("inactive")
@unknown default:
print("default")
}
}
}
}
就这么简单 ;-)
我仍在努力找出确切的区别是什么,以及它与应用程序启动和生命周期、内存管理和“那些东西”有何关系。