I have a class that uses DeviceActivityMonitor
as a subclass:
// MyMonitor.swift
import DeviceActivity
@objc(MyMonitor)
class MyMonitor: DeviceActivityMonitor {
...
}
In order to use SwiftUI in Objective-C, I'm importing this in my objective-c file #import "MyProject-Swift.h"
.
But, when I try to build, I get the error Module 'DeviceActivity' not found
from wthin #import "MyProject-Swift.h"
I've tried "Link Binary with Libraries":
I've also tried to turn on Define Modules
in Build Settings -> Packaging
.
It just seems as though the DeviceActivity
framework doesn't have a header file. Seems like it should?
When I click "Show in Finder" in MyProject -> Frameworks -> DeviceAcitivity.framework
, there is not DeviceActivity.h
file in that dir (also not in the modules folder in my screenshot):
Is there a way to create a header file for an apple framework? Or force the creation of one?
I have also tried cleaning the build folder
Here's the framework for reference: https://developer.apple.com/documentation/DeviceActivity?changes=latest_major
Let me know if there's anything else I can describe to help. I'm new to this!
The issue here is that, in most languages, including Objective-C, a class’s super class is part of the class’s public API. In your example the MyMonitor
class has a public use of DeviceActivityMonitor
, which means that Swift has to include the framework that exports that class, DeviceActivity, in the generated MyTestProject-Swift.h
header. However, DeviceActivity is a Swift-only framework, and so everything falls apart.
One way around this is to add another layer of wrapping:
import Foundation
import DeviceActivity
@objc(ObjCMonitor)
class ObjCMonitor: NSObject {
override init() {
self.sm = SwiftMonitor()
}
private let sm: SwiftMonitor
}
private class SwiftMonitor: DeviceActivityMonitor {
}
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"