Skip to content

Instantly share code, notes, and snippets.

@ddonovan
Last active August 29, 2015 14:11
Show Gist options
  • Save ddonovan/2f076e0cbcb6af025785 to your computer and use it in GitHub Desktop.
Save ddonovan/2f076e0cbcb6af025785 to your computer and use it in GitHub Desktop.
snippet from DebugTableViewController to collect .log files and zip them into an email attachment.
#import "DebugTableViewController.h"
#import <ZipArchive/ZipArchive.h>
#import <MessageUI/MessageUI.h>
@interface DebugTableViewController ()<MFMailComposeViewControllerDelegate>
@end
@implementation DebugTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)showAlertWithTitle: (NSString*)title message: (NSString*)message {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
- (NSString *)cachesDirectory {
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
}
- (NSString *)logsDirectory {
return [[self cachesDirectory] stringByAppendingPathComponent:@"Logs"];
}
- (NSData *)zipLogs {
NSString *logsDir = [self logsDirectory];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:logsDir error:nil];
NSPredicate *textFilePredicate = [NSPredicate predicateWithFormat:@"self ENDSWITH '.log'"];
files = [files filteredArrayUsingPredicate:textFilePredicate];
NSString *logZipPath = [logsDir stringByAppendingPathComponent:@"logs.zip"];
if ([[NSFileManager defaultManager] fileExistsAtPath:logZipPath]) {
[[NSFileManager defaultManager] removeItemAtPath:logZipPath error:nil];
}
ZipArchive *za = [[ZipArchive alloc] initWithFileManager:[NSFileManager defaultManager]];
[za CreateZipFile2:logZipPath];
for (NSString *file in files) {
DDLogVerbose(@"Log File being added to zip %@", file);
[za addFileToZip:[logsDir stringByAppendingPathComponent:file] newname:file];
}
[za CloseZipFile2];
NSData *zipData = [NSData dataWithContentsOfFile:logZipPath];
[[NSFileManager defaultManager] removeItemAtPath:logZipPath error:nil];
return zipData;
}
- (IBAction)emailLogFiles:(id)sender {
NSLog(@"email log files");
if (![MFMailComposeViewController canSendMail]) {
[self showAlertWithTitle:@"Unable to email log files" message:@"Please make sure email is setup on your device."];
return;
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSData *zipFileData = [self zipLogs];
dispatch_async(dispatch_get_main_queue(), ^{
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[mailVC setSubject:@"Application Log Files"];
[mailVC setToRecipients:@[@"youremail.com"]];
[mailVC setMessageBody:@"Please find the attached logs" isHTML:NO];
[mailVC addAttachmentData:zipFileData
mimeType:@"application/zip"
fileName:@"logs.zip"];
[mailVC setMailComposeDelegate:self];
[self presentViewController:mailVC animated:YES completion:nil];
});
});
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultSaved:
DDLogInfo(@"Saved as a draft");
break;
case MFMailComposeResultCancelled:
DDLogInfo(@"Mail cancelled");
break;
case MFMailComposeResultSent:
DDLogInfo(@"Mail sent");
break;
case MFMailComposeResultFailed:
DDLogInfo(@"Mail send failed");
break;
}
if (error) {
DDLogError(@"Error sending log files %@",error);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment