自动填充表单
@"document.getElementById('loginid').value = '这里填入你的用户名';document.getElementById('userpassword').value = '这里填入你的密码';";
根据ID拿到输入用户名的输入框并给值
根据ID拿到输入密码的输入框并给值
自动点击登录按钮
@"document.getElementById('login').click()"
根据ID获取登录按钮这个元素,并调用其点击事件click()
如果有特别限制的话,我们需要先访问一次特定网址,并存入cookie
NSURL * cookieHost = [NSURL URLWithString:@"http://mmmmmm/login/Login.jsp"]; NSHTTPCookie * cookie = [NSHTTPCookie cookieWithProperties: [NSDictionary dictionaryWithObjectsAndKeys: [cookieHost host],NSHTTPCookieDomain, [cookieHost path],NSHTTPCookiePath, @"COOKIE_NAME",NSHTTPCookieName, @"COOKIE_VALUE",NSHTTPCookieValue,nil]]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
退出登录里为了,不受其他干扰,要清掉cookie
NSHTTPCookieStorage * myCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie * cookie in [myCookie cookies]) { [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; } NSString * path = NSHomeDirectory(); NSString * filePath = [path stringByAppendingPathComponent:@"/Library/Cookies/Cookies.binarycookies"]; NSFileManager * manager = [NSFileManager defaultManager]; [manager removeItemAtPath:filePath error:nil];
开始发起请求,附带上cookie去请求,伪装成特定User-Agent,referer等
NSURL * url = [NSURL URLWithString:@"http://mmmmm/login/Login.jsp"]; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url]; NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]; [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36" forHTTPHeaderField:@"User-Agent"]; [request setValue:@"http://mmmmmm" forHTTPHeaderField:@"Origin"]; [request setValue:@"http://mmmmm/login/Login.jsp" forHTTPHeaderField:@"Referer"]; [request setHTTPMethod:@"GET"]; [request setHTTPShouldHandleCookies:YES]; [request setAllHTTPHeaderFields:headers];
如果要求在特定的网络环境下才能访问网址,就得处理判断了,当然这种情况下,只能受限了
NSString *ipString = [self deviceIPAdress][@"en1/ipv4"]; ipString = ipString ? ipString : [ self deviceIPAdress][@"en0/ipv4"]; if ([self canLoadNetFrom:ipString]) { [self.webView loadRequest:request]; } else { [SVProgressHUD showErrorWithStatus:@"当前网络环境不支持登录"]; }
更新:要切换不同账户密码怎么办,重新写程序?这里用Settings.bundle
然后直接用偏好去取就是了
NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; NSString *accout = [user stringForKey:@"name_preference"]; NSString *psd = [user stringForKey:@"psd_preference"];