Creating an iMessage app without using a storyboard

huangapple go评论61阅读模式
英文:

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:

&lt;key&gt;NSExtension&lt;/key&gt;
	&lt;dict&gt;
		&lt;key&gt;NSExtensionPointIdentifier&lt;/key&gt;
		&lt;string&gt;com.apple.message-payload-provider&lt;/string&gt;
		&lt;key&gt;NSExtensionPrincipalClass&lt;/key&gt;
		&lt;string&gt;MessagesViewController.swift&lt;/string&gt;
	&lt;/dict&gt;

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(&quot;Hello, Earth!&quot;)
    }
}

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

&lt;key&gt;NSExtension&lt;/key&gt;
    &lt;dict&gt;
        &lt;key&gt;NSExtensionPointIdentifier&lt;/key&gt;
        &lt;string&gt;com.apple.message-payload-provider&lt;/string&gt;
        &lt;key&gt;NSExtensionPrincipalClass&lt;/key&gt;
        &lt;string&gt;$(PRODUCT_MODULE_NAME).MessagesViewController&lt;/string&gt;
    &lt;/dict&gt;

huangapple
  • 本文由 发表于 2023年5月14日 07:35:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定