Skip to content

Commit

Permalink
Add JSON load error callback to ObjC Lottie URL initializer (#2324)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-adam-taylor committed Feb 26, 2024
1 parent 036713a commit b49dc0a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lottie-ios/Classes/Private/LOTAnimationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ + (nonnull instancetype)animationWithFilePath:(nonnull NSString *)filePath {
# pragma mark - Initializers

- (instancetype)initWithContentsOfURL:(NSURL *)url {
return [self initWithContentsOfURL:url errorBlock:nil];
}

- (instancetype)initWithContentsOfURL:(NSURL *)url
errorBlock:(nullable LOTInitializationErrorBlock)errorBlock {
self = [self initWithFrame:CGRectZero];
if (self) {
LOTComposition *laScene = [[LOTAnimationCache sharedCache] animationForKey:url.absoluteString];
Expand All @@ -69,14 +74,20 @@ - (instancetype)initWithContentsOfURL:(NSURL *)url {
[self _setupWithSceneModel:laScene];
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSData *animationData = [NSData dataWithContentsOfURL:url];
if (!animationData) {
NSError *error;
NSData *animationData = [NSData dataWithContentsOfURL:url options:0 error:&error];
if (error || !animationData) {
if (errorBlock) {
errorBlock(error);
}
return;
}
NSError *error;
NSDictionary *animationJSON = [NSJSONSerialization JSONObjectWithData:animationData
options:0 error:&error];
if (error || !animationJSON) {
if (errorBlock) {
errorBlock(error);
}
return;
}

Expand Down
6 changes: 6 additions & 0 deletions lottie-ios/Classes/PublicHeaders/LOTAnimationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "LOTValueDelegate.h"

typedef void (^LOTAnimationCompletionBlock)(BOOL animationFinished);
typedef void (^LOTInitializationErrorBlock)(NSError *error);

@interface LOTAnimationView : LOTView

Expand All @@ -37,6 +38,11 @@ typedef void (^LOTAnimationCompletionBlock)(BOOL animationFinished);
/// Loads animation asynchronously from the specified URL
- (nonnull instancetype)initWithContentsOfURL:(nonnull NSURL *)url;

/// Loads animation asynchronously from the specified URL. Calls the specified errorBlock if the
/// animation cannot be initialized from the contents of the URL.
- (nonnull instancetype)initWithContentsOfURL:(NSURL *)url
errorBlock:(nullable LOTInitializationErrorBlock)errorBlock;

/// Set animation name from Interface Builder
@property (nonatomic, strong) IBInspectable NSString * _Nullable animation;

Expand Down

0 comments on commit b49dc0a

Please sign in to comment.