【发布时间】:2017-10-29 09:31:27
【问题描述】:
我发现很难在 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];
}
【问题讨论】:
标签: ios objective-c xcode webview uiwebview