繁体   English   中英

如何在UIWebView中保存登录内容

[英]How to save the login content in UIWebView

但是,由于我是通过调用UIWebView的loadRequest在UIWebView中加载网页的,因此这些技术实际上并不适用。

关于如何将登录内容保存在网页中,以便再次启动应用程序以将我的登录内容保存在UIWebView页面中的任何想法,

该网址保存了登录内容: -http : //aubonpain.mloyalconnect.com/microsite/

document.querySelectorAll(“ input [type ='password']”)

对于所有textField,请使用:document.querySelectorAll(“ input [type ='text']”)

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

NSString *userName = @"xyz";
NSString *password = @"abc";

if (userName.length != 0 && password.length != 0) {


    NSString *jsPassword = [NSString stringWithFormat:@"document.querySelectorAll(\"input[type='password']\").value ='%@'", password];

    NSString *jsUsername = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                            for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", userName];

    [self.mywebView stringByEvaluatingJavaScriptFromString: jsUsername];
    [self.mywebView stringByEvaluatingJavaScriptFromString: jsPassword];
}

}

我在几个实时应用程序中使用了以下内容,它们运行良好。

得到回应后保存cookie

- (void)saveCookiesToDefaults
{
    NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
    [[NSUserDefaults standardUserDefaults] setObject:cookiesData forKey:@"SAVED_COOKIES"];
}  

加载cookie

- (void)loadCookies
{
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"SAVED_COOKIES"]];
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

    for (NSHTTPCookie *cookie in cookies)
    {
        NSMutableDictionary *newProperties = [[NSMutableDictionary alloc]initWithDictionary:cookie.properties];
        NSDate *date = [newProperties objectForKey:NSHTTPCookieExpires];
        if(date == nil)
            [newProperties setObject:[[NSDate date] dateByAddingTimeInterval:100*12*30*60*60] forKey:NSHTTPCookieExpires];
        else
            [newProperties setObject:[[NSDate date] dateByAddingTimeInterval:100*12*30*60*60] forKey:NSHTTPCookieExpires];
        NSHTTPCookie *newCookie = [NSHTTPCookie cookieWithProperties:newProperties];
        [cookieStorage setCookie: newCookie];
        NSLog(@"%@",newCookie);
    }
}  

从会话中清除Cookie

- (void)clearCookies
{
    for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
    {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }

    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"SAVED_COOKIES"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}  

检查会话是否存在

- (BOOL)hasExistingSession
{
    NSData *savedCookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"SAVED_COOKIES"];
    if(savedCookiesData == nil)
        return 0;
    else
    {
        NSArray *savedCookies = [NSKeyedUnarchiver unarchiveObjectWithData:savedCookiesData];

        for (NSHTTPCookie *cookie in savedCookies)
        {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }

        NSLog(@"HAS EXISTING SESSION: %@", savedCookies.count ? @"YES" : @"NO");

        return savedCookies.count;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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