Skip to content

Commit

Permalink
Merge pull request #252 from klarna/fix_ios_standalone_webview_transp…
Browse files Browse the repository at this point in the history
…arency_issue

Fix the KlarnaStandaloneWebView transparency issue on the iOS side
  • Loading branch information
MasoudFallahpourbaee authored Jun 11, 2024
2 parents b27796a + 28a9d43 commit ade4559
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
23 changes: 22 additions & 1 deletion ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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],
Expand All @@ -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;
Expand Down Expand Up @@ -262,6 +278,11 @@ - (void)reload {
}

- (NSString *)serializeDictionaryToJsonString:(NSDictionary<NSString *, id<NSCoding>> *)dictionary {
if (!dictionary) {
RCTLog(@"Dictionary is nil");
return @"{}";
}

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];

Expand Down
23 changes: 22 additions & 1 deletion ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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],
Expand All @@ -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;
Expand Down Expand Up @@ -182,6 +198,11 @@ - (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent encounter
#pragma mark - Events

- (NSString *)serializeDictionaryToJsonString:(NSDictionary<NSString *, id<NSCoding>> *)dictionary {
if (!dictionary) {
RCTLog(@"Dictionary is nil");
return @"{}";
}

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];

Expand Down

0 comments on commit ade4559

Please sign in to comment.