2017.12技术点记录

2017-12-29  本文已影响24人  光彩影

每月一总结

前言:
12月是忙碌的一个月,每天任务安排很满,但是过得很充实,但也很有压力
/(ㄒoㄒ)/~~

本月总共有几个技术点:1.我的页面模板化,2.UIWebview与js交换 3.UIWebview登录与退出状态同步 4.一些复杂页面 5.抢购倒计时实现

总结比较有意义的技术点2与3.(uiwebview与js交互并账号登录与退出账号状态同步)

  1. 定义js交互接口。(js会调用我们定义的接口)
#import <JavaScriptCore/JavaScriptCore.h>

@protocol WebApiInvoker <JSExport>
//给web数据
-(void)loadApp;
- (void)openApp:(NSString *)json;

// type => none|share
- (void)showMenu:(NSString *)type;

@end
  1. 必需要在webView加载完才能js赋值
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.jsContext[@"app"] = self;
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息:%@", exceptionValue);
    };
}
  1. 执行js脚本语言方法
 [self.webView stringByEvaluatingJavaScriptFromString:@"方法名"];
  1. uiwebview登录状态保存

uiwebview登录状态保存,最好的方式是采用cookie保存。(在app启动的时候全局设置cookie如果需要保存,就全局保存)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSHTTPCookieStorage *cook = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    [cook setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
}
  1. uiwebview退出状态清除cookie
//cookie清除
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
    [storage deleteCookie:cookie];
}

本月需记住点:

  1. uitableview 里面的高度是先算高度,后赋值的。
  2. webview 加载urlString,必须先处理链接(1.链接去空格处理 2.链接url解码 3.链接url编码)
    self.mURLString = [self.mURLString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];//去掉空格

    //iOS中文网址路径转换URLEncode
    self.mURLString = [self.mURLString URLDecodedString];
    self.mURLString = [self.mURLString URLEncodedString];
    [MBAWebShareSheetManager manager].mLink = self.mURLString;
    NSURL *url = [NSURL URLWithString:self.mURLString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.mWebView loadRequest:urlRequest];
#import "NSString+URL.h"

@implementation NSString (URL)
/**
 *  URLEncode
 */
- (NSString *)URLEncodedString
{
    return [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

/**
 *  URLDecode
 */
-(NSString *)URLDecodedString
{
    NSString *decodedString = [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
    return decodedString;
}

@end
上一篇下一篇

猜你喜欢

热点阅读