Hi All,
In iOS, AppExtension and its containing application and groups of application use App Groups capability to share data between them. For example if keyboard extension app want to share data with its containing application.
Using app group we have a single folder for group of app so that they can exchange information whenever application need to.To save the data to app groups we use this function.
func saveImage(image:UIImage,nameOfFile:String) {
let data = UIImagePNGRepresentation(image)
let groupURL : URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "Group name")!
var srt:NSString = groupURL.absoluteString as NSString
srt = (srt as NSString).substring(from: 7) as NSString
let filename = (srt as String) + nameOfFile + ".png"
let boo = (try? data!.write(to: URL(fileURLWithPath: filename), options: [.atomic])) != nil
}
when you will call this function then you have to pass image and name of file to be saved in shared folder.
To fetch data from app group , you can use below function :-
func getImage(nameOfFile:String) -> UIImage{
let groupURL : URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "GROUP NAME")!
var srt:NSString = groupURL.absoluteString as NSString
srt = (srt as NSString).substring(from: 7) as NSString
let filename = (srt as String) + nameOfFile + ".png"
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filename) {
print("FILE AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
let data:Data = UIImagePNGRepresentation(UIImage(contentsOfFile: filename)!)!
return UIImage(data:data)!
}
0 Comment(s)