Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share sqlite db into share extension into iPhone app

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.

like image 752
Kishan Vyas Avatar asked Dec 04 '25 14:12

Kishan Vyas


1 Answers

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
like image 181
MAhipal Singh Avatar answered Dec 07 '25 11:12

MAhipal Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!