英文:
Creating an iMessage app without using a storyboard
问题
I would like to create an iMessage app without using a storyboard, and preferably using SwiftUI for the view. I am following an answer in an Apple forum, but I am not getting a view inside the iMessage window.
I changed my Info.plist
to:
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.message-payload-provider</string>
<key>NSExtensionPrincipalClass</key>
<string>MessagesViewController.swift</string>
</dict>
A simple view controller:
import Messages
import SwiftUI
@objc (MessagesViewController)
class MessagesViewController: MSMessagesAppViewController {
override func viewDidLoad() {
super.viewDidLoad()
let child = UIHostingController(rootView: SwiftUIView())
child.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(child.view)
NSLayoutConstraint.activate([
child.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0),
child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0),
child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0),
child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0)
])
}
}
And finally, a simple SwiftUI view:
struct SwiftUIView: View {
var body: some View {
Text("Hello, Earth!")
}
}
So far, no luck. Does anyone have an idea where I've gone wrong?
英文:
I would like to create an iMessage app without using a storyboard, and preferably using SwiftUI for the view. I am following an answer in an Apple forum, but I am not getting a view inside the iMessage window.
I changed my Info.plist
to:
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.message-payload-provider</string>
<key>NSExtensionPrincipalClass</key>
<string>MessagesViewController.swift</string>
</dict>
A simple view controller:
import Messages
import SwiftUI
@objc (MessagesViewController)
class MessagesViewController: MSMessagesAppViewController {
override func viewDidLoad() {
super.viewDidLoad()
let child = UIHostingController(rootView: SwiftUIView())
child.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(child.view)
NSLayoutConstraint.activate([
child.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0),
child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0),
child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0),
child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0)
])
}
And finally a simple SwiftUI view:
struct SwiftUIView: View {
var body: some View {
Text("Hello, Earth!")
}
}
So far, no luck. Does anyone have an idea where I've gone wrong?
答案1
得分: 1
你的问题在于你的 Info.plist 文件中。你已经指定了 MessagesViewController.swift
作为 NSExtensionPrincipalClass
。
这是源文件名称,而不是类名称。你需要指定类名(即 MessagesViewController
),前缀是你的模块名称。
将你的 Info.plist
更改为:
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.message-payload-provider</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).MessagesViewController</string>
</dict>
英文:
Your problem is in your Info.plist file. You have specified MessagesViewController.swift
as the NSExtensionPrincipalClass
This is the source file name, not the class name. You need to specify the class name (which is MessagesViewController
) prefixed with your module name.
Change your Info.plist
to
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.message-payload-provider</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).MessagesViewController</string>
</dict>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论