UIWebview 不是持久性 cookie

UIWebview not persistant cookies

我的应用程序有 2 个故事板,它们使用相同的代码,设计方式相同,但一个用于 ipad,一个用于 iphone。 iPhone 故事板工作正常,但 iPad 故事板在变得不活动时无法保留它的 cookie(称为通知中心,暂停应用程序到后台并再次启动,等等)据我所知它应该工作正常。有人愿意看一下并帮助我吗?提前致谢!

变得活跃:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    self.user.loggedOut = [defaults objectForKey:@"loggedOut"];
    Database* db = [[Database alloc] init];

    //Get Node from voffice
    NSString* hostStr = [NSString stringWithFormat:@"************?klantnaam="];
    //NSLog(@"host string:%@", hostStr);
    NSData* dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
    //NSLog(@"Server output:%@", serverOutput);
    NSArray *output = [serverOutput componentsSeparatedByString:@"|"];
    self.nodeNummer = output[0];

    if([db checkConnection]){

        // Reset badge number to 0
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

        self.user = [db checkAppStart];
        BOOL userCheck = FALSE;

        // IF NO USER IN LOCAL DATABASE
        if(self.user != NULL){
            userCheck = TRUE;
        }

        //TRUE = mainNavigator / FALSE = loginView


        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            NSString *segueId = userCheck ? @"mainNavigator" : @"loginView";
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
            UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:segueId];

            if(userCheck){
                [self.window setRootViewController:initViewController];
            }
        }else{
            NSString *segueId = userCheck ? @"mainNavigator" : @"loginView";
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
            UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:segueId];

            if(userCheck){
                [self.window setRootViewController:initViewController];
            }
        }

    }

}

WebViewController.m

#import "WebViewController.h"


@interface WebViewController () 

@end

@implementation WebViewController
@synthesize vofficeWebview;
@synthesize activityIndicator;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


// DID LOAD
- (void)viewDidLoad
{
    [super viewDidLoad];

    [activityIndicator startAnimating];


    ad = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    defaults = [NSUserDefaults standardUserDefaults];
    db = [[Database alloc] init];


    // Pull to refresh part
    vofficeWebview.tag = 999;

    for(UIView* subview in vofficeWebview.subviews){
        if([subview isKindOfClass:[UIScrollView class]]){
            currentScrollView = (UIScrollView*)subview;
            vofficeWebview.delegate = (id) self;
        }
    }

    PullToRefreshView *pull = [[PullToRefreshView alloc] initWithScrollView:currentScrollView];

    [pull setDelegate:self];

    pull.tag = 998;

    [currentScrollView addSubview:pull];
    [self.view addSubview:vofficeWebview];


    [vofficeWebview setBackgroundColor:[UIColor clearColor]];
    [vofficeWebview setOpaque:NO];

}

- (void)viewWillAppear:(BOOL)animated{
    // Hide nagivationBar because of the website bar
//    [self.navigationController setNavigationBarHidden:YES];
}

// DID APPEAR
- (void)viewDidAppear:(BOOL)animated{
    NSURLRequest *urlRequest;
    BOOL killed = [defaults boolForKey:@"appKilled"];


    NSLog(@"name:%@", ad.user.name);
    NSLog(@"version:%@", ad.user.version);
    NSLog(@"pdakey:%@", ad.user.pdaKey);

    if([db checkConnection]){

        //Get Node from voffice
        NSString* hostStr = [NSString stringWithFormat:@"*************?klantnaam=%@", ad.user.company];
        //NSLog(@"host string:%@", hostStr);
        NSData* dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
        NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
        //NSLog(@"Server output:%@", serverOutput);
        NSArray *output = [serverOutput componentsSeparatedByString:@"|"];
        ad.nodeNummer = output[0];
        [[NSUserDefaults standardUserDefaults] setObject:ad.nodeNummer forKey:@"nodeNummer"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"Node nummer: %@", ad.nodeNummer);



        if(!ad.urlSaved){

            ad.urlSaved = TRUE;

            NSString* urlString = [NSString stringWithFormat:@"***********?db=%@&logincode=%@", ad.user.version, ad.user.pdaKey];
            urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
            [defaults setBool:FALSE forKey:@"appKilled"];

        }else if (killed){

            NSString* urlString = [NSString stringWithFormat:@"**********?db=%@&logincode=%@",ad.nodeNummer, ad.user.version, ad.user.pdaKey];
            urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
            [defaults setBool:FALSE forKey:@"appKilled"];
        }else{

            NSRange wordRange = NSMakeRange(0, 24);
            NSString* checkString = [NSString stringWithFormat:@"************", ad.nodeNummer];
            NSString* savedURL = [defaults objectForKey:@"savedURL"];

            NSString* searchString = [savedURL substringWithRange:wordRange];


            if([searchString isEqualToString:checkString]){

                NSTimeInterval interval = [ad.user.lastView timeIntervalSinceNow];
                NSInteger TimeToGoToDefault = 60*60*2;
                if(interval < -TimeToGoToDefault){
                    NSString* urlString = [NSString stringWithFormat:@"*********?db=%@&logincode=%@", ad.user.version, ad.user.pdaKey];
                    urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
                }else{
                    urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:savedURL]];
                }
            }else{

                NSString *urlString = [NSString stringWithFormat:@"http://***********?db=%@&logincode=%@",ad.nodeNummer, ad.user.version, ad.user.pdaKey];
                urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

            }
        }


        [vofficeWebview loadRequest:urlRequest];
    }
}



// START LOADING
- (void)webViewDidStartLoad:(UIWebView *)webView {

    [self.loadingIndicator startAnimating];

}


// FINISHED LOADING
- (void)webViewDidFinishLoad:(UIWebView *)webView {

    [self.loadingIndicator stopAnimating];

    [self.activityIndicator stopAnimating];

    vofficeWebview.hidden = FALSE;

    NSString *currentURL = vofficeWebview.request.URL.absoluteString;

    NSString* afbeeldingCheckString = @"";
    NSRange pdf = [currentURL rangeOfString:@".pdf" options:NSCaseInsensitiveSearch];
    NSRange worddoc = [currentURL rangeOfString:@".doc" options:NSCaseInsensitiveSearch];
    NSRange worddocx = [currentURL rangeOfString:@".docx" options:NSCaseInsensitiveSearch];

    if(pdf.location != NSNotFound){
        webView.scalesPageToFit = true;
    }
    if(worddoc.location != NSNotFound){
        webView.scalesPageToFit = true;
    }
    if(worddocx.location != NSNotFound){
        webView.scalesPageToFit = true;
    }
    if(currentURL.length > 47){
        afbeeldingCheckString = [currentURL substringToIndex:47];
    }


    NSString* savedURL = [defaults objectForKey:@"savedURL"];

    NSArray* separatedSavedURL = [currentURL componentsSeparatedByString:@"."];

    savedURL = [NSString stringWithFormat:@"http://%@%@",ad.nodeNummer, [currentURL substringFromIndex:[separatedSavedURL[0] length]]];

    NSLog(@"current URL: %@",currentURL);
    NSLog(@"saved URL: %@",savedURL);

    ad.urlSaved = savedURL;
    [defaults setObject:savedURL forKey:@"savedURL"];
    [defaults synchronize];

    NSString* leftBarButtonName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('knop')[0].getAttribute('name')"];

    NSString* leftButtonValue = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('knop')[0].getAttribute('link')"];

    NSLog(@"leftButtonTitle:'%@'", leftBarButtonName);

    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:vofficeWebview.request.URL];
    NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
    [request setAllHTTPHeaderFields:headers];

//    NSLog(@"test body:%@", leftButtonValue);


    if(leftBarButtonName != NULL && ![leftBarButtonName isEqualToString:@""] && leftBarButtonName.length != 0){
        [self changeLeftBarButton:leftBarButtonName link:leftButtonValue];
    }else{




    }


    [(PullToRefreshView*)[self.view viewWithTag:998] finishedLoading];



}


- (void)changeLeftBarButton:(NSString*)name link:(NSString*)link{

    leftButtonLink = link;

    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:name style:UIBarButtonItemStyleBordered target:nil action:nil];

    [self.navigationItem setLeftBarButtonItem:leftBarButtonItem];

    leftBarButtonItem.title = name;
    [leftBarButtonItem setTarget:self];
    [leftBarButtonItem setAction:@selector(changeLeftBarButtonLink)];

}

- (void)changeLeftBarButtonLink{
    NSString* URL = [NSString stringWithFormat:@"http://*************",ad.nodeNummer, leftButtonLink];
    NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
    [vofficeWebview loadRequest:urlRequest];
}


- (void) pullToRefreshViewShouldRefresh:(PullToRefreshView *)view{
    [(UIWebView*)[self.view viewWithTag:999] reload];

}

- (IBAction)textFieldReturn:(id)sender{

        [vofficeWebview stringByEvaluatingJavaScriptFromString:@"document.activeElement.blur()"];


}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goBackButtonPressed:(id)sender {
    [vofficeWebview goBack];
}

- (IBAction)goToDefaultButtonPressed:(id)sender {
    NSURLRequest *requestObj;

    NSString *defaultURL = [NSString stringWithFormat:@"http://*****/***/*****?db=%@&logincode=%@", ad.user.version, ad.user.pdaKey];
    //NSLog(@"%@", defaultURL);
    requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:defaultURL]];

    [vofficeWebview loadRequest:requestObj];
}
- (void)viewDidUnload {
    [self setVofficeView:nil];
    [super viewDidUnload];
}



@end

Iphone 故事板: http://pastebin.com/vUWX4XsD

Ipad 故事板: http://pastebin.com/BiSFq3LY

已通过实施正确的方法解决。我的前任没有使用正确的方法查看某些页面