应用扩展程序如何访问包含应用程序文档/文件夹中的文件

保罗·史密斯

在应用扩展程序中,是否有一种方法可以获取从包含在/ var / mobile / Containers / Data / Application // Documents //文件夹中的包含应用程序生成的图像?

音系的

为了使文件可用于应用扩展,您必须使用Group Path,因为应用扩展无法访问应用的文档文件夹,因此您必须执行以下步骤,

  1. 项目设置->功能中启用应用程序组
  2. 添加一个群组扩展名,例如group.yourappid
  3. 然后使用以下代码。

    NSString *docPath=[self groupPath];
    
    NSArray *contents=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:nil];
    
    NSMutableArray *images=[[NSMutableArray alloc] init];
    
        for(NSString *file in contents){
            if([[file pathExtension] isEqualToString:@"png"]){
                [images addObject:[docPath stringByAppendingPathComponent:file]];
            }
        }
    
    
    -(NSString *)groupPath{
         NSString *appGroupDirectoryPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:group.yourappid].path;
    
        return appGroupDirectoryPath;
    }
    

您可以根据生成的图像扩展名添加或更改路径扩展名。

注意-请记住,您需要在组文件夹中而不是在文档文件夹中生成图像,因此该图像可用于应用程序和扩展程序。

干杯。

Swift 3更新

let fileManager = FileManager.default
let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "YOUR_GROUP_ID")?.appendingPathComponent("logo.png")

// Write to Group Container            
if !fileManager.fileExists(atPath: url.path) {

    let image = UIImage(named: "name")
    let imageData = UIImagePNGRepresentation(image!)
    fileManager.createFile(atPath: url.path as String, contents: imageData, attributes: nil)
}

// Read from Group Container - (PushNotification attachment example)              
// Add the attachment from group directory to the notification content                    
if let attachment = try? UNNotificationAttachment(identifier: "", url: url!) {

    bestAttemptContent.attachments = [attachment]

    // Serve the notification content
    self.contentHandler!(self.bestAttemptContent!)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从IOS上的UIView将图像保存到应用程序文档文件夹

播放iOS中从应用程序文档文件夹下载的Widevine Classic文件?

WatchKit:Swift-如何获取数据并将其放入iPhone应用程序文档文件夹中的sqlite数据库中

文件应用程序和我的应用程序的文档文件夹之间的交互

如何访问应用程序文件夹中的图像

在 CodeIgniter 中,如何使用 site_url 访问应用程序文件夹之外的文件夹

打开应用程序文件夹中的文件

如何在GNOME应用程序菜单中创建应用程序文件夹?

斯威夫特:如何确定应用程序文件夹?

无法访问Grails 3中的Web应用程序文件夹文件

如何允许应用程序用户选择可以在应用程序文档中保存的文件夹位置

在Ubuntu中查看包含应用程序文件夹的命令

无法在Codeigniter中访问应用程序文件夹的内容

如何将文件保存到电子js中的应用程序文件夹

如何在UWP应用程序中的应用程序文件夹之外访问文件(如SQLite数据库)?

如何从应用程序视图中删除空的应用程序文件夹

如何将位图保存到Android中的应用程序文件夹

如何更改 VSCode 中的设置和应用程序文件夹

如何设置图像驻留在应用程序文件夹中的Image Control的源

PHP:如何在.htaccess中隐藏我的应用程序文件夹

如何播放应用程序文件夹中的声音?

如何永久禁用GNOME菜单中的应用程序文件夹?

如何在 Yii2 Advanced 中添加新的应用程序文件夹

如何从 docker 镜像中获取应用程序文件夹

在codeigniter中,如何通过将资产文件夹放在应用程序文件夹中使其工作?

如何访问Docker容器中的应用程序文件?

是否可以在Play之外访问文件!应用程序文件夹?

React Redux应用程序文件夹结构

Android Studio创建应用程序文件夹