使用JSPatch这么久了,一直都是使用http://jspatch.com/平台集成SDK来实现js文件下发和版本控制,简单便捷。事实却是如此,使用它我们可以不用为了下发js文件再去搭建服务器,也避免了在解决文件加密时的麻烦(在项目中我一直是这么用的)。但今天闲着没事,决定自己实现文件下发。
1:新建工程,从github下载JSPatch sdk集成到工程
2:没有服务器来存放js文件?那么就使用云服务吧,最便捷的当属七牛了(七牛的忠实用户),js内容如下:
require('UILabel');
defineClass('ViewController', {
testFunction: function() {
console.log('我是voidxin')
self.contentLabel().setText('我是voidxin, 我来自JSpatch');
},
});
目的是替换native的testFunction方法
3:怎么解决js文件的加密问题?那就用AES256加密js文件内容吧,读取的时候再用AES256解密。加密代码如下(加密秘钥是ab12):
新建NSData+AES256分类:
// NSData+AES256.h
// VXHotFix
//
// Created by voidxin on 16/12/7.
// Copyright © 2016年 wugumofang. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
@interface NSData (AES256)
-(NSData *) aes256_encrypt:(NSString *)key;
-(NSData *) aes256_decrypt:(NSString *)key;
@end
//
// NSData+AES256.m
// VXHotFix
//
// Created by voidxin on 16/12/7.
// Copyright © 2016年 wugumofang. All rights reserved.
//
#import "NSData+AES256.h"
@implementation NSData (AES256)
//加密
- (NSData *)aes256_encrypt:(NSString *)key
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
//解密
- (NSData *)aes256_decrypt:(NSString *)key
{
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode,
keyPtr, kCCBlockSizeAES128,
NULL,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
@end
新建NSString+AES256分类如下:
//
// NSString+AES256.h
// VXHotFix
//
// Created by voidxin on 16/12/7.
// Copyright © 2016年 wugumofang. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
#import "NSData+AES256.h"
@interface NSString (AES256)
-(NSString *) aes256_encrypt:(NSString *)key;
-(NSString *) aes256_decrypt:(NSString *)key;
@end
//
// NSString+AES256.m
// VXHotFix
//
// Created by voidxin on 16/12/7.
// Copyright © 2016年 wugumofang. All rights reserved.
//
#import "NSString+AES256.h"
@implementation NSString (AES256)
-(NSString *) aes256_encrypt:(NSString *)key
{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
NSData *result = [data aes256_encrypt:key];
if (result && result.length > 0) {
Byte *datas = (Byte*)[result bytes];
NSMutableString *output = [NSMutableString stringWithCapacity:result.length * 2];
for(int i = 0; i < result.length; i++){
[output appendFormat:@"%02x", datas[i]];
}
return output;
}
return nil;
}
-(NSString *) aes256_decrypt:(NSString *)key
{
NSMutableData *data = [NSMutableData dataWithCapacity:self.length / 2];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [self length] / 2; i++) {
byte_chars[0] = [self characterAtIndex:i*2];
byte_chars[1] = [self characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[data appendBytes:&whole_byte length:1];
}
NSData* result = [data aes256_decrypt:key];
if (result && result.length > 0) {
return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
}
return nil;
}
@end
注意:以上方法可在google搜到,我只是搬运工,但特别注意的是,在NSString+AES256中要去掉
const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding]; NSData *data = [NSData dataWithBytes:cstr length:self.length];
改成:
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
不然加密后解密时会丢失字段。
加密后的js文件内容是:(文件名是“hotfix2.js”,把加密后的文件放到七牛上)
a2c11fedca6537526520ffc507d13205a329d0157a182d60ec4955d36c0b7329e5d271e3b7feb4f9cbb4c7cff721e7c2ecb470413b24eb7c0752c281a7e2fd3f243eb18315b1cce5a4a8961508b13438db9b56326005e77235d79c0261e82f38192fe6eeba66fc2cead6807be3ef6a531522e9ee4479165c43f44da1f0430be3bd5a8db17ee30d27baf0c20670318df74482a2c7cffb7928b258c95db8a7430b40767eac42a54d215d8acc24bd0f87d4e80b1c33004ff2b41d0a74bc046d954708570d0113a5479eb64c965b1b0725a4c579f272583c9a296b719e6c7222c6059075f0d24c639893f01ae1791eb3ead2e7b0250075a6a8e4603f68da144443bdd87f28ff426c3aaa6dd0092d83b67b9e6db2b5a27fdd05a462b5a182ba26446e
4:怎么解决版本控制问题?新建appversion.txt文件,里面保存要修复目标版本的app版本号,然后存在七牛云,每次启动app时,读取appverson.txt文件中的内容,和app当前版本对比,版本号相同时则执行JS文件修复。
版本控制代码如下:
+ (void)checkVersion{
//获取服务器端控制的版本号和本地版本号对比
NSString *path = kVersion_URL;
NSString *content = [NSString stringWithContentsOfURL:[NSURL URLWithString:path] encoding:NSUTF8StringEncoding error:nil];
[content isEqualToString:[self _getterAppVersion]] ? ({
//版本号一致执行js代码
@try {
NSString *path = kFilePath_URL;
NSString *content = [NSString stringWithContentsOfURL:[NSURL URLWithString:path] encoding:NSUTF8StringEncoding error:nil];
//AES256解密
content = [content aes256_decrypt:kEnKey];
NSLog(@"解密后是:%@",content);
[JPEngine startEngine];
[JPEngine evaluateScript:content];
} @catch (NSException *exception) {
NSLog(@"加载jspatch文件出错");
} @finally {
}
}) : ({
//版本号不一致不执行js代码
});
}
+ (NSString *)_getterAppVersion{
//获取当前app版本号
NSDictionary *infoDic = [NSBundle mainBundle].infoDictionary;
NSString *app_version = [infoDic objectForKey:@"CFBundleShortVersionString"];
NSLog(@"app version:%@",app_version);
return app_version;
}
由此即可实现js文件的下发和版本控制并保证js文件的安全性
本文demo下载请跳转至:https://github.com/voidxin/VXHotFix