Lottie: Can't get Animation with name

Viewed 4676

I'm using Lottie 2.1.3, XCode 9 and IOS 11.

When i'm trying to get animation like this

var loadingView = LOTAnimationView(name: "preloader")

I'm taking this error: +[LOTComposition animationNamed:inBundle:]: Animation Not Found

But getting the animation like below works fine

var loadingView = LOTAnimationView(filePath: "/Users/username/Git/iOS/Swift/LottieTest/LottieTest/preloader.json")

Here is the preloader.json i'm using https://www.lottiefiles.com/storage/datafiles/Hhw0wgYmETDTkxW/data.json

What am i doing wrong here?

3 Answers

Quite interesting, but don't you wanna use the other init methods such as using json file?

I've checked out Lottie's documentation and I can't seem to find the explanation behind this initializing function LOTAnimationView(name: "name"). As we can see in Lottie's example, the LottieLogo.json file has a different data compared the json file you've presented in your question, as well as to my own json file in my project.

Nevertheless, just add your json file into your project and read it and use this init function of Lottie --> LOTAnimationView(json: jsonObject)

I made a function for reading a json file in my small project called GPKit https://github.com/glennposadas/gpkit-ios :D

public class GPJSONReader {

    /** Get the whole JSON object from a file
     *  @returns an optional dictionary [String: Any]?
     */

    public class func readJson(fileName: String, withBlock completion: ([String : Any]?) -> Void) {
        do {
            if let file = Bundle.main.url(forResource: fileName, withExtension: "json") {
                let data = try Data(contentsOf: file)
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                if let object = json as? [String : Any] {
                    GPLog(classSender: self, log: "JSON found as dictionary")
                    completion(object)
                } else {
                    GPLog(classSender: self, log: "JSON is invalid")
                    completion(nil)
                }
            } else {
                GPLog(classSender: self, log: "No file found!")
                completion(nil)
            }
        } catch {
            GPLog(classSender: self, log: error.localizedDescription)
            completion(nil)
        }
    }
}

Then using the function above for Lottie like so:

// Animate here...
GPJSONReader.readJson(fileName: "connecting", withBlock: { (jsonObject) in
    if let jsonObject = jsonObject {
        self.animationView = LOTAnimationView(json: jsonObject)
        self.animationView.loopAnimation = true
        self.animationView.play()
    }
})

I solved my problem. Appereantly that was my fault. In order to use LOTAnimationView(name: "name") init method you have to do the following...

In your XCode project, click on your preLoader.json file, then in the inspector (Right panel of Xcode ) click on the checkbox with the name of your projet in Target MemberShip

Related