0

I have a main app view controller where a image is set and fetched using user defaults as given below:

animalcat = ["s1.jpg","s2.jpg","s3.jpg","s4.jpg"] 
var appGroupDefaults1 = UserDefaults.standard appGroupDefaults1 = UserDefaults(suiteName:"group.testKeyboard.Emo")!
appGroupDefaults1.set(animalcat, forKey: "imageValue")
appGroupDefaults1.synchronize()

This image has to fetched in keyboard extension class file, but the image is showing nil.

In keyboard extension-

let dataArray1 = appGroupDefaults1.object(forKey: "imageValue") print(dataArray1)

How to fetch the image from main app and show it in keyboard extension so that it is displayed when the keyboard is loaded?

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
Saranya
  • 595
  • 2
  • 7
  • 19

2 Answers2

0

To enable data sharing, use Xcode or the Developer portal to enable app groups for the containing app and its contained app extensions.

Read : Sharing Data with Your Containing App.

Here are some important steps need to be followed,

Step 1 : All images should be shared between Extension/ContainingApp, using bundle. Make sure bundle is added to both targets.

Step 2 : Once all images are shared between both Extension/ContainingApp you can get all images in both targets.

Step 3 : Make sure app group is created according to Sharing Data with Your Containing App.

If you follow above steps properly, you should be having image in both Extension/ContainingApp.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
0

Please Check have the following steps: 1. To share the image from app to extention you need to have the app group enabled and set up correctly. Now save all the images in the sharedContainer you can see following code.

let defaults = UserDefaults.init(suiteName: kAppGroupIdentifier)

        var arrDefault = [String]()
        var arrImages = [String]()

        if (defaults?.object(forKey: "arrImg") != nil) {
            arrDefault =  defaults?.value(forKey: "arrImg") as! [String]
            print("Array : \(String(describing: defaults?.object(forKey: "arrImg")))")

            arrDefault.append(imgName)
        }
        else{
            arrDefault.append(imgName)
        }

        arrImages.removeAll()
        arrImages.append(contentsOf: arrDefault)
        defaults?.set(arrImages, forKey: "arrImg")

and fetch it like this... 2. let defaults = UserDefaults.init(suiteName: kAppGroupIdentifier)

        var arrTemp = [String]()

    if (defaults?.object(forKey: "arrImg") != nil) {
        arrTemp =  defaults?.value(forKey: "arrImg") as! [String]
        print("Array of Image Name : \(String(describing: arrTemp))")}

here kAppGroupIdentifier is your appgroup identifier you have to use this while saving and retriving data from shared container. "arrImg" is the key that I m fetching to get my images array.

Bhoomika
  • 26
  • 4
  • let saved = arrTemp[0] let value = UIImage(named: saved) all working till here but getting nil here at extension app when I want to use this array for image – Saranya Aug 17 '17 at 06:34
  • are you getting anything in variable saved?? here arrTemp is arr of images fetch the image like this let imgURL = sharedContainerURL.path.appending("/\(imgName)") let url = URL(fileURLWithPath: imgURL) print("Share Container Url : \(url)") – Bhoomika Aug 17 '17 at 06:47
  • print(saved) value is s2.jpg ..but when I am taking it image It returns nil.SharedContainer url is unresolved identifier – Saranya Aug 17 '17 at 06:55
  • sharedContainerURL is FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: kAppGroupIdentifier)! – Bhoomika Aug 17 '17 at 07:29
  • let sharedContainerURL = FileManager.default.containerURL(SecurityApplcationGroup‌, Identifier: "group.testKeyboard.Emo")! not working – Saranya Aug 17 '17 at 07:44
  • var fileManager = FileManager.default var storeUrl = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.testKeyboard.Emo") print("\(storeUrl)") let imgURL = storeUrl?.path.appending("/(s1)") this is also not working print(imgURL) let url = URL(fileURLWithPath: imgURL!) print("Share Container Url : (url)") – Saranya Aug 17 '17 at 07:57
  • Yes I saw your code, debug the code, check the image exist at the sharedContainer path or not.. – Bhoomika Aug 17 '17 at 10:36
  • url value is not proper – Saranya Aug 17 '17 at 10:43
  • let path = "/Users/aradhya/Desktop/testKeyboard/testKeyboard/s1.jpg" image path from host app is this – Saranya Aug 17 '17 at 10:47
  • @saranya- Your path is incorrect.. this is not the sharedContainer path. that is y u r not getting the path. Check Your appGroup is correctly installed or not, then check the shared container path. – Bhoomika Aug 17 '17 at 11:35
  • have to convert image url to image data for getting exact image? – Saranya Aug 17 '17 at 11:43
  • let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.testKeyboard.Emo") let imgURL1 = sharedContainerURL?.path.appending("/(s4.jpg)") let url2 = URL(fileURLWithPath: imgURL1!) print("Share Container Url : (url2)") what is wrong in this – Saranya Aug 17 '17 at 12:00
  • result of Url is -file:///Users/aradhya/Library/Developer/CoreSimulator/Devices/B928FE0C-9767-4696-A57B-B26F77AABEC3/data/Containers/Shared/AppGroup/AE55696F-BE28-4148-9135-6A289BC83A67/s4.jpg – Saranya Aug 17 '17 at 12:09
  • I am not able to set that image to image view,I need array of image for custom keyboard – Saranya Aug 18 '17 at 05:14