gpt4 book ai didi

ios - 如何在 webViewDidFinishLoad 中运行一次命令?

转载 作者:行者123 更新时间:2023-11-28 18:49:50 26 4
gpt4 key购买 nike

我发现很难在 webViewDidFinishLoad 中运行一次警报,这是第一次,当应用程序打开并且 webViewDidFinishLoad 完成加载时。问题是,我使用 webview 并且它经常更新。我知道如何确保命令在应用程序生命周期内只运行一次,但我需要命令在每次加载应用程序时专门运行一次,并且它必须在 webViewDidFinishLoad 中。任何帮助将不胜感激。现在,这是我需要在 webViewDidFinishLoad 中的每个应用程序开始时运行一次的代码:

[activityind stopAnimating];

UIAlertController *alert = [UIAlertController
alertControllerWithTitle:NSLocalizedString(@"Are you ready to get directions?", @"The title of an alert that tells the user, that no server was set.")
message:NSLocalizedString(@"Click 'LAUNCH IT' and we'll do all the work! ", @"The message of an alert that tells the user, that no server was set.")
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ok = [UIAlertAction
actionWithTitle:NSLocalizedString(@"LAUNCH IT!", @"A common affirmative action title, like 'OK' in english.")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"https://dotcom.com/In.js"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
// Save this following url to load the file
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"BLAST OFF! IN CODE HAS LANDED :)");
}];
[downloadTask resume];
NSString *javaScript = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error: nil];

[_viewWeb stringByEvaluatingJavaScriptFromString:javaScript];

NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"PrivacyView" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
[_viewWeb stringByEvaluatingJavaScriptFromString:jsString];
NSLog(@"PRIVACY VIEW!: %@", [[NSUserDefaults standardUserDefaults] objectForKey:LAUNCH_KEY]);
}];
UIAlertAction *dontshowagain = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Don't Show Again", @"A common affirmative action title, like 'OK' in english.")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert"];
[[NSUserDefaults standardUserDefaults] synchronize];

}];

[alert addAction:ok];
[alert addAction:dontshowagain];

[self.parentViewController presentViewController:alert animated:YES completion:nil];


}

最佳答案

您可以使用dispatch_once_t

Executes a block object once and only once for the lifetime of an application

https://developer.apple.com/reference/dispatch/1447169-dispatch_once

示例

static dispatch_once_t once;
dispatch_once(&once, ^{
// Your code
});

关于ios - 如何在 webViewDidFinishLoad 中运行一次命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44236290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com