Pandey_Laxman Pandey_Laxman - 1 year ago 255
iOS Question

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:@""];

And without any issue whenever user tapping on emojis icon all the custom emojis shows in grid in place of keyboard by 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. 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];

- (NSString *)getFullPath:(NSString *)file {

NSURL* shareContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@""];
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.

*Update = * File is getting deleted after showing in push notification.

Answer Source

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."

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

                NSLog(@"Attachement Error = %@",error);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download