OGeek|极客世界-中国程序员成长平台

标题: ios - 将 sqlite db 共享到 iPhone 应用程序的共享扩展中 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:20
标题: ios - 将 sqlite db 共享到 iPhone 应用程序的共享扩展中

我想将共享扩展程序集成到我的 iPhone 应用程序中。应用程序包含 sqlite 数据库,其中包含一些电话簿联系人。我在应用程序中添加了共享扩展,并且扩展在共享事件 View Controller 中也是可见的。但问题是我无法访问主应用程序 sqlite 数据库。当我在扩展中记录数据库路径时,它显示的路径与主应用程序 sqlite 数据库路径不同。所以请帮助我了解如何访问主应用程序数据库以从 sqlite 获取数据,以便我可以将它们显示到共享扩展自定义 View Controller 中,并且还想将新数据插入数据库中。



Best Answer-推荐答案


在主应用和共享扩展之间共享 UserDefault 和 Sqlite:

在功能中添加“应用组”,以便将主应用中的数据传输到扩展

let fileManager = FileManager.default 
let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xxx.xxx")
return directory!.appendingPathComponent(fileName).path
    target 'Main_App' do
    # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
    use_frameworks!
    
    target 'Main_App_extension' do
     use_frameworks! 
      inherit! :search_paths
      pod 'FMDB’
      pod 'Kingfisher'
    
    end
    end

关于ios - 将 sqlite db 共享到 iPhone 应用程序的共享扩展中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45251714/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4