diff --git a/CHANGELOG.md b/CHANGELOG.md
index dfbd921..e0f0518 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+# 0.3.2 (2021-04-09)
+* 实现ios端的`postException`,修复[#16](https://github.com/yz1311/rn-bugly/issues/16)
+* 添加`startCrashReport`、`closeCrashReport`、
+ `getCurrentTag`、`getUserData`、`getBuglyVersion`等接口
+
# 0.3.1 (2021-03-17)
* 由于jcenter仓库的问题,暂时固定`com.tencent.bugly:nativecrashreport`版本为`3.8.0`
diff --git a/README.md b/README.md
index da38bee..0571431 100644
--- a/README.md
+++ b/README.md
@@ -36,7 +36,7 @@ $ pod install
...
//初始化bugly,会自动读取info.plist中的参数
[RNBugly startWithAppId];
- return YES;
+ return YES;
}
```
`info.plist`文件读取SDK初始化参数,可配置的参数如下(`除Appid为必填外,其它可选`):
@@ -76,7 +76,7 @@ public void onCreate() {
//初始化并且自动检查更新
RNBuglyModule.init(getApplicationContext(),"注册时申请的APPID",false);
- //仅仅初始化(推荐使用该方法,所有的检查更新触发都都js端,更加灵活)
+ //仅仅初始化(推荐使用该方法,所有的检查更新触发都在js端,更加灵活)
RNBuglyModule.initWithoutAutoCheckUpgrade(getApplicationContext(),"注册时申请的APPID",false);
}
```
@@ -109,9 +109,27 @@ RNBugly.checkUpgrade({
//获取更新信息
//注意:更新信息获取一次后会储存在本地,即使后台控制停止该版本更新,该方法依旧可以获取到数据
let updateInfo = await RNBugly.getUpgradeInfo();
+
+
+//上报自定义异常信息
+RNBugly.postException({
+ errorMsg: 'login error',
+ stack: 'sadasdasd\nzxzxsds',
+ extraInfo: {
+ userInfo: 'test',
+ password: '12345'
+ }
+})
```
-具体方法请查看: [index.d.ts](./index.d.ts)
+bugly后端异常信息如下:
+
+![](https://tva1.sinaimg.cn/large/008eGmZEgy1gpdin9v721j30ww04imxk.jpg)
+
+
+![](https://tva1.sinaimg.cn/large/008eGmZEgy1gpdiomwgs7j30w6068aal.jpg)
+
+其他方法及注释请查看: [index.d.ts](types/index.d.ts)
参考[demo](./example),或者下载[example.apk](https://zhaoyang.lanzous.com/ib832sh)体验
diff --git a/android/src/main/java/com/reactlibrary/bugly/RNBuglyModule.java b/android/src/main/java/com/reactlibrary/bugly/RNBuglyModule.java
index 8a773ef..2f7a6c1 100644
--- a/android/src/main/java/com/reactlibrary/bugly/RNBuglyModule.java
+++ b/android/src/main/java/com/reactlibrary/bugly/RNBuglyModule.java
@@ -14,6 +14,7 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
+import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
@@ -22,7 +23,10 @@
import com.tencent.bugly.crashreport.CrashReport;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
+import java.util.Set;
public class RNBuglyModule extends ReactContextBaseJavaModule {
@@ -172,6 +176,38 @@ public void setUserSceneTag(int tagId) {
CrashReport.setUserSceneTag(getReactApplicationContext(), tagId);
}
+ @ReactMethod
+ public void startCrashReport() {
+ CrashReport.startCrashReport();
+ }
+
+ @ReactMethod
+ public void closeCrashReport() {
+ CrashReport.closeCrashReport();
+ }
+
+ @ReactMethod
+ public void getCurrentTag(final Promise promise) {
+ promise.resolve(CrashReport.getUserSceneTagId(getReactApplicationContext()));
+ }
+
+ @ReactMethod
+ public void getUserData(final Promise promise) {
+ Set keys = CrashReport.getAllUserDataKeys(getReactApplicationContext());
+ Iterator iterator = keys.iterator();
+ WritableMap map = Arguments.createMap();
+ while (iterator.hasNext()) {
+ String key = (String)iterator.next();
+ map.putString(key, CrashReport.getUserData(getReactApplicationContext(), key));
+ }
+ promise.resolve(map);
+ }
+
+ @ReactMethod
+ public void getBuglyVersion(final Promise promise) {
+ promise.resolve(CrashReport.getBuglyVersion(getReactApplicationContext()));
+ }
+
@ReactMethod
public void putUserData(String userKey, String userValue, Promise promise) {
try
diff --git a/index.js b/index.js
index a399d82..37a3d0b 100644
--- a/index.js
+++ b/index.js
@@ -7,7 +7,7 @@ export default {
/**
* 设置当前的用户id
* 精确定位到某个用户的异常
- * @param userId
+ * @param userId
*/
setUserId: function (userId) {
if(Platform.OS === 'android') {
@@ -27,14 +27,14 @@ export default {
if(Platform.OS === 'android') {
RNBugly.setAppChannel(appChannel);
} else {
-
+
}
},
setAppPackage: function (appPackage) {
if(Platform.OS === 'android') {
RNBugly.setAppPackage(appPackage);
} else {
-
+
}
},
setTag: function (tagId) {
@@ -44,13 +44,32 @@ export default {
RNBugly.setTag(tagId);
}
},
+ startCrashReport: function () {
+ if(Platform.OS === 'android') {
+ RNBugly.startCrashReport();
+ } else {
+
+ }
+ },
+ closeCrashReport: function () {
+ RNBugly.closeCrashReport();
+ },
+ getCurrentTag: function () {
+ return RNBugly.getCurrentTag();
+ },
+ getUserData: function () {
+ return RNBugly.getUserData();
+ },
+ getBuglyVersion: function () {
+ return RNBugly.getBuglyVersion();
+ },
/**
* 自定义Map参数可以保存发生Crash时的一些自定义的环境信息。在发生Crash时会随着异常信息一起上报并在页面展示。
* 最多可以有9对自定义的key-value(超过则添加失败);
* key限长50字节,value限长200字节,过长截断;
* key必须匹配正则:[a-zA-Z[0-9]]+。
- * @param {*} userKey
- * @param {*} userValue
+ * @param {*} userKey
+ * @param {*} userValue
*/
putUserData: function (userKey,userValue) {
return RNBugly.putUserData(userKey,userValue);
@@ -87,19 +106,27 @@ export default {
/**
* Android Only,打印日志
* 用户传入TAG和日志内容。该日志将在Logcat输出,并在发生异常时上报。
- * @param {*} level
- * @param {*} tag
- * @param {*} log
+ * @param {*} level
+ * @param {*} tag
+ * @param {*} log
*/
log: function (level, tag, log) {
if(Platform.OS === 'android') {
RNBugly.log(level,tag,log);
}
},
- //Android Only,主动上传日志
+ //主动上传日志
postException: function (params) {
- if(Platform.OS === 'android') {
- RNBugly.postException(params);
+ if(!params.errorType) {
+ params.errorType = 'React Native Exception';
+ }
+ if(!params.category) {
+ if(Platform.OS === 'android') {
+ params.category = 8;
+ } else {
+ params.category = 5;
+ }
}
+ RNBugly.postException&&RNBugly.postException(params);
}
};
diff --git a/ios/RNBugly.m b/ios/RNBugly.m
index 52be61b..b5b4b36 100644
--- a/ios/RNBugly.m
+++ b/ios/RNBugly.m
@@ -1,6 +1,7 @@
#import "RNBugly.h"
#import
+#import
@implementation RNBugly
@@ -34,10 +35,48 @@ + (void)startWithAppId
[Bugly setTag:tag];
}
+RCT_EXPORT_METHOD(closeCrashReport)
+{
+ [Bugly closeCrashReport];
+}
+
+RCT_EXPORT_METHOD(getCurrentTag:(RCTPromiseResolveBlock)resolve
+ reject:(RCTPromiseRejectBlock)reject)
+{
+ NSUInteger tag = [Bugly currentTag];
+ resolve([NSNumber numberWithUnsignedInteger:tag]);
+}
+
+RCT_EXPORT_METHOD(getUserData:(RCTPromiseResolveBlock)resolve
+ reject:(RCTPromiseRejectBlock)reject)
+{
+ NSDictionary* dict = [Bugly allUserValues];
+ resolve(dict);
+}
+
+RCT_EXPORT_METHOD(getBuglyVersion:(RCTPromiseResolveBlock)resolve
+ reject:(RCTPromiseRejectBlock)reject)
+{
+ resolve([Bugly sdkVersion]);
+}
+
RCT_EXPORT_METHOD(putUserData:(NSString *)userKey:(NSString *)userValue)
{
[Bugly setUserValue:userValue forKey:userKey];
}
+RCT_EXPORT_METHOD(postException:(NSDictionary *)params)
+{
+ NSInteger category = [RCTConvert NSInteger:[params objectForKey:@"category"]];
+ NSString *errorType = [RCTConvert NSString:[params objectForKey:@"errorType"]];
+ NSString *errorMsg = [RCTConvert NSString:[params objectForKey:@"errorMsg"]];
+ NSString *stack = [RCTConvert NSString:[params objectForKey:@"stack"]];
+ NSArray *stackTraceArray = [stack componentsSeparatedByString:@""];
+ NSDictionary *extraInfo = [RCTConvert NSDictionary:[params objectForKey:@"extraInfo"]];
+ if(extraInfo == nil) {
+ extraInfo = [NSMutableDictionary dictionary];
+ }
+ [Bugly reportExceptionWithCategory:category name:errorMsg reason:@" " callStack:stackTraceArray extraInfo:extraInfo terminateApp:NO];
+}
@end
-
+
diff --git a/package.json b/package.json
index 534983c..3f2ed25 100644
--- a/package.json
+++ b/package.json
@@ -1,9 +1,10 @@
{
"name": "rn-bugly",
- "version": "0.3.1",
+ "version": "0.3.2",
"description": "bugly for react-native,支持统计,android支持全量升级",
"main": "index.js",
+ "types": "./types/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
diff --git a/index.d.ts b/types/index.d.ts
similarity index 75%
rename from index.d.ts
rename to types/index.d.ts
index c133583..9159f38 100644
--- a/index.d.ts
+++ b/types/index.d.ts
@@ -2,7 +2,7 @@ declare module 'rn-bugly' {
/**
* 设置当前的用户id
* 精确定位到某个用户的异常
- * @param userId
+ * @param userId
*/
export function setUserId (userId: string): void;
@@ -13,15 +13,15 @@ declare module 'rn-bugly' {
export function setAppVersion (version: string): void;
/**
- * 设置渠道名称
- * @param appChannel
+ * Android Only,设置渠道名称
+ * @param appChannel
*/
export function setAppChannel (appChannel: string): void;
/**
* Android Only,设置自定义包名
* 默认读取AndroidManifest.xml中的Package信息
- * @param appPackage
+ * @param appPackage
*/
export function setAppPackage (appPackage: string): void;
@@ -29,19 +29,47 @@ declare module 'rn-bugly' {
* 自定义标签
* 用于标明App的某个“场景”。在发生Crash时会显示该Crash所在的“场景”,
* 以最后设置的标签为准,标签id需大于0。
- * @param tagId
+ * @param tagId
*/
export function setTag (tagId: number): void;
+ /**
+ * Android Only,开启CrashReport
+ */
+ export function startCrashReport (): void;
+
+ /**
+ * 关闭CrashReport
+ */
+ export function closeCrashReport (): void;
+
+ /**
+ * 获取当前设置标签
+ * @return 当前标签ID
+ */
+ export function getCurrentTag (): Promise;
+
+ /**
+ * 获取关键数据
+ * @return 关键数据
+ */
+ export function getUserData (): Promise>;
+
+ /**
+ * 获取SDK的版本
+ * @return sdk的版本
+ */
+ export function getBuglyVersion (): Promise;
+
/**
* 自定义Map参数可以保存发生Crash时的一些自定义的环境信息。在发生Crash时会随着异常信息一起上报并在页面展示。
* 最多可以有9对自定义的key-value(超过则添加失败);
* key限长50字节,value限长200字节,过长截断;
* key必须匹配正则:[a-zA-Z[0-9]]+。
- * @param params
+ * @param params
*/
export function putUserData (params:{userKey: string,userValue: string}): boolean;
-
+
/**
* 获取本地已有升级策略(非实时,可用于界面红点展示)
*/
@@ -111,14 +139,23 @@ declare module 'rn-bugly' {
/**
* Android Only,打印日志
* 用户传入TAG和日志内容。该日志将在Logcat输出,并在发生异常时上报。
- * @param {*} level
- * @param {*} tag
- * @param {*} log
+ * @param {*} level
+ * @param {*} tag
+ * @param {*} log
*/
export function log (level:logLevel,tag:string,log:string): void;
- export function postException (parmas:{category:number,errorType:string,errorMsg:string,stack?:string,extraInfo?:{[key:string]:string}}): void;
-
+ /**
+ * 上报自定义异常
+ * @param params
+ * @param category 类型 ios:(Cocoa=3,CSharp=4,JS=5,Lua=6) android:未知,可以不传
+ * @param errorType 自定义名称,可以不传
+ * @param errorMsg 名称
+ * @param stack 堆栈
+ * @param extraInfo 附加数据
+ */
+ export function postException (params:{category?:number,errorType?:string,errorMsg:string,stack?:string,extraInfo?:{[key:string]:string}}): void;
+
enum logLevel {
v = 'v',
d = 'd',
@@ -126,14 +163,14 @@ declare module 'rn-bugly' {
w = 'w',
e = 'e',
}
-
+
enum publishType {
//测试
test = 0,
//正式
production = 1
}
-
+
/**
* 升级策略
*/
@@ -145,4 +182,4 @@ declare module 'rn-bugly' {
//手工
manual = 3
}
-}
\ No newline at end of file
+}