Skip to content

Commit b282cf6

Browse files
committed
Web view: set underPageBackgroundColor from META theme-color
1 parent 38d206f commit b282cf6

File tree

3 files changed

+93
-7
lines changed

3 files changed

+93
-7
lines changed

Sources/InAppSettingsKit/Controllers/IASKAppSettingsWebViewController.m

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "IASKAppSettingsWebViewController.h"
1818
#import "IASKSettingsReader.h"
1919
#import "IASKSpecifier.h"
20+
#import "UIColor+IASKAdditions.h"
2021

2122
@interface IASKAppSettingsWebViewController()
2223
@property (nullable, nonatomic, strong, readwrite) WKWebView *webView;
@@ -209,14 +210,15 @@ - (void)viewWillLayoutSubviews {
209210
[super viewWillLayoutSubviews];
210211

211212
self.webView.frame = self.view.bounds;
212-
if (@available(iOS 15.0, *)) {
213-
self.webView.underPageBackgroundColor = UIColor.systemBackgroundColor;
214-
}
215213
}
216214

217215
- (void)viewWillAppear:(BOOL)animated {
218216
[super viewWillAppear:animated];
219-
217+
218+
if (@available(iOS 15.0, *)) {
219+
self.webView.underPageBackgroundColor = UIColor.systemBackgroundColor;
220+
}
221+
220222
// Load URL:
221223
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
222224
}
@@ -348,11 +350,50 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat
348350
// Stop and hide default indicator and update title:
349351
[self.activityIndicatorView stopAnimating];
350352
[self.webView evaluateJavaScript:@"document.title" completionHandler:^(id result, NSError *error) {
351-
NSString* title = (NSString*)result;
352-
self.title = self.customTitle.length ? self.customTitle : title;
353+
self.title = self.customTitle.length ? self.customTitle : result;
353354
}];
354355

355-
// Update button states when loading finishes:
356+
if (@available(iOS 15.0, *)) {
357+
NSString *javascript = @"function getThemeColorAsHex() {\n"
358+
" const themeColorMeta = document.querySelector('meta[name=\"theme-color\"]');\n"
359+
" if (!themeColorMeta) {\n"
360+
" return null;\n"
361+
" }\n"
362+
" \n"
363+
" const color = themeColorMeta.content;\n"
364+
" \n"
365+
" const temp = document.createElement('div');\n"
366+
" temp.style.color = color;\n"
367+
" document.body.appendChild(temp);\n"
368+
" \n"
369+
" const computedColor = window.getComputedStyle(temp).color;\n"
370+
" document.body.removeChild(temp);\n"
371+
" \n"
372+
" const match = computedColor.match(/rgba?\\((\\d+),\\s*(\\d+),\\s*(\\d+)(?:,\\s*([\\d.]+))?\\)/);\n"
373+
" \n"
374+
" if (!match) {\n"
375+
" return color; // Fallback: Originalwert zurückgeben\n"
376+
" }\n"
377+
" \n"
378+
" const r = parseInt(match[1]);\n"
379+
" const g = parseInt(match[2]);\n"
380+
" const b = parseInt(match[3]);\n"
381+
" const a = match[4] ? parseFloat(match[4]) : 1;\n"
382+
" \n"
383+
" const toHex = (num) => num.toString(16).padStart(2, '0');\n"
384+
" \n"
385+
" const alphaHex = Math.round(a * 255).toString(16).padStart(2, '0');\n"
386+
" return `${toHex(r)}${toHex(g)}${toHex(b)}${alphaHex}`;\n"
387+
"}\n"
388+
"getThemeColorAsHex()";
389+
390+
[self.webView evaluateJavaScript:javascript completionHandler: ^(id result, NSError *error) {
391+
UIColor *color = [UIColor iaskColorWithHexString:result];
392+
self.webView.underPageBackgroundColor = color;
393+
}];
394+
}
395+
396+
// Update button states when loading finishes:
356397
[self updateNavigationButtons];
357398
}
358399

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// UIColor+IASKAdditions.m
3+
// InAppSettingsKit
4+
//
5+
// Created by Ortwin Gentz on 2025-10-23.
6+
// Copyright 2025 FutureTap. All rights reserved.
7+
//
8+
9+
#import "UIColor+IASKAdditions.h"
10+
11+
@implementation UIColor(IASKAdditions)
12+
13+
// Returns a UIColor for an RGBA string
14+
15+
+ (UIColor* _Nullable)iaskColorWithHexString:(NSString *)stringToConvert {
16+
if (![stringToConvert isKindOfClass:NSString.class] || stringToConvert.length != 8) return nil;
17+
NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
18+
unsigned hex;
19+
if (![scanner scanHexInt:&hex] || stringToConvert.length != 8) return nil;
20+
21+
int r = (hex >> 24) & 0xFF;
22+
int g = (hex >> 16) & 0xFF;
23+
int b = (hex >> 8) & 0xFF;
24+
int a = (hex) & 0xFF;
25+
26+
return [UIColor colorWithRed:r / 255.0f
27+
green:g / 255.0f
28+
blue:b / 255.0f
29+
alpha:a / 255.0f];
30+
}
31+
32+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// UIColor+IASKAdditions.h
3+
// InAppSettingsKit
4+
//
5+
// Created by Ortwin Gentz on 2025-10-23.
6+
// Copyright 2025 FutureTap. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UIColor(IASKAdditions)
12+
+ (nullable UIColor*)iaskColorWithHexString:(nullable NSString *)stringToConvert;
13+
@end

0 commit comments

Comments
 (0)