However, because I am loading the web page in UIWebView by calling UIWebView's loadRequest, those techniques are not really applicable.
Any ideas how I can save the login content in web page so that on again app launch to save my login content in UIWebView page,
This url save login content:- http://aubonpain.mloyalconnect.com/microsite/
document.querySelectorAll("input[type='password']")
For all textField use: 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];
}
}
I am using the following in couple of my live apps and they work quite well.
Save cookies when you get response :
- (void)saveCookiesToDefaults
{
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
[[NSUserDefaults standardUserDefaults] setObject:cookiesData forKey:@"SAVED_COOKIES"];
}
Load cookies :
- (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);
}
}
Clear cookies from session :
- (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];
}
Check if session exists :
- (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;
}
}
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.