Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • App Group capability of iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 356
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: