3

I want to integrate share extension into my iPhone app. App contain sqlite database which contains some of the phone book contacts. 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. When I log the database path in extension it show different path than main application sqlite database path. 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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kishan Vyas
  • 327
  • 1
  • 2
  • 15
  • hii @Vyas Kishan, i'm facing same problem .. you got any solution for this? – MAhipal Singh Nov 18 '17 at 09:33
  • 1
    @V12, I found alternate solution for this. Use App Groups for this. Store all the information into it when user open share extension and when user open main app retrieve information from app group and store into sqlite db. This is the only solution for this. – Kishan Vyas Nov 19 '17 at 12:02
  • got the solution :) i'll send you the step that i followed. – MAhipal Singh Nov 20 '17 at 12:04

2 Answers2

1

Share UserDefault and Sqlite Between Main App and Share Extension:

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"];

  • if you copied sqlite file in document directory. 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
  • if you need to import any pod in extension then edit pod file
    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
Community
  • 1
  • 1
MAhipal Singh
  • 4,745
  • 1
  • 42
  • 57
0

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:

   let ShareableDb = NSData(contentsOfFile: checkPath())

Thats all you need to share db file. Hope it will be helpful for you.

Ibrar
  • 117
  • 1
  • 9
  • I get the different path for app group and main app. -- Db path from main app /Users/dnk/Library/Developer/CoreSimulator/Devices/3AEA77CB-EDC4-4484-9FA4-BAA92B98D277/data/Containers/Data/Application/9B9A59C1-74A4-4BF8-9E99-40A4660E0ED4/Documents/DB.sqlite -- Db path from app group /Users/dnk/Library/Developer/CoreSimulator/Devices/3AEA77CB-EDC4-4484-9FA4-BAA92B98D277/data/Containers/Data/PluginKitPlugin/202A137C-D65B-4ECD-9BE2-5FC15135ABA4/Documents/DB.sqlite – Kishan Vyas Jul 29 '17 at 05:32