ios - 在通知内容扩展中以编程方式调用 VC 时无法插入 COPY_SEND

标签 ios swift usernotifications

创建通知内容扩展然后删除 NSExtensionMainStoryboard 并将其替换为 info.plist 文件中的 NSExtensionPrincipalClass 并适本地调用 module.viewController 然后调试扩展我看到以下错误吗?

NotificationExtension[58062:21665141] [常见] _BSMachError: port 5c03; (os/kern) 无效能力 (0x14) “无法插入 COPY_SEND”

下面是 NotificationExtensionViewControllerCode

import UIKit
import UserNotifications
import UserNotificationsUI

class NotificationViewController: UIViewController, UNNotificationContentExtension {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any required interface initialization here.
    }

    func didReceive(_ notification: UNNotification) {

    }

}

是什么导致了错误,我该如何解决?

如果您执行上述操作但使用 NSExtensionMainStoryboard 调用 Storyboard 但删除标签,您会得到同样的错误!

提前致谢。

最佳答案

您需要在附件 url 上调用 startAccessingSecurityScopedResource()(在 didReceive 中)。 Apple 的一些天才认为,通知扩展仅访问通知存储 中的文件是危险的。当然,它有据可查(不是!)

if let attachment = notification.request.content.attachments.first {
    if attachment.url.startAccessingSecurityScopedResource() {
        if let data = NSData(contentsOfFile: attachment.url.path) as? Data {
            self.uiImage.image = UIImage(data: data) // <- that's the image view
            attachment.url.stopAccessingSecurityScopedResource()
        }
    }
}

关于ios - 在通知内容扩展中以编程方式调用 VC 时无法插入 COPY_SEND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52788795/

相关文章:

iphone - 预定的 UILocalNotification 数

ios - 使用解码协议(protocol)解析 Json (Codable)

iOS 10 : How to show incoming VOIP call notification when app is in background?

ios - 如何在 ios 中绘制具有多种颜色的动画路径?

ios - 选择多个图像(UIImagePickerController 或 Photos.app 共享 UI)

objective-c - objective-c :无法获取网络状态值

swift - Swift-使用多个条件对对象数组进行排序

ios - UITextfield 委托(delegate)响应所有文本字段

ios - UNUserNotificationCenterDelegate 方法未被调用 Xcode 8 iOS 10

iOS 13 : Announce messages with Siri