无法从 iOS 10 中的应用程序组共享容器中获取扩展应用程序中的图像
Not able to fetch images in extension app from App group shared container in iOS 10
在我的主机应用程序中,我正在下载自定义表情符号图像文件夹,在通过以下 url 成功解压缩后保存。
NSURL* shareContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.company.app.PushServiceExtn"];
只要用户点击表情符号图标,所有自定义表情符号都会通过 shareContainerURL 显示在网格中,而不是键盘上。
我创建了 PushNotification 服务扩展,我需要在推送到来时通过从负载中获取表情符号名称来显示自定义表情符号图像。使用下面的代码。
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
NSDictionary* mediaAttachment = [self.bestAttemptContent.userInfo objectForKey:@"media-attachment"];
NSString* attachType = [mediaAttachment objectForKey:@"attachType"];
if ([attachType isEqualToString:@"emoji"]) {
NSString* strEmojiURL = [mediaAttachment objectForKey:@"url"];
self.bestAttemptContent.title = strEmojiURL;
NSString* emojiName = [[strEmojiURL stringByRemovingPercentEncoding] lastPathComponent];
NSString* strUnpresseedEmojiPath = [self getFullPath:@"emoji/Pressed"];
NSString* strImagePath = [NSString stringWithFormat:@"%@/%@ Pressed.png",strUnpresseedEmojiPath, emojiName];
NSURL* fileURL = [NSURL fileURLWithPath:strImagePath];
NSData *imageData = [NSData dataWithContentsOfURL:fileURL];
UIImage *image = [UIImage imageWithData:imageData];
if (image) {
NSError* error;
// CGRect rect = CGRectMake(0,0,50,50);
// @{UNNotificationAttachmentOptionsThumbnailClippingRectKey:(__bridge NSDictionary*)CGRectCreateDictionaryRepresentation(rect)} option dict;
UNNotificationAttachment * attachement = [UNNotificationAttachment attachmentWithIdentifier:strImagePath.lastPathComponent URL:fileURL options:nil error:&error];
if (error == nil) {
self.bestAttemptContent.attachments = @[attachement];
}
}
}
self.contentHandler(self.bestAttemptContent);
}
- (NSString *)getFullPath:(NSString *)file {
NSURL* shareContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.company.app.PushServiceExtn"];
return [shareContainerURL.path stringByAppendingPathComponent: file];
}
我总是得到有效 url 但第二次我得到图像 nil 但第一次每张图像都有效。无法找到根本原因。任何帮助将不胜感激。
以下是每张图片第二次出现的错误。
2016-10-27 17:34:59.081026 pushNotificationServiceExtension[651:34632] Attachement Error = Error Domain=UNErrorDomain Code=100 "Invalid attachment file URL" UserInfo={NSLocalizedDescription=Invalid attachment file URL}
另外请告诉我如何查看应用程序组共享容器,无法找到查看其中包含的文件的方法。
*更新 = * 文件在推送通知中显示后被删除。
From apple "UNNotificationAttachment 一旦通过验证,附件将被移动到附件数据存储中,以便适当的进程可以访问它们。位于应用程序包内的附件将被复制而不是移动。
所以我复制我的表情符号图像以复制 URL 并将其分配给 UNNotificationAttachment。
if (imageFileURL) {
NSURL* duplicateImageURL = [self getFullPath:@"EmojiAttachment"];
if (![fileManager fileExistsAtPath:duplicateImageURL.path]) {
[fileManager createDirectoryAtPath:duplicateImageURL.path withIntermediateDirectories:NO attributes:nil error:&error];
}
emojiName = [NSString stringWithFormat:@"%@ Unpressed.png", emojiName];
duplicateImageURL = [duplicateImageURL URLByAppendingPathComponent:emojiName];
[[NSFileManager defaultManager]copyItemAtURL:imageFileURL toURL:duplicateImageURL error:&error];
UNNotificationAttachment * attachement = [UNNotificationAttachment attachmentWithIdentifier:emojiName URL:[duplicateImageURL filePathURL] options:nil error:&error];
if (error == nil) {
self.bestAttemptContent.attachments = @[attachement];
}
else{
NSLog(@"Attachement Error = %@",error);
}
}
在我的主机应用程序中,我正在下载自定义表情符号图像文件夹,在通过以下 url 成功解压缩后保存。
NSURL* shareContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.company.app.PushServiceExtn"];
只要用户点击表情符号图标,所有自定义表情符号都会通过 shareContainerURL 显示在网格中,而不是键盘上。
我创建了 PushNotification 服务扩展,我需要在推送到来时通过从负载中获取表情符号名称来显示自定义表情符号图像。使用下面的代码。
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
NSDictionary* mediaAttachment = [self.bestAttemptContent.userInfo objectForKey:@"media-attachment"];
NSString* attachType = [mediaAttachment objectForKey:@"attachType"];
if ([attachType isEqualToString:@"emoji"]) {
NSString* strEmojiURL = [mediaAttachment objectForKey:@"url"];
self.bestAttemptContent.title = strEmojiURL;
NSString* emojiName = [[strEmojiURL stringByRemovingPercentEncoding] lastPathComponent];
NSString* strUnpresseedEmojiPath = [self getFullPath:@"emoji/Pressed"];
NSString* strImagePath = [NSString stringWithFormat:@"%@/%@ Pressed.png",strUnpresseedEmojiPath, emojiName];
NSURL* fileURL = [NSURL fileURLWithPath:strImagePath];
NSData *imageData = [NSData dataWithContentsOfURL:fileURL];
UIImage *image = [UIImage imageWithData:imageData];
if (image) {
NSError* error;
// CGRect rect = CGRectMake(0,0,50,50);
// @{UNNotificationAttachmentOptionsThumbnailClippingRectKey:(__bridge NSDictionary*)CGRectCreateDictionaryRepresentation(rect)} option dict;
UNNotificationAttachment * attachement = [UNNotificationAttachment attachmentWithIdentifier:strImagePath.lastPathComponent URL:fileURL options:nil error:&error];
if (error == nil) {
self.bestAttemptContent.attachments = @[attachement];
}
}
}
self.contentHandler(self.bestAttemptContent);
}
- (NSString *)getFullPath:(NSString *)file {
NSURL* shareContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.company.app.PushServiceExtn"];
return [shareContainerURL.path stringByAppendingPathComponent: file];
}
我总是得到有效 url 但第二次我得到图像 nil 但第一次每张图像都有效。无法找到根本原因。任何帮助将不胜感激。
以下是每张图片第二次出现的错误。
2016-10-27 17:34:59.081026 pushNotificationServiceExtension[651:34632] Attachement Error = Error Domain=UNErrorDomain Code=100 "Invalid attachment file URL" UserInfo={NSLocalizedDescription=Invalid attachment file URL}
另外请告诉我如何查看应用程序组共享容器,无法找到查看其中包含的文件的方法。
*更新 = * 文件在推送通知中显示后被删除。
From apple "UNNotificationAttachment 一旦通过验证,附件将被移动到附件数据存储中,以便适当的进程可以访问它们。位于应用程序包内的附件将被复制而不是移动。
所以我复制我的表情符号图像以复制 URL 并将其分配给 UNNotificationAttachment。
if (imageFileURL) {
NSURL* duplicateImageURL = [self getFullPath:@"EmojiAttachment"];
if (![fileManager fileExistsAtPath:duplicateImageURL.path]) {
[fileManager createDirectoryAtPath:duplicateImageURL.path withIntermediateDirectories:NO attributes:nil error:&error];
}
emojiName = [NSString stringWithFormat:@"%@ Unpressed.png", emojiName];
duplicateImageURL = [duplicateImageURL URLByAppendingPathComponent:emojiName];
[[NSFileManager defaultManager]copyItemAtURL:imageFileURL toURL:duplicateImageURL error:&error];
UNNotificationAttachment * attachement = [UNNotificationAttachment attachmentWithIdentifier:emojiName URL:[duplicateImageURL filePathURL] options:nil error:&error];
if (error == nil) {
self.bestAttemptContent.attachments = @[attachement];
}
else{
NSLog(@"Attachement Error = %@",error);
}
}