一.储存崩溃日志
- (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;
}