如何在iOS中存储UIWebView缓存,以便在页面打开时不会闪烁

时间:2014-07-19 10:31:54

标签: javascript ios iphone caching uiwebview

我有一个简单的应用程序在UIWebView中加载针对iPhone优化的网站。 但是当webview加载时,某些页面会在点击时闪烁。 我认为这是一个缓存问题。我正在使用一些变量来存储最后的页面值。 请提出一些好主意。 感谢

以下是我正在使用的代码

NSString *strPassword1=[[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
NSString *strUsername1=[[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
if (strUsername1.length!=0&&strPassword1.length!=0)
{


    NSString* userId1   = strUsername1; //here just replace that string to the username
    NSString* password1 =   strPassword1;//here just replace that string to the password


    if(userId1 != nil && password1 != nil )
    {

        NSString*  jScriptString1 = [NSString  stringWithFormat:@"document.getElementById('username').value='%@'",userId1];
        //username is the id for username field in Login form

        NSString*  jScriptString2 = [NSString stringWithFormat:@"document.getElementById('password').value='%@'",password1];
        //here password is the id for password field in Login Form
        //Now Call The Javascript for entring these Credential in login Form
        [webView stringByEvaluatingJavaScriptFromString:jScriptString1];

        [webView stringByEvaluatingJavaScriptFromString:jScriptString2];
        //Further if you want to submit login Form Automatically the you may use below line

        [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit();"];


        NSString *strTemp=[NSString stringWithFormat:@"%@", webView.request.URL];
        if ([strTemp isEqualToString:@"http://test.xyz.com/"])
            _webView.hidden=NO;

    }
}
else
{

    if(self.userName.length!=0&&self.password.length!=0)
    {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://test.xyz.com/login?ios_id=%@",appKey]];

        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

        NSOperationQueue *queue = [[NSOperationQueue alloc] init];

        [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)

        {
           // NSLog(@"%@", data);
        }];
    }


    [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%@",self.userName] forKey:@"userName"];


    [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%@",self.password] forKey:@"password"];
    [[NSUserDefaults standardUserDefaults]synchronize];




}

1 个答案:

答案 0 :(得分:0)

一个灵巧的解决方案是在负载发生时更改UIWebViews。您的webView的设置属性以及将出现负载的新“屏幕外”Web视图...

@property(nonatomic, weak) IBOutlet UIWebView *webView;  // assume you have this 
@property(nonatomic, strong) UIWebView *offscreenWebView;

初始化屏幕外Web视图,使其具有与初始Web视图相同的框架和委托......

self.offscreenWebView = [[UIWebView alloc] initWithFrame:self.webView.frame];
self.offscreenWebView.delegate = self;

实施委托。当代表要求启动加载时,请回答“否”,而是在屏幕外Web视图中启动加载...

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    // It's advisable to present some "busy" UI here, like an activity indicator
    [self.offscreenWebView loadRequest:request];
    return NO;
}

加载完成后,交换屏幕和屏幕外的Web视图......

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    // remove the busy UI here

    if (webView == self.offscreenWebView) {
        // swap them on the parent
        [self.view addSubview:self.offscreenWebView];
        [self.webView removeFromSuperview];

        // swap our pointers to them
        UIWebView *temp = self.offscreenWebView;
        self.offscreenWebView = self.webView;
        self.webView = temp;
    }
}