码迷,mamicode.com
首页 > Web开发 > 详细

OC JS交互(通常情况下,如果网页写得正规的话,是可以正常交互的,之前就遇到后台写h5始终拿不到事件,元素也拿不到,更别说交互了,真是奇了怪了)

时间:2016-11-17 19:34:02      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:directory   元素   mozilla   根据id获取   dict   ble   out   ict   adr   

自动填充表单

@"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"];

 

OC JS交互(通常情况下,如果网页写得正规的话,是可以正常交互的,之前就遇到后台写h5始终拿不到事件,元素也拿不到,更别说交互了,真是奇了怪了)

标签:directory   元素   mozilla   根据id获取   dict   ble   out   ict   adr   

原文地址:http://www.cnblogs.com/songxing10000/p/6074511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!