简体   繁体   English

无法从iOS 10中的App组共享容器中获取扩展程序中的图像

[英]Not able to fetch images in extension app from App group shared container in iOS 10

In my host App I am downloading custom emojis images folder after unzipping successfully saving by below url. 在我的主机应用程序中,我正在通过以下网址解压缩成功后下载自定义表情符号图像文件夹。

NSURL* shareContainerURL =   [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.company.app.PushServiceExtn"];

And without any issue whenever user tapping on emojis icon all the custom emojis shows in grid in place of keyboard by shareContainerURL. 用户点击emojis图标时没有任何问题,所有自定义表情符号都通过shareContainerURL显示在网格中代替键盘。

I have created PushNotification Service Extension where I need to show the custom emojis image by fetching emoji name from payload whenever push comes. 我已经创建了PushNotification服务扩展,我需要通过在推送到来时从有效负载中获取表情符号名称来显示自定义表情符号图像。 using below code. 使用下面的代码。

- (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];

} 

I am always getting valid url but second time I get image nil but first time of every image it works. 我总是得到有效的网址,但第二次我得到的图像为零,但每次拍摄的图像都是第一次。 Couldn't get the root cause. 无法得到根本原因。 Any help would appreciated. 任何帮助将不胜感激。

Below is the error that occurred second time for every image. 以下是每张图像第二次出现的错误。

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}

Also please let me know how to view App Group shared container, Couldn't find way to view the files contained inside. 另请告诉我如何查看App Group共享容器,找不到查看其中包含的文件的方法。

*Update = * File is getting deleted after showing in push notification. *更新= *文件在推送通知中显示后被删除。

From apple " UNNotificationAttachment Once validated, attached files are moved into the attachment data store so that they can be accessed by the appropriate processes. Attachments located inside an app's bundle are copied instead of moved." 来自apple“ UNNotificationAttachment一旦验证,附加文件就会被移动到附件数据存储中,以便可以通过适当的进程访问它们。位于应用程序包中的附件将被复制而不是移动。”

So I copy my emoji image to duplicate URL and assign it to 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);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从应用程序组iOS中的共享容器读取 - Not able to read from shared containor in app group ios 如何创建/使用共享应用程序组容器作为包含应用程序及其在ios中的扩展名之间的缓存存储 - How to create/use a shared app group container as cache storage between containing app and it's extension in ios 从XCode下载共享的应用程序组容器 - Download shared App Group Container from XCode iMessage扩展程序是否需要iOS10中的容器(父)应用程序? - Does iMessage extension need container (Parent) app in iOS10? 无论如何,可以通过iOS 8.3中的应用扩展打开容器应用吗? - Is there anyway of opening the container app from an app extension in iOS 8.3? 如何从Cordova访问应用程序组共享容器? - How to access app group shared container from Cordova? 容器上缩放的iOS应用图像 - scaled iOS app images on container 从扩展名写入存储在App Group容器中的数据库 - Write into a database stored in App Group container from extension 调试 iOS 应用时如何查看 App Group Shared Container 目录的内容? - How can I view the contents of the App Group Shared Container directory when debugging an iOS app? iOS Swift Today Extension:从容器应用程序导入类? - iOS Swift Today Extension: import class from container app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM