从互联网上下载 javascript 文件时,它去了哪里?
When downloading a javascript file from the internet, where does it go?
我正在使用这样的代码:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.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(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
NSString *javaScript = [NSString stringWithContentsOfURL: __URL_YOU_SAVED_AVOBE__ encoding:NSUTF8StringEncoding error: nil];
[webView stringByEvaluatingJavaScriptFromString:javaScript];
每次打开应用程序时,此代码都会下载一个 js 文件并将其加载到 uiwebview。下载并加载到 uiwebview 的 javascript 文件会发生什么情况?是否保存在应用程序中?它保存在 iPhone 上吗?每次打开应用程序下载文件时,它是否占用 iPhone 上的 GB 和内存?但说真的,文件去了哪里?哈哈
当您创建 NSURLSessionDownloadTask 时,文件保存在磁盘上并带有目的地。
你肯定可以用 NSFileManager 删除文件,检查它是 api。
如果你想要一个只执行的 js 文件,使用来自源 url 的 NSString 加载,这样 "JS String" 只会在你每次调用这个 action.Then 时在 RAM 上,你可以在你的 webview 上执行它js。
满意吗?
我正在使用这样的代码:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.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(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
NSString *javaScript = [NSString stringWithContentsOfURL: __URL_YOU_SAVED_AVOBE__ encoding:NSUTF8StringEncoding error: nil];
[webView stringByEvaluatingJavaScriptFromString:javaScript];
每次打开应用程序时,此代码都会下载一个 js 文件并将其加载到 uiwebview。下载并加载到 uiwebview 的 javascript 文件会发生什么情况?是否保存在应用程序中?它保存在 iPhone 上吗?每次打开应用程序下载文件时,它是否占用 iPhone 上的 GB 和内存?但说真的,文件去了哪里?哈哈
当您创建 NSURLSessionDownloadTask 时,文件保存在磁盘上并带有目的地。 你肯定可以用 NSFileManager 删除文件,检查它是 api。 如果你想要一个只执行的 js 文件,使用来自源 url 的 NSString 加载,这样 "JS String" 只会在你每次调用这个 action.Then 时在 RAM 上,你可以在你的 webview 上执行它js。 满意吗?