UIWebView with local file, does not open the first time [Objective-C]

0

Hello, I'm new to Objective-C, and I took a project to maintain my company, do not condemn the idea was not my kkk

But the following, we have an app that opens a UIWebView to a local file that goes bundled with the app. This always worked the way it was, but now the first time I open the application after installing a crash does not find the index.html, however if I close the app and open again without reinstalling it works normally.

By checking the code, when opening the app, the controller (code below) makes a copy of the webs files from one directory to another (a user directory apparently), and even doing the successful copy (debug) the app never works on the first opening.

Can someone explain to me if the process is correct, or if there is another way I can do it? I'm pretty lost and I could not find a solution to that.

 (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    NSString *urlStr = url.absoluteString;
    NSLog(@"shouldStartLoadWithRequest %@",urlStr);
    return [self processaURL:urlStr];
}

   (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self preparaHtmlLocalCompletion:^(NSString *path) {

        dispatch_async(dispatch_get_main_queue(), ^{

            if ([Sessao usuarioLogado]) {
                [self obtemKPIsCompletion:^{

                    // Carrega o webview
                    NSString *principalHtmlPath = [path stringByAppendingPathComponent:@"index.html"];
                    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:principalHtmlPath]];
                    [self.webView loadRequest:request];

                }];
            }
            else {
                [Sessao logoff];
                [self fecharAnimado:NO];
            }
        });
    }];
}


for (NSURL *fileURL in enumerator) {
        NSString *filename;
        [fileURL getResourceValue:&filename forKey:NSURLNameKey error:nil];

        NSNumber *isDirectory;
        [fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];

        NSString *destinationPath = [[destinoURL path] stringByAppendingPathComponent:[fileURL lastPathComponent]];

        if ([isDirectory boolValue]) {
            // Cria o diretório e copia o conteúdo
            if (![fileManager fileExistsAtPath:destinationPath isDirectory:nil]){
                NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath];
                [fileManager createDirectoryAtURL:destinationURL withIntermediateDirectories:YES attributes:nil error:nil];
                [self copia:fileURL paraDestino:destinationURL];
            }
        }
        else {
            // Copia o arquivo
            if (![fileManager fileExistsAtPath:destinationPath isDirectory:nil]){
                [fileManager copyItemAtURL:fileURL toURL:[NSURL fileURLWithPath:destinationPath] error:nil];
            }
        }
    }

Thank you.

    
asked by anonymous 24.07.2018 / 15:48

0 answers