Friday 15 April 2016

Get the crash log from console

I am developing one of application which crashes in 64 devices  which i don't have. My client is outside from company and i have to upload the build and check the log . He has to mail me crash log.

For that i apply this functionality which  anyone can use.

This function can be used to write the log in document folder and then email them.

    - (IBAction)redirectLogToDocuments
        {
            if ([MFMailComposeViewController canSendMail]==TRUE) {


            NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [allPaths objectAtIndex:0];
            NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"clipboard.txt"];

            freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);

            NSString *emailTitle = @"Email Log";
            // Email Content
            NSString *messageBody = @"Check Log to view from where application stop";
            // To address
            NSArray *toRecipents = [NSArray arrayWithObject:@"support@appcoda.com"];

            MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
            mc.mailComposeDelegate = self;
            [mc setSubject:emailTitle];
            [mc setMessageBody:messageBody isHTML:NO];
            mc.modalPresentationStyle = UIModalPresentationCurrentContext;
            [mc setToRecipients:toRecipents];
            NSData *fileData = [NSData dataWithContentsOfFile:pathForLog];
            [mc addAttachmentData:fileData mimeType:@"text/plain"  fileName:@"clipboard.txt"];

            // Present mail view controller on screen
            //[self presentViewController:mc animated:YES completion:nil];
            [self presentViewController:mc animated:YES completion:^{

            }];
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Please configure mail" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                [alert show];
            }

        }


  - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        NSString *strMessage;
        switch (result)
        {
            case MFMailComposeResultCancelled:
                NSLog(@"Mail cancelled");
                strMessage = @"Mail cancelled";
                break;
            case MFMailComposeResultSaved:
                NSLog(@"Mail saved");
                strMessage = @"Mail save";
                break;
            case MFMailComposeResultSent:
                strMessage = @"Mail sent";
                NSLog(@"Mail sent");
                break;
            case MFMailComposeResultFailed:
                NSLog(@"Mail sent failure: %@", [error localizedDescription]);
                 strMessage = @"Mail sent failure";
                break;
            default:
                break;
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:strMessage delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [self dismissViewControllerAnimated:YES completion:NULL];
    }

Thats it you are done now you can mail yout whole log of application by mail.

Happy Coding!!!!

No comments:

Post a Comment