Access UNNotificationAttachment from watchOS

Hi Guys

When my app receive a push notification I use a UNNotificationServiceExtension to modify the notification content, download image from an url and add it as notification attachment

From iOS i can access to the attachment from this code


UNNotificationAttachment *attachment =content.attachments.firstObject; 
if (attachment!=nil && [attachment.identifier isEqualToString:@"thumbFileURL"]) {
      if ([attachment.URL startAccessingSecurityScopedResource]) { 
          self.immagine.image=[UIImage imageWithContentsOfFile:attachment.URL.path]; 
          [attachment.URL stopAccessingSecurityScopedResource ]; 
          } 
     }


But on watchOS when i try to execute the same code the

startAccessingSecurityScopedResource

doesn't start and i can't use the notification attachment !


UNNotificationAttachment * attachment =content.attachments.firstObject; 
NSLog(@"attachment\n%@", attachment); 

if (attachment!=nil && [attachment.identifier isEqualToString:@"thumbFileURL"]) { 
     if ([attachment.URL startAccessingSecurityScopedResource]) {
           NSLog(@"can access to the attachment"); 
          UIImage*attachmentImage=[UIImage imageWithContentsOfFile: attachment.URL.path]];
           dispatch_async(dispatch_get_main_queue(), ^{ 
               [self.immagineArticolo setImage:attachmentImage]; 
               }); 
          [attachment.URL stopAccessingSecurityScopedResource ]; }
      else{ 
          NSLog(@"can't access to the attachment"); 
          }
      }    


If i log the attachment i can read correctly all data (identifier,url,path,options etc etc), but i don't know how to display the image in the attachment !

Thank you in advanced and Happy Holiday !


P.S.: excuse me for my terrible english


Vanni

Any idea for this problem ?

Thank you so much !!


Vanni

I've encountered the same problem and filed a Radar. The radar has been marked as a duplicate of another one that has been closed.

I am also facing same problem. did any one found solution.

Thnks.

Hi ,

did you find the solution. Please help me I am also facing the same problem.

Thanks,

//dispatch_async(dispatch_get_main_queue(), ^{ 
               [self.immagineArticolo setImage:attachmentImage]; 
         //      });


If you comment out the dispatch_async it works. I guess the file is being loaded lazily into UIImage so that when it is actually loaded it is outside of the security scope.


But I still have issues as sometimes my watch notifications do not have all the attachments that are sent, though the same iOS noticification has all attachments. So is pop luck if the image attachment is there or not.


watchOS4.2


Gerard.

Access UNNotificationAttachment from watchOS
 
 
Q