From 28a9d4309d7aeff62458e1492fa862df77b1148e Mon Sep 17 00:00:00 2001 From: Masoud Fallahpourbaee Date: Mon, 10 Jun 2024 16:08:21 +0200 Subject: [PATCH] Set the background color of KlarnaStandaloneWebView to transparent on the iOS side --- .../newarch/KlarnaStandaloneWebViewWrapper.mm | 23 ++++++++++++++++++- .../oldarch/KlarnaStandaloneWebViewWrapper.mm | 23 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm b/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm index 3ebf7f21..dd69734d 100644 --- a/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm +++ b/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm @@ -37,7 +37,11 @@ - (id)init { } -(void)dealloc { - [self.klarnaStandaloneWebView removeObserver:self forKeyPath:PROPERTY_NAME_ESTIMATED_PROGRESS context:nil]; + @try { + [self.klarnaStandaloneWebView removeObserver:self forKeyPath:PROPERTY_NAME_ESTIMATED_PROGRESS context:nil]; + } @catch(NSException *exception) { + RCTLog(@"Could not remove the progress observer: %@", exception); + } } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { @@ -80,6 +84,7 @@ - (void)initializeKlarnaStandaloneWebView:(nonnull NSString*)returnUrl { self.klarnaStandaloneWebView.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:self.klarnaStandaloneWebView]; + [self setWebViewBackgroundToTransparent]; [NSLayoutConstraint activateConstraints:[[NSArray alloc] initWithObjects: [self.klarnaStandaloneWebView.topAnchor constraintEqualToAnchor:self.topAnchor], @@ -89,6 +94,17 @@ - (void)initializeKlarnaStandaloneWebView:(nonnull NSString*)returnUrl { ]]; } +- (void)setWebViewBackgroundToTransparent { + for (UIView *subview in self.klarnaStandaloneWebView.subviews) { + if ([subview isKindOfClass:[WKWebView class]]) { + WKWebView *webView = (WKWebView *) subview; + webView.backgroundColor = [UIColor clearColor]; + webView.opaque = NO; + webView.scrollView.backgroundColor = [UIColor clearColor]; + } + } +} + - (void)layoutSubviews { [super layoutSubviews]; self.klarnaStandaloneWebView.frame = self.bounds; @@ -262,6 +278,11 @@ - (void)reload { } - (NSString *)serializeDictionaryToJsonString:(NSDictionary> *)dictionary { + if (!dictionary) { + RCTLog(@"Dictionary is nil"); + return @"{}"; + } + NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error]; diff --git a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm index 822ebbb3..0d8646f3 100644 --- a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm +++ b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm @@ -26,7 +26,11 @@ - (id)init { } -(void)dealloc { - [self.klarnaStandaloneWebView removeObserver:self forKeyPath:PROPERTY_NAME_ESTIMATED_PROGRESS context:nil]; + @try { + [self.klarnaStandaloneWebView removeObserver:self forKeyPath:PROPERTY_NAME_ESTIMATED_PROGRESS context:nil]; + } @catch(NSException *exception) { + RCTLog(@"Could not remove the progress observer: %@", exception); + } } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { @@ -75,6 +79,7 @@ - (void)initializeKlarnaStandaloneWebView { self.klarnaStandaloneWebView.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:self.klarnaStandaloneWebView]; + [self setWebViewBackgroundToTransparent]; [NSLayoutConstraint activateConstraints:[[NSArray alloc] initWithObjects: [self.klarnaStandaloneWebView.topAnchor constraintEqualToAnchor:self.topAnchor], @@ -84,6 +89,17 @@ - (void)initializeKlarnaStandaloneWebView { ]]; } +- (void)setWebViewBackgroundToTransparent { + for (UIView *subview in self.klarnaStandaloneWebView.subviews) { + if ([subview isKindOfClass:[WKWebView class]]) { + WKWebView *webView = (WKWebView *) subview; + webView.backgroundColor = [UIColor clearColor]; + webView.opaque = NO; + webView.scrollView.backgroundColor = [UIColor clearColor]; + } + } +} + - (void)layoutSubviews { [super layoutSubviews]; self.klarnaStandaloneWebView.frame = self.bounds; @@ -182,6 +198,11 @@ - (void)klarnaComponent:(id _Nonnull)klarnaComponent encounter #pragma mark - Events - (NSString *)serializeDictionaryToJsonString:(NSDictionary> *)dictionary { + if (!dictionary) { + RCTLog(@"Dictionary is nil"); + return @"{}"; + } + NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];