Skip to content

Instantly share code, notes, and snippets.

@MACastro987
Last active August 29, 2015 14:27
Show Gist options
  • Save MACastro987/4bbe7d5aa2dfb815ec7a to your computer and use it in GitHub Desktop.
Save MACastro987/4bbe7d5aa2dfb815ec7a to your computer and use it in GitHub Desktop.
View Controller
#import "ViewController.h"
@interface ViewController () <CLLocationManagerDelegate>
@end
@implementation ViewController{
NSString *currentLongitude;
NSString *currentLatitude;
NSString *googleMapsURL;
CLLocationManager *locationManager_;
}
- (void)viewDidLoad {
[super viewDidLoad];
locationManager_ = [[CLLocationManager alloc] init];
locationManager_.delegate = self;
locationManager_.distanceFilter = kCLDistanceFilterNone;
locationManager_.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager_ requestAlwaysAuthorization];
[locationManager_ startUpdatingLocation];
}
- (void)viewDidAppear:(BOOL)animated{
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
googleMapsURL = [[NSString alloc] initWithFormat:@"https://maps.google.com?saddr=Current+Location&daddr=%1.6f,%1.6f",newLocation.coordinate.latitude, newLocation.coordinate.longitude];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[picker dismissViewControllerAnimated:YES completion:nil];
// Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.
[self performSelector:@selector(composeMail:) withObject:image afterDelay:1.0];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)composeMail:(UIImage *)image{
NSString *bodyHeader = @"Here are your directions:";
NSString *mailBody = [NSString stringWithFormat:@"%@\n%@", bodyHeader, googleMapsURL];
NSData *data = UIImagePNGRepresentation(image);
MFMailComposeViewController *emailComposer = [[MFMailComposeViewController alloc] init];
[emailComposer setSubject:@"Google Maps Directions"];
[emailComposer setMessageBody:mailBody isHTML:NO];
[emailComposer setToRecipients:@[@"castro.michael87@gmail.com"]];
[emailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[emailComposer addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage"];
[self presentViewController:emailComposer animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
UIAlertView *messageFailed = [[UIAlertView alloc]initWithTitle:@"Message Failed" message:@"Your message could not be sent" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
UIAlertView *messageSent = [[UIAlertView alloc] initWithTitle:@"Message Sent" message:@"Your message has been sent" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
UIAlertView *messageComposeResultSaved = [[UIAlertView alloc] initWithTitle:@"Message Saved" message:@"Your message has been saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
UIAlertView *messageComposeResultCancelled = [[UIAlertView alloc] initWithTitle:@"Message Cancelled" message:@"Your message has been cancelled" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
switch (result) {
case MFMailComposeResultSent:
[self presentViewController: messageSent animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
break;
case MFMailComposeResultSaved:
[self presentViewController:messageComposeResultSaved animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
break;
case MFMailComposeResultCancelled:
[self presentViewController:messageComposeResultCancelled animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
break;
case MFMailComposeResultFailed:
[self presentViewController:messageFailed animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
break;
default:
NSLog(@"An error occurred when trying to compose this email");
break;
}
// Dismiss the composer
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)captureImageButton:(id)sender {
if (self.imageView.image == nil) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
else {
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment