-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALSUFileLogger+SendLogs.m
136 lines (126 loc) · 5.4 KB
/
ALSUFileLogger+SendLogs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// ALSUFileLogger+SendLogs.m
// Spheres
//
// Created by Alexey Strokin on 11/13/14.
// Copyright (c) 2014 Pocketspheres LLC. All rights reserved.
//
#import "ALSUFileLogger.h"
#import "PleaseWaitAlertView.h"
#import "ZipFile.h"
#import "ZipWriteStream.h"
#import <MessageUI/MessageUI.h>
@implementation ALSUFileLogger (SendLogs)
-(void)sendLogs
{
[PleaseWaitAlertView show];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale* enUS = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale: enUS];
[formatter setLenient: YES];
[formatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"];
NSString *archiveName = [NSString stringWithFormat:@"Logs_PS%@.zip", [formatter stringFromDate:[NSDate date]]];
NSString *archivePath = [PathForCachesFolder() stringByAppendingPathComponent:archiveName];
self.archivePath = archivePath;
NSThread *zipThread= [[NSThread alloc] initWithTarget:self selector:@selector(zipFilesToFile:) object:archivePath];
[zipThread start];
}
- (void)zipFilesToFile:(NSString *)archivePath
{
@autoreleasepool {
@try {
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:archivePath mode:ZipFileModeCreate];
NSString *logPath = PathForCachesFolder();
NSArray *logFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:logPath error:nil];
for (NSString *logFileName in logFiles)
{
if ([logFileName hasSuffix:@"log"])
{
NSString *filePath = [logPath stringByAppendingPathComponent:logFileName];
ZipWriteStream *stream= [zipFile writeFileInZipWithName:logFileName compressionLevel:ZipCompressionLevelBest];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
[stream writeData:data];
[stream finishedWriting];
}
}
[zipFile close];
[self performSelectorOnMainThread:@selector(finishZip) withObject:nil waitUntilDone:NO];
}
@catch (NSException *e)
{
[self performSelectorOnMainThread:@selector(failZip) withObject:nil waitUntilDone:NO];
}
}
}
- (void)finishZip
{
[PleaseWaitAlertView dismiss];
[self emailLogs];
}
-(void)failZip
{
[PleaseWaitAlertView dismiss];
DLog(@"Zipping error");
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:NSLocalizedString(@"Zipping error", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil] show];
}
- (void)emailLogs
{
if (![MFMailComposeViewController canSendMail])
{
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:NSLocalizedString(@"Setup email client", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil] show];
return;
}
if (self.archivePath != nil)
{
NSString *name = [self.archivePath lastPathComponent];
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
NSData *attachmentData = [[NSData alloc] initWithContentsOfFile:self.archivePath];
[mailPicker addAttachmentData:attachmentData mimeType:@"application/zip" fileName:name];
[mailPicker setSubject:@"PocketSpheres iOS logs"];
[mailPicker setBccRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
UIViewController *vc = [[[[[UIApplication sharedApplication] windows] firstObject] rootViewController] presentedViewController];
[vc presentViewController:mailPicker animated:YES completion:NULL];
}
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *message = nil;
if (error)
{
message = error.localizedDescription;
}
else if (result == MFMailComposeResultSent)
{
message = NSLocalizedString(@"Logs have been successfully sent. Thank you.", nil);
}
else if (result == MFMailComposeResultFailed)
{
message = error.localizedDescription;
}
else if (result == MFMailComposeResultSaved)
{
message = NSLocalizedString(@"Saved successfully", nil);
}
else if (result == MFMailComposeResultCancelled)
{
message = NSLocalizedString(@"Deleted successfully", nil);
}
DLog(@"Email: %@", message);
double delayInSeconds = 0.4;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"", nil) message:message delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil] show];
});
[controller dismissViewControllerAnimated:YES completion:NULL];
}
-(UIButton*)sendLogsButton
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:STR(@"Send logs") forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(sendLogs) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
@end