通知服务扩展文件夹中文件(图像)的路径

Path to file(image) in Notification Service Extension folder

我正在尝试将本地图像(图标)作为附件添加到 iOS 12 上的远程通知。换句话说,图像 URL 未在通知 userInfo 中传递,而是根据 userInfo 中的其他标准在内部确定。因此,我想要一个与应用程序捆绑在一起的图标库以供选择。如果我想将此类图像添加到通知中,我无法确定相对 URL 是什么。这就是我想要做的,“example.png”文件位于“通知服务扩展”文件夹中。

我想知道如何将图像文件夹从 xcode 复制到我要构建到的设备。

然后我想知道访问这些图像以用于附件的相对路径是什么。

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Title";
content.body = @"Body";
content.sound = [UNNotificationSound defaultSound];
NSURL *imageURL = [NSURL fileURLWithPath:@"example.png"];
NSError *error;
UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:imageURL options:nil error:&error];
if (error)
{
    NSLog(@"error while storing image attachment in notification: %@", error);
}
if (icon)
{
    content.attachments = @[icon];
}

只需将图像目录复制到您的 Xcode 项目中。 Xcode 将捆绑 并将其复制到设备。在设备上,执行以下操作(直接取自文档)

NSBundle *main = [NSBundle mainBundle];
NSString *resourcePath = [main pathForResource:@"Seagull" ofType:@"jpg"];

来自文档,但还有一个 URLForResource:withExtention 更好用。