近期 做些准备性得工作和有意思的事情。所以近期做了一个适合ios和android 错误信息捕捉的unity插件。

两个功能,app崩溃也就是闪退 是开发人员 非常头疼的一件事,还有就是一些莫名得错误 有时候也会困扰着我们。如今,unity已经封装得挺好了,及时出现数组越界,和空对象这样严重得错误也不会崩溃。听着挺好。可是这给开发人员带了非常多烦恼啊。由于有时候可能出错了 你要跟就不知道 ,在什么地方出得错误啊。所以我们要想办法去解决问题。

我们都知道及时app崩溃,事实上后台还是在执行得 仅仅只是是 到了还有一个线程去处理崩溃得一些问题。那好我们就能够去捕捉到错误,供我们去理解这个问题。

首先 我们先针对 ios闪退得错误得问题。我们知道 如今ios还是才去得OC编程,身为一个编译性得语言,一般非常小得一个错误都会造成整个app得崩溃。

接下来 我们说下原理。 对于 IOS的机制是。它专门有一个类来处理异常错误。

那就是NSException类。来处理各种错误得一个类。我们要的就是这个类中得一个通知,就是 在app出现异常崩溃的事后会通知得一个方法C语言的NSSetUncaughtExceptionHandler。我们就是用这种方法来进行 异常注冊。

 NSSetUncaughtExceptionHandler(&caughtExceptionHandler);

当中  caughtExceptionHandler是自己写得类中得一个C语言得方法。

void caughtExceptionHandler(NSException *e){}

能够看到 在这种方法中我们就能够拿到NSException得错误得指针。

就能够得到全部得错误得信息。

这里知道了一半。信息我们是得到了,可是app如今是处于崩溃中,我们想让我们的错误信息传到server或者返回给开发人员得处理平台。那我们得线程必须等待才干够。这样才干给我们时间把错误信息上传到处理平台。

那怎么才干把线程堵塞调那 正好 ios有线程进入等待得方法CFRunLoopRunInMode()

好了想好这个多 我们開始 进行实施了。

这是 异常获取得方法,把此方法 进行注冊 也就是

-(void) _registerException{
NSSetUncaughtExceptionHandler(&caughtExceptionHandler);
}

void caughtExceptionHandler(NSException *e){
NSString *currentTime = [[VKCatchCrash shareCatchCrash] getCurrentTime];
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop); NSArray *arr = [e callStackSymbols];
NSString *reason = [e reason];
NSString *name = [e name]; NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setValue:[[VKCatchCrash shareCatchCrash] _getDeviceInfo] forKey:@"device"];
[dic setValue:arr forKey:@"callStackSymbols"];
[dic setValue:reason forKey:@"reason"];
[dic setValue:name forKey:@"name"];
[dic setObject:currentTime forKey:@"time"]; NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
options:NSJSONWritingPrettyPrinted
error:&error];
if ([jsonData length] > 0 && error == nil){
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *path = [[VKCatchCrash shareCatchCrash] fullScreenshots:currentTime]; //if([[VKCatchCrash shareCatchCrash] checkNetwork]){
[Upload startUpload:[Upload initUpBackBlock:^(NSData *data, NSURLResponse *response, NSError *error) {
NSFileManager *manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:path]){
[manager removeItemAtPath:path error:nil];
}
} upUrl:HOST upDelegate:nil formName:@"fileField" filePath:path contentKey:[[NSArray alloc] initWithObjects:@"bug", nil] contentValue:[[NSArray alloc] initWithObjects:jsonString, nil]]];
// }
NSLog(@"%@",jsonString);
[[VKCatchCrash shareCatchCrash] performSelectorOnMainThread:@selector(alertUploadBug) withObject:nil waitUntilDone:YES]; while (!dismiss)
{
for (NSString *mode in (__bridge NSArray *)allModes)
{
CFRunLoopRunInMode((__bridge CFStringRef)mode, 0, false);
}
}
CFRelease(allModes);
NSSetUncaughtExceptionHandler(NULL);
}else{
NSLog(@"nil");
}
}

警告框提示app闪退。

-(void)alertUploadBug{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"产生莫名得崩溃,报告正在发送server。" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}

当前时间

-(NSString *)getCurrentTime{
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString * locationString=[dateformatter stringFromDate:senddate];
return locationString;
}

这里还用到了一个 自己写的额一个上传得类VKHttpManager,回来我会上传 源代码。

基本信息就这么多。详情能够看下 源代码。

以下我们 我们来说下Android得闪退得异常捕获。

Android相对于ios比較简单,仅仅要使用一个类来接口UncaughtExceptionHandler就能够了

然后有借口得方法,会自己主动调用该回调。

public class MyCrashHandler  implements UncaughtExceptionHandler{
@Override
public void uncaughtException(Thread thread, Throwable ex) {
}
}

当中 Throwable得异常对象就是我们须要得异常对象。

当中这里也有 和ios相似得地方 那就是 线程堵塞

Looper.prepare();

Looper.loop();

这里有一个地方要注意得就是 在安卓中 unity调用 AndroidUI得东西 要使用 UI线程不然是无法显示。

详情见以下得源代码

UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
@Override
public void run() { }
}。

然后 unity里我们还要做些东西来捕获unity得异常和对ios和Android进行对接得一些东西。

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class CatchCrash : MonoBehaviour { private static string HOST ="http://192.168.1.240/game/";
private static string currentTime = ""; #if UNITY_IPHONE
[DllImport ("__Internal")]
private static extern void registerException (string host);
[DllImport ("__Internal")]
private static extern void createBug ();
[DllImport ("__Internal")]
private static extern void unityBugAlert (string msg);
[DllImport ("__Internal")]
private static extern string getDeviceInfo (); #elif UNITY_ANDROID
AndroidJavaClass jc = null;
AndroidJavaObject jo = null;
#endif
// Use this for initialization
void Awake () { #if UNITY_EDITOR #elif UNITY_IPHONE || UNITY_ANDROID
DontDestroyOnLoad (gameObject);
Application.RegisterLogCallback(OnLog);
#endif #if UNITY_IPHONE
registerException (HOST);
#elif UNITY_ANDROID
jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("registerException",HOST);
#endif
} void OnLog (string message, string stacktrace, LogType type)
{
if (type == LogType.Exception)
{
currentTime = getcurrentTime().ToString();
string butMsg = "{\n"+
"\"message\":" +"\""+message.Replace("\n","")+"\""+
",\n\"stacktrace\":"+"\""+stacktrace.Replace("\n","")+"\""+
",\n\"time\":"+"\""+currentTime+"\""
+"\n" +
"\"device\":" +"\""+
#if UNITY_IPHONE
getDeviceInfo().Replace("\n","")+"\""+
#elif UNITY_ANDROID
jo.CallStatic<string>("getDeviceInfo").Replace("\n","")+"\""+
#endif
"\n}";
StartCoroutine(uploadBug(butMsg));
#if UNITY_IPHONE
unityBugAlert (butMsg);
#elif UNITY_ANDROID
jo.CallStatic("unityBugAlert",butMsg);
#endif
}
} IEnumerator uploadBug(string butMsg){
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D (width,height,TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
byte [] bytes = tex.EncodeToPNG (); WWWForm form = new WWWForm();
form.AddField ("bug",butMsg);
form.AddBinaryData ("fileField",bytes,currentTime+".png","image/png"); WWW w = new WWW (HOST,form);
yield return w;
if (w.error != null) {
Debug.Log (" upload bug erro");
} else {
Debug.Log (" upload bug finish");
}
} public static string getcurrentTime()
{
System.DateTime now = System.DateTime.Now;
return now.Year + "-" + now.Month + "-" + now.Day + " " + now.Hour + ":" + now.Minute + ":" + now.Second;
}
}

project再次哦

(插播)unity的 异常捕捉和 ios Android 崩溃信息的捕捉。的更多相关文章

  1. WEB通知和React Native之即时通讯(iOS Android)

    WEB通知和React Native之即时通讯(iOS Android) 一,需求分析 1.1,允许服务器主动发送信息给客户端,客户端能监听到并且能接收. 1.2,为了方便同一个系统内的用户可以指定某 ...

  2. 常用获取Android崩溃日志和IOS崩溃日志的几种方法

    一:前言 在日常测试app时,经常会遇到崩溃问题,测试快速抓取到崩溃日志可以有效方便开发进行定位,快速解决问题所在测试做到测试分析,定位是非常重要的,这也是判断一个测试能力指标的一大维度. 二:And ...

  3. 封装 React Native 原生组件(iOS / Android)

    封装 React Native 原生组件(iOS / Android) 在 React Native中,有很多种丰富的组件了,例如 ScrollView.FlatList.SectionList.Bu ...

  4. Xamarin体验:使用C#开发iOS/Android应用

    Xamarin是Mono创始人Miguel de Icaza创建的公司,旨在让开发者可以用C#编写iOS, Android, Mac应用程序,也就是跨平台移动开发.   简介 Xamarin是基于Mo ...

  5. Ionic中使用Chart.js进行图表展示以及在iOS/Android中的性能差异

    Angular Chart 简介 在之前的文章中介绍了使用 Ionic 开发跨平台(iOS & Android)应用中遇到的一些问题的解决方案. 在更新0.1.3版本的过程中遇到了需要使用图表 ...

  6. iOS/Android 浏览器(h5)及微信中唤起本地APP

    在移动互联网,链接是比较重要的传播媒质,但很多时候我们又希望用户能够回到APP中,这就要求APP可以通过浏览器或在微信中被方便地唤起. 这是一个既直观又很好的用户体验,但在实现过程中会遇到各种问题: ...

  7. fir.im Weekly - iOS/Android 应用程序架构解析

    假如问你一个iOS or Android app的架构,你会从哪些方面来说呢? 本期 fir.im Weekly 收集了关于  iOS/Android 开发资源,也加入了一些关于 Web 前端方面的分 ...

  8. 【原】常见CSS3属性对ios&android&winphone的支持

    2个月前,我在博文<webapp开发中兼容Android4.0以下版本的css hack>中写过“那对于做移动网页开发的同事来说,一般只要做好webkit内核浏览器的展现效果就行了” ,在 ...

  9. iOS / Android 移动设备中的 Touch Icons

    上次转载了一篇<将你的网站打造成一个iOS Web App>,但偶然发现这篇文章的内容有些是错误的——准确来说也不是错误,只是不适合自半年前来的情况了(也可以说是iOS7 之后的时间)—— ...

随机推荐

  1. 很多人都在埋怨没有遇到好的团队,但好的团队不可能凭空出现,一流的团队不能仅靠团队成员努力,作为Leader,要有可行的规划,并坚定地执行、时势地调整(转)

    <西游记>中的唐僧团队历经千难万险,终于求得真经,目标明确.分工合理为这支队伍最终走向成功奠定了基础.唐僧从一开始,就为这个团队设定了西天取经的目标,虽然经历各种挫折与磨难,但目标从未动摇 ...

  2. bootstrap在 刷新页面,tab选择页面不会改变。

    您可以直接复制代码 注意在同级别文件夹中引用 相应js 和 css. 实现tab影响 关键看bootstrap的 data-toggle= tab <html lang="en&quo ...

  3. Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)

    集合操作符UNION/UNION ALLINTERSECTMINUS Union All不排序,不去重,其余均升序且去重.create table e1 as select * from emp wh ...

  4. arcmap坐标点生成线和面(更正版)

    一:本博客的脉络 (1 )做了例如以下更正:之前在网上搜到的结果是:arcmap坐标点生成线和面 ------ 注意该功能在ArcGIS10中没有了,当时自己也没有多想就转载了,再此做一下更正或者叫做 ...

  5. C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!!

    原文:C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!! 一般我们在开发Winodws Phone APP 的时候往 ...

  6. 玩转Web之servlet(四)---B/S是如何使用http协议完成通信过程的

    在上一篇文章中,我简单的说了一下B/S架构的流程图,关于浏览器和服务器之间的通信过程知识含糊的说了一下,在这篇文章中我再总结一下B/S架构里是如何利用http协议去完成通信的. (一)通讯过程 1:浏 ...

  7. yum使用总结(转)

    安装一个软件时yum -y install httpd安装多个相类似的软件时yum -y install httpd*安装多个非类似软件时yum -y install httpd php php-gd ...

  8. Android Widget 小部件(一) 简单实现

    在屏幕上加入Widget:或长按屏幕空白处,或找到WidgetPreview App选择. 原生系统4.0下面使用长按方式,4.0及以上 打开WIDGETS 创建Widget的一般步骤: 在menif ...

  9. CSDN 夏令营课程 项目分析

    主题如以下: 正确改动后的程序: #include <iostream.h> //using namespace std; class BASE { char c; public: BAS ...

  10. MyBatis系列教程(六)-- 与Spring综合(Integrate with Spring)

    其它工具或技术需要使用: 项目管理工具 : Maven 前台WEB图库:JSP 其他框架:Spring, Spring MVC 数据库 : Derby Maven的Web项目 Maven Depend ...