-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Detox iOS18 compatibility - #4749 #4816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
markdevocht
wants to merge
13
commits into
master
Choose a base branch
from
feat/Detox-iOS18-compatibility-#4749
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−24
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3041c15
Detox iOS18 compatibility - #4749
markdevocht 4439673
update - uncoupling from the test app
markdevocht 497a7ec
Clean up
markdevocht 185494c
update genycloud runnings to 1, iOS 17.0.1 to iOS 18.0.0, and message…
markdevocht 0764383
Merge remote-tracking branch 'origin/master' into feat/Detox-iOS18-co…
markdevocht 285495e
revert the concurrent testers to 5
markdevocht c36f9a1
Requested changes for PR
markdevocht 7808d75
Merge remote-tracking branch 'origin/master' into feat/Detox-iOS18-co…
markdevocht 5956181
revert to iOS 17.0.1
markdevocht 505e1e1
crash fix
markdevocht ba88ce6
Merge remote-tracking branch 'origin/master' into feat/Detox-iOS18-co…
markdevocht 0b43f1d
rollback for displayDuration. Set back to 2.0 sec. For iOS18.0 we do …
markdevocht d422897
updated ios !8 support for overlays
markdevocht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,19 @@ | |
|
||
@import ObjectiveC; | ||
|
||
void WKPreferencesSetWebSecurityEnabled(id, bool); | ||
|
||
//void WKPreferencesSetWebSecurityEnabled(id, bool); | ||
|
||
|
||
// Private WebKit API declarations | ||
typedef struct OpaqueWKPreferences* WKPreferencesRef; | ||
extern void WKPreferencesSetWebSecurityEnabled(WKPreferencesRef, bool); | ||
|
||
|
||
@interface WKWebView (DetoxSecurity) | ||
- (instancetype)dtx_initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration; | ||
@end | ||
|
||
|
||
@interface DTXFakeWKPreferencesRef: NSObject | ||
@property (nonatomic) void* _apiObject; | ||
|
@@ -16,61 +28,121 @@ @interface DTXFakeWKPreferencesRef: NSObject | |
@implementation DTXFakeWKPreferencesRef | ||
@end | ||
|
||
|
||
/// Set web-security policy for WebKit (e.g. CORS restriction). | ||
/// | ||
/// @note Since we can't access the `WKPreferencesSetWebSecurityEnabled` directly with | ||
markdevocht marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// a `WKPreferences*`, we wrap it in a `WKPreferencesRef`, which can be passed to this function. | ||
/// a `WKPreferences*`, we wrap it in a `WKPreferencesRef` in the getWKPrefsRef function, which can be passed to this function. | ||
/// This private API is not officially supported on iOS, and generally used for debugging / testing | ||
/// purposes on MacOS only. So there's no guarantee that it will work in the future. | ||
void DTXPreferencesSetWebSecurityEnabled(WKPreferences* prefs, bool enabled) { | ||
DTXFakeWKPreferencesRef* fakeRef = [DTXFakeWKPreferencesRef new]; | ||
|
||
Ivar ivar = class_getInstanceVariable([WKPreferences class], "_preferences"); | ||
void* realPreferences = (void*)(((uintptr_t)prefs) + ivar_getOffset(ivar)); | ||
fakeRef._apiObject = realPreferences; | ||
|
||
WKPreferencesSetWebSecurityEnabled(fakeRef, enabled); | ||
if (!prefs) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this soft-failing approach dangerous here? |
||
|
||
if (@available(iOS 18.0, *)) { | ||
void *prefsPtr = (__bridge void *)(prefs); | ||
WKPreferencesSetWebSecurityEnabled((WKPreferencesRef)prefsPtr, enabled); | ||
|
||
} else { | ||
DTXFakeWKPreferencesRef* fakeRef = [DTXFakeWKPreferencesRef new]; | ||
|
||
Ivar ivar = class_getInstanceVariable([WKPreferences class], "_preferences"); | ||
void* realPreferences = (void*)(((uintptr_t)prefs) + ivar_getOffset(ivar)); | ||
fakeRef._apiObject = realPreferences; | ||
WKPreferencesSetWebSecurityEnabled((__bridge WKPreferencesRef)fakeRef, enabled); | ||
} | ||
} | ||
|
||
@implementation WKWebViewConfiguration (Detox) | ||
|
||
+ (void)load { | ||
[self swizzleWKWebViewConfigurationSetPreferences]; | ||
|
||
if (@available(iOS 18.0, *)) { | ||
[self swizzleWKWebViewInitWithFrameConfiguration]; | ||
} | ||
} | ||
|
||
+ (void)swizzleWKWebViewConfigurationSetPreferences { | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
Class class = [self class]; | ||
|
||
SEL originalSelector = @selector(setPreferences:); | ||
SEL swizzledSelector = @selector(dtx_setPreferences:); | ||
|
||
Method originalMethod = class_getInstanceMethod(class, originalSelector); | ||
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); | ||
|
||
BOOL didAddMethod = class_addMethod(class, | ||
originalSelector, | ||
method_getImplementation(swizzledMethod), | ||
method_getTypeEncoding(swizzledMethod)); | ||
originalSelector, | ||
method_getImplementation(swizzledMethod), | ||
method_getTypeEncoding(swizzledMethod)); | ||
|
||
if (didAddMethod) { | ||
class_replaceMethod(class, | ||
swizzledSelector, | ||
method_getImplementation(originalMethod), | ||
method_getTypeEncoding(originalMethod)); | ||
swizzledSelector, | ||
method_getImplementation(originalMethod), | ||
method_getTypeEncoding(originalMethod)); | ||
} else { | ||
method_exchangeImplementations(originalMethod, swizzledMethod); | ||
} | ||
}); | ||
} | ||
|
||
- (void)dtx_setPreferences:(WKPreferences *)preferences { | ||
if ([self shouldDisableWebKitSecurity]) { | ||
DTXPreferencesSetWebSecurityEnabled(preferences, NO); | ||
} | ||
+ (void)swizzleWKWebViewInitWithFrameConfiguration { | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
Class wkWebViewClass = [WKWebView class]; | ||
|
||
SEL originalSelector = @selector(initWithFrame:configuration:); | ||
SEL swizzledSelector = @selector(dtx_initWithFrame:configuration:); | ||
|
||
Method originalMethod = class_getInstanceMethod(wkWebViewClass, originalSelector); | ||
Method swizzledMethod = class_getInstanceMethod(wkWebViewClass, swizzledSelector); | ||
|
||
[self dtx_setPreferences:preferences]; | ||
BOOL didAddMethod = class_addMethod(wkWebViewClass, | ||
originalSelector, | ||
method_getImplementation(swizzledMethod), | ||
method_getTypeEncoding(swizzledMethod)); | ||
|
||
if (didAddMethod) { | ||
class_replaceMethod(wkWebViewClass, | ||
swizzledSelector, | ||
method_getImplementation(originalMethod), | ||
method_getTypeEncoding(originalMethod)); | ||
} else { | ||
method_exchangeImplementations(originalMethod, swizzledMethod); | ||
} | ||
}); | ||
} | ||
|
||
- (void)dtx_setPreferences:(WKPreferences *)preferences { | ||
if ([self shouldDisableWebKitSecurity]) { | ||
DTXPreferencesSetWebSecurityEnabled(preferences, NO); | ||
} | ||
[self dtx_setPreferences:preferences]; | ||
} | ||
|
||
- (BOOL)shouldDisableWebKitSecurity { | ||
return [NSUserDefaults.standardUserDefaults boolForKey:@"detoxDisableWebKitSecurity"]; | ||
return [NSUserDefaults.standardUserDefaults boolForKey:@"detoxDisableWebKitSecurity"]; | ||
} | ||
|
||
@end | ||
|
||
|
||
@implementation WKWebView (DetoxSecurity) | ||
|
||
- (instancetype)dtx_initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration { | ||
BOOL shouldDisable = [configuration shouldDisableWebKitSecurity]; | ||
if (shouldDisable) { | ||
if (!configuration.preferences) { | ||
configuration.preferences = [[WKPreferences alloc] init]; | ||
} | ||
|
||
DTXPreferencesSetWebSecurityEnabled(configuration.preferences, !shouldDisable); | ||
} | ||
|
||
return [self dtx_initWithFrame:frame configuration:configuration]; | ||
} | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻