Last active
September 28, 2023 06:59
-
-
Save davbeck/8613f2e74e8fc335c14d to your computer and use it in GitHub Desktop.
Boilerplate implementation of WKUIDelegate to support Javascript alerts.
This file contains 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
#pragma mark - WKUIDelegate | |
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)())completionHandler | |
{ | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil | |
message:message | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { | |
completionHandler(); | |
}]]; | |
[self presentViewController:alert animated:YES completion:nil]; | |
} | |
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler | |
{ | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil | |
message:message | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { | |
completionHandler(NO); | |
}]]; | |
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { | |
completionHandler(YES); | |
}]]; | |
[self presentViewController:alert animated:YES completion:nil]; | |
} | |
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *result))completionHandler | |
{ | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil | |
message:prompt | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { | |
textField.placeholder = prompt; | |
textField.secureTextEntry = NO; | |
textField.text = defaultText; | |
}]; | |
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { | |
completionHandler(nil); | |
}]]; | |
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { | |
completionHandler([alert.textFields.firstObject text]); | |
}]]; | |
[self presentViewController:alert animated:YES completion:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@davbeck
Hi, Is there any license information?
I want to use your code in my commercial app.
But I can'f find any terms and conditions for use, reproduction, and distribution.
I wonder if I can use the code for commercial distribution or not.
Please let me know how I can use the code for commercial distribution.