簡體   English   中英

無法從iOS 10中的App組共享容器中獲取擴展程序中的圖像

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

在我的主機應用程序中,我正在通過以下網址解壓縮成功后下載自定義表情符號圖像文件夾。

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

用戶點擊emojis圖標時沒有任何問題,所有自定義表情符號都通過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];

} 

我總是得到有效的網址,但第二次我得到的圖像為零,但每次拍攝的圖像都是第一次。 無法得到根本原因。 任何幫助將不勝感激。

以下是每張圖像第二次出現的錯誤。

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}

另請告訴我如何查看App Group共享容器,找不到查看其中包含的文件的方法。

*更新= *文件在推送通知中顯示后被刪除。

來自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);
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM