-
Notifications
You must be signed in to change notification settings - Fork 984
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
(ios) feat: navigationAction in shouldOverrideLoadWithRequest #1333
base: master
Are you sure you want to change the base?
Conversation
The problem with this approach is that it exposes WebKit-specific API (by exposing What information about the frame do you need specifically? I'm guessing the security origin? |
Currently I only check if For my part, just passing the |
As an example, this is the code that I am currently testing in the plugin. @objc func shouldOverrideLoadWithRequest(_ request: URLRequest, navigationAction: WKNavigationAction) -> Bool {
var allowNavigationsPass = true
if let url = request.url, url.scheme == "http" || url.scheme == "https" {
if navigationAction.sourceFrame == nil {
allowNavigationsPass = false
}
switch navigationAction.navigationType {
case .linkActivated: // This is not triggered in some iframes
allowNavigationsPass = false
case .other:
let range = url.absoluteString.range(of: "utm_content")
if range != nil {
allowNavigationsPass = false
}
default:
break
}
if !allowNavigationsPass {
UIApplication.shared.open(url)
}
}
return allowNavigationsPass
} |
What do you think about something similar to this? This should avoid the problem of exposing the WebKit API and should allow you to extend it if a plugin needs other information in the future. @interface CDVWebViewOverrideLoadWithRequest : NSObject
@property(nonatomic, readwrite) NSURLRequest * request;
@property(nonatomic, readwrite) int navigationType;
@property(nonatomic, readwrite) Boolean sourceFrameIsNil;
//@property(nonatomic, readwrite) WKFrameInfo * sourceFrame;
// ...
@end
@implementation CDVWebViewOverrideLoadWithRequest
@end and SEL selector2 = NSSelectorFromString(@"shouldOverrideLoadWithRequest:overrideLoadWithRequest:");
if ([plugin respondsToSelector:selector2]) {
anyPluginsResponded = YES;
CDVWebViewOverrideLoadWithRequest *overrideLoadWithRequest = [[CDVWebViewOverrideLoadWithRequest alloc] init];
overrideLoadWithRequest.request = navigationAction.request;
overrideLoadWithRequest.navigationType = (int)navigationAction.navigationType;
overrideLoadWithRequest.sourceFrameIsNil = (navigationAction.sourceFrame == nil) ? YES : NO;
//...
shouldAllowRequest = (((BOOL (*)(id, SEL, id, id))objc_msgSend)(plugin, selector2, navigationAction.request, overrideLoadWithRequest));
if (!shouldAllowRequest) {
break;
}
} Sorry if the code is not quite correct, I'm still learning how |
My current thinking is that we could pull the properties of the WKNavigationAction into an NSDictionary and pass that along as an "extra data" parameter to the plugin handler method. It's not as strongly typed as what you've proposed, but is more future compatible for other types of WebViews with their own navigation properties (since all the dictionary keys would be optional) |
It looks good to me. |
This also adds a dictionary parameter containing the other navigation action details so that plugins can make choices based on frames. Closes apacheGH-1272. Closes apacheGH-1333. Co-Authored-By: Michael Tamburro <[email protected]>
This also adds a dictionary parameter containing the other navigation action details so that plugins can make choices based on frames. Closes apacheGH-1272. Closes apacheGH-1333. Co-Authored-By: Michael Tamburro <[email protected]>
This also adds a dictionary parameter containing the other navigation action details so that plugins can make choices based on frames. Closes apacheGH-1272. Closes apacheGH-1333. Co-Authored-By: Michael Tamburro <[email protected]>
This also adds a dictionary parameter containing the other navigation action details so that plugins can make choices based on frames. Closes apacheGH-1272. Closes apacheGH-1333. Co-Authored-By: Michael Tamburro <[email protected]>
This also adds a dictionary parameter containing the other navigation action details so that plugins can make choices based on frames. Closes apacheGH-1272. Closes apacheGH-1333. Co-Authored-By: Michael Tamburro <[email protected]>
Platforms affected
iOS
Motivation and Context
Currently
shouldOverrideLoadWithRequest
only returns therequest
andnavigationType
, the plugin I'm working with needs to check ifnavigationAction.sourceFrame
is null or not to decide whether to open the url in the browser, currently I can't do that (far as I know).Description
Added a new
shouldOverrideLoadWithRequest:navigationAction
selector to have full access to thenavigationAction
object.Testing
I have kept the old implementation so that plugins that already use it are not affected, with the change both the old and the new one work correctly.
I don't know if there is a better way to implement this, since my knowledge of
Objective-C
is basic, or if it would be better to replace completely with the new one, to avoid redundancy.Checklist
(platform)
if this change only applies to one platform (e.g.(android)
)