简体   繁体   English

将sqlite db共享到iPhone应用程序的共享扩展中

[英]Share sqlite db into share extension into iPhone app

I want to integrate share extension into my iPhone app. 我想将共享扩展程序集成到我的iPhone应用程序中。 App contain sqlite database which contains some of the phone book contacts. 应用程序包含sqlite数据库,其中包含一些电话簿联系人。 I added share extension into app and extension is also visible into share activity view controller. 我在应用程序中添加了共享扩展,并且共享活动视图控制器中也可以看到扩展。 But the problem is that I can't access main app sqlite database. 但是问题是我无法访问主应用sqlite数据库。 When I log the database path in extension it show different path than main application sqlite database path. 当我以扩展名登录数据库路径时,它显示的路径与主应用程序sqlite数据库路径不同。 So please help me about how to access main application database to fetch data from sqlite so I can show them into share extension custom view controller and also want to insert new data into database. 因此,请帮助我有关如何访问主应用程序数据库以从sqlite获取数据的信息,以便可以将其显示到共享扩展自定义视图控制器中,并且还希望将新数据插入数据库中。

First you need to find the database path as: 首先,您需要找到数据库路径为:

  func checkPath()->String {
        let dbPath = ""
       let filemgr = FileManager.default
    let dirPaths =
        NSSearchPathForDirectoriesInDomains(.documentDirectory,
                                            .userDomainMask, true)

    let docsDir = dirPaths[0]

    let databasePath = docsDir.appending("/dbName.sqlite") as NSString

    if !filemgr.fileExists(atPath: databasePath as String) {


        dbPath = databasePath

    }
   return dbPath
   }

After getting path, you can now access shareable db file as: 获取路径后,您现在可以按以下方式访问可共享的db文件:

   let ShareableDb = NSData(contentsOfFile: checkPath())

Thats all you need to share db file. 那就是您共享数据库文件所需的全部。 Hope it will be helpful for you. 希望对您有帮助。

Share UserDefault and Sqlite Between Main App and Share Extension: 在主应用程序和共享扩展之间共享UserDefault和Sqlite:

Add "App Groups" in Capabilities for data transfer in Main app to Extension 在功能中添加“应用程序组” ,以便将主应用程序中的数据传输到扩展程序

  • To share the NSUserDefaults data: [NSUserDefaults standardUserDefaults] Replace with [[NSUserDefaults alloc] initWithSuiteName:@"group.com.XXX.XXX"]; 共享NSUserDefaults数据: [NSUserDefaults standardUserDefaults]替换为[[NSUserDefaults alloc] initWithSuiteName:@"group.com.XXX.XXX"];
  • if you copied sqlite file in document directory. 如果在文档目录中复制了sqlite文件。 So, change Document Directory path from 因此,从

    try! FileManager.default.url(for:.documentDirectory, in:.userDomainMask,appropriateFor: nil, create:false).appendingPathComponent(fileName)

to

let fileManager = FileManager.default 
let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xxx.xxx")
return directory!.appendingPathComponent(fileName).path
  • Select Target Members (both main project and Extension) for required classes 选择Target Members (主要项目和扩展)作为必修课程
  • if you need to import any pod in extension then edit pod file 如果您需要导入扩展中的任何Pod,请编辑Pod文件
  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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM