Hot questions for Using Lottie in xcode

Question:

the setup

I have an iOS App MY-App which uses my own framework MY-Framework. Both are written in swift. The app only handles user authentification and passes an access token to MY-Framework. MY-Framework then handles the entire ScreenFlow and business logic. The goal is to distribute the MY-Framework to customers to use it in their apps.

a minimal sample of the project setup exhibiting this problem is available here: https://github.com/vprimachenko/lottie-pod-problem-sample

now I was to enhance my framework provided views with some animations and were to use lottie for it. i am using cocoapods version1.6.0-pre

Naïve attempt

i created a Podfile with following content

target 'fw' do
  pod 'lottie-ios'
end

which resulted in a compile error in the framework

./fw/fw/File.swift:4:8: error: no such module 'Lottie'
import Lottie
       ^
frameworks

after some googling i changed my Podfile to:

target 'fw' do
  use_frameworks!
  pod 'lottie-ios'
end

result: Runtime crash

dyld: Library not loaded: @rpath/Lottie.framework/Lottie
  Referenced from: .../Build/Products/Debug-iphonesimulator/fw.framework/fw
  Reason: image not found
modular headers maybe?

cocoapods release notes mention use_modular_headers!, lets try that:

target 'fw' do
 use_modular_headers!
 pod 'lottie-ios'
end

result: compiler error in the containing app

./app/app/ViewController.swift:3:8: error: missing required module 'Lottie'
import fw
      ^
maybe both?
target 'fw' do
 use_modular_headers!
 use_frameworks!
 pod 'lottie-ios'
end

result: runtime crash

dyld: Library not loaded: @rpath/Lottie.framework/Lottie
 Referenced from: .../Build/Products/Debug-iphonesimulator/fw.framework/fw
 Reason: image not found
the hack

after some trying around I was able to solve the situation by providing my own bringing header for Lottie but this feels more like a duct-tape rather than a proper solution. I will still post this later as an additional answer.

My Question

how do i use properly integrate lottie-ios cocoapod in such a way that it is completely contained in MY-Framework, so when i share it to a customer they can just drop it into their App and not worry about any dependencies?

Releasing it as a private pod with dependencies is sadly not an option.


Answer:

The common way to use objective-c code in swift is using bridging headers, take a look at this:

Importing Objective-C into Swift

you need to create a bridging header and add it to your project then inside the .h file you created simply add :

#import <Lottie/Lottie.h>

Question:

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?


Answer:

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()
    }
})

Question:

I'm new to Lottie library and trying to integrate a simple animation. I have my animated json at folder Resources->Json->LottieAnimations->checkMark.json

I have installed Lottie via pod install and its current version is 3.1.0. In my storyboard I have added a view and gave the class name as AnimationView and in the interface builder gave the Animation Name as "checkMark".

This is the outlet

@IBOutlet weak var checkMarkAnimationView: AnimationView!

And on viewDidLoad I just call

checkMarkAnimationView.play()

But nothing happens here. Am I missing something? Please advice


Answer:

You need to do something like this (if in the code):

 let view = AnimationView()
    let path = Bundle.main.path(forResource: "checkMark",
                                ofType: "json") ?? ""
    view.animation = Animation.filepath(path)
    self.view.addSubview(view)
    view.play()

if doesn't work, please check animation name or JSON file

Updated

also check is JSON file added in Bundle resources (Build phases -> Copy Bundle Resources)

Question:

I am using a UIObject to display an animation using Lottie framework which is a submit button, and when this UIView object is tapped, the animation is played.

However the text within this animation does not appear, and the dimensions are also wrong. My UIView object has dimensions of 193*33 and the animation also does. Here is my code;

 let SubmitButton = LOTAnimationView(name: "submit_195_33")
 self.SubmitViewName.addSubview(SubmitButton)
 SubmitButton.frame = CGRect(x: 0, y: 0, width: 195, height: 33)
 SubmitButton.contentMode = .scaleAspectFill
 SubmitButton.play()

My UIView object is larger then the animation even if I use this code, and the animation is super small like this:


Answer:

Maybe you can try setting constraints to the button, for example:

submitButton.translatesAutoresizingMaskIntoConstraints = false
submitButton.widthAnchor.constraint(equalToConstant: 195).isActive = true
submitButton.heightAnchor.constraint(equalToConstant: 33).isActive = true