为什么UserAgent会影响我的NSURLSessionDownloadTask?

时间:2017-06-01 20:10:13

标签: ios objective-c xcode nsurlconnection

我在AppDelegate中有这段代码:

 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

我在webViewDidFinishLoad中运行NSURLSessionDownloadTask,如下所示:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:@"https://gps.com/Intel.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! 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]);
}

当我运行我的应用程序时会发生下载任务不间断运行但是一旦我从AppDelegate中删除UserAgent脚本,下载任务就像它假设的那样工作并下载javascript文件ONCE。任何有助于使downloadAask运行ONCE或者告诉我为什么UserAgent脚本会导致这样的错误的帮助将不胜感激!

附加信息:

我的控制台日志看起来像这样,因为downloadtask运行了很多次:

2017-06-01 14:48:50.009898-0700 GPS[1664:311236] BLAST OFF! CODE HAS LANDED :)
2017-06-01 14:48:51.019193-0700 GPS[1664:311236] PRIVACY VIEW!: Yes
2017-06-01 14:48:51.250776-0700 GPS[1664:311236] BLAST OFF! CODE HAS LANDED :)
2017-06-01 14:48:52.767284-0700 GPS[1664:311236] PRIVACY VIEW!: Yes
2017-06-01 14:48:52.968515-0700 GPS[1664:311236] BLAST OFF! CODE HAS LANDED :)
2017-06-01 14:48:55.018189-0700 GPS[1664:311236] PRIVACY VIEW!: Yes
2017-06-01 14:48:55.124841-0700 GPS[1664:311236] BLAST OFF! CODE HAS LANDED :)
2017-06-01 14:48:57.265404-0700 GPS[1664:311236] PRIVACY VIEW!: Yes
2017-06-01 14:48:57.265980-0700 GPS[1664:311236] BLAST OFF! CODE HAS LANDED :)
2017-06-01 14:48:59.033294-0700 GPS[1664:311236] PRIVACY VIEW!: Yes
2017-06-01 14:48:59.151356-0700 GPS[1664:311236] BLAST OFF! CODE HAS LANDED :)
2017-06-01 14:49:02.547727-0700 GPS[1664:311236] PRIVACY VIEW!: Yes
2017-06-01 14:49:02.549679-0700 GPS[1664:311236] BLAST OFF! CODE HAS LANDED :)
2017-06-01 14:49:04.801953-0700 GPS[1664:311236] PRIVACY VIEW!: Yes
2017-06-01 14:49:04.939535-0700 GPS[1664:311236] BLAST OFF! CODE HAS LANDED :)

0 个答案:

没有答案