需求須知:公司要將微信公眾號里面的H5寫成自家app ,當然首選使用wkwebview浓冒。我們的HTML是之前老員工寫的離職后是一個小哥接手,這次寫這個我們兩個要密切配合才行捅位。(對于大神來說不用看了,直接給我寫正確的方法吧,不然巫了你的眼)昌粤。
問題:保持登錄
方案一:使用oc回調js吧access_token傳過去,后臺那邊自己保存判斷使用啄刹,(當然這個首先后臺那邊知道如何寫)我傳過去了他不知道雜用很郁悶涮坐,簡單說一下方法:
oc代碼:
// NSString *jsStr =[NSString stringWithFormat:@"tokenResult('%@')",@"access_token"];
//? ? [webView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
//? ? ? ? NSLog(@"==%@----%@",result,error);
//? ? }];
h5代碼:
function tokenResult(str){
? ? ? ? if(str == 'null'){
? ? ? ? ? ? alert(str);//這個str就是access_token
? ? ? ? } else {
? ? ? ? ? ? alert(str);
? ? ? ? ? ? window.webkit.messageHandlers.timeout.postMessage({body: ''});//這個是調取我這邊的方法js調用oc
? ? ? ? }
? ? }
方案2:OC端每個請求后面拼接上access_token,
聲明一些屬性后面要用到
返回按鈕的點擊事件里面的代碼
截取URL拼接方法:
url截取的代碼
#pragma mark = WKNavigationDelegate
// 在發(fā)送請求之前誓军,決定是否跳轉
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
? ? NSLog(@"%@",navigationAction.request.URL.absoluteString);
? ? //允許跳轉
? ? //decisionHandler(WKNavigationActionPolicyAllow);
? ? //不允許跳轉
? ? //decisionHandler(WKNavigationActionPolicyCancel);
? ? NSString *hostname = navigationAction.request.URL.host.lowercaseString;
? ? NSLog(@"是否跨平臺%@",hostname);
//? ? if (navigationAction.navigationType == WKNavigationTypeLinkActivated
//? ? ? ? && ![hostname containsString:@"m.shaimobao.com"]) {
//? ? ? ? // 對于跨域袱讹,需要手動跳轉
//? ? ? ? [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
//
//? ? ? ? // 不允許web內跳轉
//? ? ? ? decisionHandler(WKNavigationActionPolicyCancel);
//? ? } else {
//? ? ? ? //self.progresslayer.alpha = 1.0;
//? ? ? ? decisionHandler(WKNavigationActionPolicyAllow);//允許跳轉
//? ? }
#pragma mark - 截取連接并保存返回連接方法開始
? ? if (self.isbackbool) {//用戶點擊了返回按鈕返回YES
? ? ? ? decisionHandler(WKNavigationActionPolicyAllow);
? ? ? ? self.isbackbool = NO;
? ? }else{//用戶沒有點擊返回返回NO
? ? ? ? NSURL *URL = navigationAction.request.URL;
? ? ? ? MBLog(@"%@", URL);
? ? ? ? NSString * strurl = [NSString string];
? ? ? ? if (![[NSString stringWithFormat:@"%@",URL] isEqualToString:[NSString stringWithFormat:@"http://"]]) { // 不是主頁 加載
? ? ? ? ? ? if (![[NSString stringWithFormat:@"%@",URL] isEqualToString:[NSString stringWithFormat:@"%@",self.currentUrl]]) {
? ? ? ? ? ? ? ? NSString *string = [NSString string];
? ? ? ? ? ? ? ? if ([[NSString stringWithFormat:@"%@",URL] containsString:@"access_token"]) {
? ? ? ? ? ? ? ? ? ? NSLog(@"women 包含 bitch");
? ? ? ? ? ? ? ? ? ? string = [NSString stringWithFormat:@"%@",URL];
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? NSLog(@"women 不存在 bitch");
? ? ? ? ? ? ? ? ? ? string =[NSString stringWithFormat:@"%@&access_token=%@",URL,[self.userdic objectForKey:@"access_token"]];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:string]];
? ? ? ? ? ? ? ? navigationAction = navigationAction;
? ? ? ? ? ? ? ? [webView loadRequest:request];
? ? ? ? ? ? ? ? self.currentUrl = URL;
? ? ? ? ? ? ? ? strurl = [NSString stringWithFormat:@"%@",URL];
? ? ? ? ? ? ? ? [self urlarrayisboolURL:strurl];
? ? ? ? ? ? ? ? decisionHandler(WKNavigationActionPolicyCancel); // 必須實現(xiàn) 取消加載 不然會加載2遍
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? self.currentUrl = URL;
? ? ? ? ? ? ? ? decisionHandler(WKNavigationActionPolicyAllow); // 必須實現(xiàn) 加載
? ? ? ? ? ? ? ? strurl = [NSString stringWithFormat:@"%@",URL];
? ? ? ? ? ? ? ? [self urlarrayisboolURL:strurl];
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? self.currentUrl = URL;
? ? ? ? ? ? decisionHandler(WKNavigationActionPolicyAllow); // 必須實現(xiàn) 加載
? ? ? ? ? ? return;
? ? ? ? }
? ? }
#pragma mark - 截取連接并保存返回連接方法結束
}
-(void)urlarrayisboolURL:(NSString *)strurl{
? ? //保存請求的連接按順序保存
? ? //首先判斷數(shù)組中是否有該鏈接(只保存一次)
? ? BOOL isbool = [_urlArray containsObject: strurl];
? ? if (isbool) {//存在
? ? }else{//不存在
? ? ? ? //存進去
? ? ? ? if ([[NSString stringWithFormat:@"%@",strurl] containsString:@"access_token"]) {
? ? ? ? ? ? [self.urlArray addObject:strurl];
? ? ? ? } else {
? ? ? ? ? ? //NSLog(@"沒有拼接的參數(shù)");
? ? ? ? }
? ? }
}
返回按鈕代碼
-(void)goback{
//? ? if ([webView canGoBack]) {
//? ? ? ? [webView goBack];
//? ? ? ? NSLog(@"back");
//? ? }
? ? if (self.urlArray.count < 1||self.urlArray.count == 1) {
? ? ? ? self.isbackbool = NO;
? ? }else{
? ? ? ? self.isbackbool = YES;//表示用戶點擊了返回按鈕此時不應該在添加鏈接
? ? ? ? NSURL * url = [NSURL URLWithString:self.urlArray[self.urlArray.count - 2]];
? ? ? ? [webView loadRequest:[NSURLRequest requestWithURL:url]];
? ? ? ? [self.urlArray removeLastObject];
? ? }
? ? //? ? /*A*網(wǎng)頁操控相關方法**/
? ? //? ? [self.webView goBack];? ? // 回退
? ? //? ? [self.webView goForward];? //前進
? ? //? ? [self.webView reload];? ? ? //刷新
? ? //設置是否能夠前進和回退
? ? //? ? self.goBackBtn.enabled = webView.canGoBack;
? ? //? ? self.fowardBtn.enabled = webView.canGoForward;
}
實現(xiàn)思路說明:
1.在 【在發(fā)送請求之前,決定是否跳轉decidePolicyForNavigationAction 】的方法中獲取到用戶點擊的當前連接
2.判斷當前連接是否已經(jīng)拼接access_token昵时,拼接之后保存當前self.currentUrl連接捷雕,然后再次發(fā)[webView loadRequest:request];重新加載,(為了防止循環(huán)壹甥,我們要判斷救巷,當前連接和將要加載的連接是否一樣,要是一樣就不用再次加載句柠,不一樣就再次loadRequest)浦译。
3.重新加載的時候將當前正在加載的URL保存到self.urlArray數(shù)組中,為防止重復添加需要做一下判斷溯职,保證數(shù)組中的元素是唯一的(urlarrayisboolURL:(NSString *)strurl)精盅。此時正面加載已經(jīng)沒有問題了。
4.返回上一個界面的的思路谜酒,定義一個bool值叹俏,yes為用戶點擊了返回按鈕(返回的時候沒有必要再截取URL)所以在【在發(fā)送請求之前,決定是否跳轉decidePolicyForNavigationAction?】方法中判斷一下甚带。要是self.isbackbool為yes就直接WKNavigationActionPolicyAllow實現(xiàn)她肯,不然就截取保存佳头。
5.返回按鈕中實現(xiàn)思路:首先去掉返回首頁的情況或是首頁的情況(及判斷數(shù)組中連接的個數(shù))。再次就是返回獲取數(shù)組中倒數(shù)第二個元素(self.urlArray[self.urlArray.count - 2])就是上一個界面的連接晴氨,最后重新加載完成康嘉。