Skip to content

Commit 27ef9dd

Browse files
author
xgf
committed
add readme
1 parent 6364d81 commit 27ef9dd

File tree

5 files changed

+101
-40
lines changed

5 files changed

+101
-40
lines changed
Binary file not shown.

HotFix/HotFix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// HotFix.h
3-
// FIX
3+
// HotFix
44
//
55
// Created by xgf on 2018/6/20.
66
// Copyright © 2018年 xgf. All rights reserved.

HotFix/HotFix.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// HotFix.m
3-
// FIX
3+
// HotFix
44
//
55
// Created by xgf on 2018/6/20.
66
// Copyright © 2018年 xgf. All rights reserved.

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# HotFix
2+
一种轻量级的可以通过苹果审核的热修复方案,可以替代JSPatch。
3+
4+
-----------------------------------
5+
6+
## 使用 Usage
7+
8+
- 1.App启动时,用同步的方式调用接口,从服务器请求下发的JavaScript字符串
9+
10+
```
11+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
12+
//sync downloading js here
13+
//App启动时,主动同步请求服务端修复脚本,并执行修复方案
14+
//do something else
15+
return YES;
16+
}
17+
```
18+
- 2.执行修复
19+
下载完成后,同步的方式执行修复:
20+
```
21+
[[HotFix shared] fix:js];
22+
```
23+
24+
两步合到一起:
25+
```
26+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
27+
//sync downloading js here
28+
//App启动时,主动同步请求服务端修复脚本,并执行修复方案
29+
//这个里的js应该是通过同步的方式请求接口得到的,如:
30+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://xxxx/hotfix?access_token=xxxx"]];//调用获取修复脚本的接口
31+
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
32+
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
33+
NSString *js = json[@"hotfix_js"];//这里只是举个例子
34+
if(js) {
35+
[[HotFix shared] fix:js];
36+
}
37+
//do something else
38+
return YES;
39+
}
40+
```
41+
42+
## 举个栗子🌰 For Example
43+
ViewController里有一个这样的调用,参数为nil时会导致崩溃。
44+
```
45+
@implementation ViewController
46+
47+
- (void)viewDidLoad {
48+
[super viewDidLoad];
49+
[self join:@"Steve" b:nil];
50+
}
51+
- (void)join:(NSString *)a b:(NSString *)b {
52+
NSArray *tmp = @[a,b,@"Good Job!"];
53+
NSString *c = [tmp componentsJoinedByString:@" "];
54+
printf("%s\n",[c UTF8String]);
55+
}
56+
57+
@end
58+
```
59+
我们从服务器下发这段脚本来修复这个闪退(替换join:b:这个方法):
60+
```
61+
"fixInstanceMethodReplace('ViewController', 'join:b:', function(instance, originInvocation, originArguments){ \
62+
if (!originArguments[0] || !originArguments[1]) { \
63+
console.log('nil goes here'); \
64+
} else { \
65+
runInvocation(originInvocation); \
66+
} \
67+
});"
68+
```
69+
App重新启动的时候,会以同步的方式加载到该脚本,并执行修复:
70+
```
71+
[[HotFix shared] fix:js];
72+
```
73+
这样原来的jion:b:方法就会被替换,当参数为nil时,就会打印`nil gose here`,若部位nil则正常执行。这样崩溃就解决了~
74+
75+
## 安装 Installation
76+
77+
```
78+
pod repo update
79+
pod `HotFix`
80+
```
81+
82+
- 更多信息请参考该[链接](http://limboy.me/tech/2018/03/04/ios-lightweight-hotfix.html)

main/AppDelegate.m

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ @implementation AppDelegate
1717

1818

1919
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
20-
//sync downloading js here
21-
//App启动时,主动同步请求服务端修复脚本,并执行修复方案
20+
/*sync downloading js here
21+
App启动时,主动同步请求服务端修复脚本,并执行修复方案
22+
这个里的js应该是通过同步的方式请求接口得到的,如
23+
调用获取修复脚本的接口
24+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://xxxx/hotfix?access_token=xxxx"]];
25+
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
26+
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
27+
NSString *js = json[@"hotfix_js"];
28+
if(js) {
29+
[[HotFix shared] fix:js];
30+
}*/
31+
//这是同步下载得到的js脚本字符串,假设我们从服务器获得了这样一个脚本,用于修复ViewController的join:b:方法由于nil导致崩溃。
2232
NSString *js = @"fixInstanceMethodReplace('ViewController', 'join:b:', function(instance, originInvocation, originArguments){ \
23-
console.log(instance);\
24-
console.log(originInvocation);\
25-
console.log(originArguments);\
26-
if (!originArguments[0] || !originArguments[1]) { \
27-
console.log('nil goes here'); \
28-
} else { \
29-
runInvocation(originInvocation); \
30-
} \
33+
if (!originArguments[0] || !originArguments[1]) { \
34+
console.log('nil goes here'); \
35+
} else { \
36+
runInvocation(originInvocation); \
37+
} \
3138
});";
3239
if(js) {
3340
[[HotFix shared] fix:js];
@@ -36,32 +43,4 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
3643
return YES;
3744
}
3845

39-
40-
- (void)applicationWillResignActive:(UIApplication *)application {
41-
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
42-
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
43-
}
44-
45-
46-
- (void)applicationDidEnterBackground:(UIApplication *)application {
47-
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
48-
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
49-
}
50-
51-
52-
- (void)applicationWillEnterForeground:(UIApplication *)application {
53-
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
54-
}
55-
56-
57-
- (void)applicationDidBecomeActive:(UIApplication *)application {
58-
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
59-
}
60-
61-
62-
- (void)applicationWillTerminate:(UIApplication *)application {
63-
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
64-
}
65-
66-
6746
@end

0 commit comments

Comments
 (0)