简体   繁体   中英

How do I fix my webview loading the second time and not the first?

This question is kind of self-explanatory but I'll explain it anyways. For some reason when I try and load a new web page it will see the web page URL except it won't load it the first time you press the button. Only when you press the button again will it actually load. How do I fix this?

Here's the code for my button:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];

// Download and write to file

NSURL *url = [NSURL URLWithString:_varString];

NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
//Load file in UIWebView
[_Hoot loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

*Also note, even though I'm copying the web page here and then loading it from a file, I still get the same problem if I'm just loading it from a URL.

Try this:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];

    // Download and write to file
    NSURL *url = [NSURL URLWithString:_varString];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    [urlData writeToFile:filePath atomically:YES];

    //Load file in UIWebView
    NSURL *urlen = [NSURL fileURLWithPath:filePath];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
    [_Hoot loadRequest:urlRequest];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM