AppDelegate

一.储存崩溃日志

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);

}
#pragma mark - 储存崩溃日志

void UncaughtExceptionHandler(NSException *exception) {

NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息

NSString *reason = [exception reason];//非常重要,就是崩溃的原因

NSString *name = [exception name];//异常类型

NSString *content = [NSString stringWithFormat:@"exception type : %@ n crash reason : %@ n call stack info : %@", name, reason, arr];

NSLog(@"exception type : %@ n crash reason : %@ n call stack info : %@", name, reason, arr);

//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

//    [userDefaults setObject:content forKey:@"content"];

//    [userDefaults synchronize];

// 可以在上传时使用当前的系统事件作为文件名

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

// 设置时间格式

formatter.dateFormat = @"yyyyMMddHHmmss";

NSString *timeStr = [formatter stringFromDate:[NSDate date]];

NSString *nameStr = [NSString stringWithFormat:@"%@iOSException.txt", timeStr];

//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

//    NSString *str = [userDefaults objectForKey:@"content"];

//NSString *eStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//

//    NSLog(@"%@",str);

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *path=[paths    objectAtIndex:0];

NSLog(@"path = %@",path);

//NSString *filename=[path stringByAppendingPathComponent:@"exception.plist"];

NSString *filename=[path stringByAppendingPathComponent:nameStr];

NSFileManager* fm = [NSFileManager defaultManager];

[fm createFileAtPath:filename contents:nil attributes:nil];

//NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];

//创建一个dic,写到plist文件里

//NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:str,@"exception",nil];

//[dic writeToFile:filename atomically:YES];

[content writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:nil] ;

/**

*  把异常崩溃信息发送至开发者邮件

*/

NSMutableString *mailUrl = [NSMutableString string];

[mailUrl appendString:@"mailto:1178638897@qq.com"];

[mailUrl appendString:@"?subject=程序异常崩溃,请配合发送异常报告,谢谢合作!"];

[mailUrl appendFormat:@"&body=%@", content];

// 打开地址

NSString *mailPath = [mailUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailPath]];

}


二.获取devicetoken方法

#warning 将得到的deviceToken传给SDK

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

方法1:

NSString *token =

[[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"

withString:@""]

stringByReplacingOccurrencesOfString:@">"

withString:@""]

stringByReplacingOccurrencesOfString:@" "

withString:@""];


方法1获取不到时可以用方法2

方法2:

NSMutableString *deviceTokenString1 = [NSMutableString string];

const char *bytes = (char*)[deviceToken bytes];

int iCount = deviceToken.length;

for (int i = 0; i < iCount; i++) {

[deviceTokenString1 appendFormat:@"%02x", bytes[i]&0x000000FF];

}

NSLog(@"My dvsToken is %@",deviceTokenString1);

}

三.NSUserDefaults储存数据

NSUserDefaults *userde = [NSUserDefaults standardUserDefaults];

NSDictionary *userDic = [userde objectForKey:@"UserInfoDic"];

NSString *selfStr = [userDic objectForKey:@"user_id"];

NSString *APNSStr = [NSString stringWithFormat:@"APNSArr%@",selfStr];

id notificationarr = [userde objectForKey:APNSStr];

NSMutableArray *APNSArr = [[NSMutableArray alloc]init];

if ([notificationarr isKindOfClass:[NSArray class]])

{

APNSArr = [NSMutableArray arrayWithArray:notificationarr];

} else {

}

NSDate *date = [NSDate date];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

NSString *DateTime = [formatter stringFromDate:date];

NSData *modelData = [NSKeyedArchiver archivedDataWithRootObject:[GPLModel baseModelWithText:text.content orderId:gplorderidStr nickname:gplnameStr userid:gpluseridStr time:DateTime]];

[APNSArr addObject:modelData];
**NSUserDefaults不能储存可变数组,需要先转成NSArray**

NSArray *arrAY = [NSArray arrayWithArray:APNSArr];

[userde setObject:arrAY forKey:APNSStr];

[userde synchronize];
**model需要进行编码和解码**
#import "GPLModel.h"

@implementation GPLModel

- (instancetype)initWithText:(NSString *)text orderId:(NSString *)orderIdStr  nickname:(NSString*)nicknameStr userid:(NSString*)useridStr time:(NSString*)timeStr

{

if (self==[super init]) {

_textStr = [text copy];

_orderId = [orderIdStr copy];

_nickname = [nicknameStr copy];

_userid = [useridStr copy];

_time = [timeStr copy];

}

return self;

}

+ (instancetype)baseModelWithText:(NSString *)text orderId:(NSString *)orderIdStr  nickname:(NSString*)nicknameStr userid:(NSString*)useridStr time:(NSString*)timeStr

{

return [[self alloc] initWithText:text orderId:orderIdStr  nickname:nicknameStr userid:useridStr time:timeStr];

}

-(void)encodeWithCoder:(NSCoder *)aCoder

{

[aCoder encodeObject:_textStr forKey:@"textStr"];

[aCoder encodeObject:_orderId forKey:@"orderId"];

[aCoder encodeObject:_nickname forKey:@"nickname"];

[aCoder encodeObject:_userid forKey:@"userid"];

[aCoder encodeObject:_time forKey:@"time"];

}

-(id)initWithCoder:(NSCoder *)aDecoder

{

if (self=[super init]) {

_textStr= [aDecoder decodeObjectForKey:@"textStr"];

_orderId= [aDecoder decodeObjectForKey:@"orderId"];

_nickname= [aDecoder decodeObjectForKey:@"nickname"];

_userid= [aDecoder decodeObjectForKey:@"userid"];

_time= [aDecoder decodeObjectForKey:@"time"];

}

return self;

}

@end

四.获取当前最顶层的ViewController

#pragma mark 获取当前最顶层的ViewController

- (UIViewController *)topViewController {

UIViewController *resultVC;

resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];

while (resultVC.presentedViewController) {

resultVC = [self _topViewController:resultVC.presentedViewController];

}

return resultVC;

}

- (UIViewController *)_topViewController:(UIViewController *)vc {

if ([vc isKindOfClass:[UINavigationController class]]) {

return [self _topViewController:[(UINavigationController *)vc topViewController]];

} else if ([vc isKindOfClass:[UITabBarController class]]) {

return [self _topViewController:[(UITabBarController *)vc selectedViewController]];

} else {

return vc;

}

return nil;

}

推荐阅读更多精彩内容

  • 1、出租车上,司机突然问我:“小姑娘,你有男朋友吗?” 我玩手机的手顿时一抖,有点慌了!但还是强装镇定,说:“没呢...
    图南未央阅读 14,662评论 33 320
  • 大家好,我是小瓶子,一个软件资源分享者。今天给大家推荐是一些提高生活质量和效率的网站,相信我,看完你肯定会有收获!...
    驻地氧气瓶阅读 11,701评论 3 256
  • 本人今年32岁,马上33岁了,现在这个年龄可以说是男人的黄金年龄段,是事业和家庭双丰收的年纪,而我却不太“...
    早已忘了初心阅读 6,172评论 90 131
  • 一直都有个习惯,就是每天都会练半小时的字。这个习惯坚持了很多年,现在呢,又有了一个新的习惯,就是每天的日更,做这些...
    诗与泡面阅读 1,319评论 3 93
  • 和身边的朋友交流自己最近的写作心得,以及一些自媒体的运营思路,有人立马出钱让我给他写文案,一百元一篇,我拒绝了。 ...
    丰盛圆满富足的我阅读 1,557评论 16 106