So my simple idea is to create an app that allows the user to report their location's latitude and longitude coordinates via email. You tap the button, the email screen comes up via the MessageUI framework, the To, Subject, and Body fields are already pre-entered, all that's needed is for the user to hit "send".
My problem, is that I need the latitude and longitude coordinates to be included in the email body. Those coordinate variables are generated in the -(void)CLLocationManager function and turned into strings, just like I need. The problem is, the email is being sent from another function, -(void)displayComposerSheet, and I can't figure out how to get the lat / long strings into the body of the email to be sent. After pounding my way through Google for a bit, I've come across the idea of Global Variables. This seems like it's what I need to implement. A lot of sources are saying "declare your variables in开发者_如何学JAVA the app delegate and then you'll be able to use them wherever in your code", or at least that's what I'm taking it to mean. Again, I'll stress that I'm fairly new to this game. So, it seems that if I were to create my latitude and longitude strings in the delegate.m file instead of the project .m file, then I'll be able to call upon them at my leisure and, presto, send them in my email's body.
I'm just not exactly where all my "stuff" is supposed to go. Here's what I've got so far (which works perfectly). I just need to replace my default latitude value of "12.3456" and longitude value of "78.9012" withe the ACTUAL values generated by CLLocationManager. Any help would be greatly appreciated. Thanks!
//Code that generates the Latitude and Longitude strings
//--------------------------------------------------------
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//Breaks down the location into degrees, minutes, and seconds.
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
latitude.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longitude.text = longt;
}
//Code that prepares the email for sending
//------------------------------------------
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"New Location Report!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"pghapps2009@gmail.com"];
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString *message = @"user reported their location at:";
NSString *msgLat = @"12.3456";
NSString *msgLong = @"78.9012";
NSString *emailBody = [NSString stringWithFormat:@"%@\nLatitude = %@\nLongitude = %@", message, msgLat, msgLong];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
// NSString *msgLat = self->latitude.text; do not do this
// NSString *msgLong = self->longitude.text; or this
NSString *msgLat = latitude.text;
NSString *msgLong = longitude.text;
No global variables needed (assume both methods belong to the same class).
You don't need global variables for this. Just create instance variables (ivars) in the controller class, and stash the lat/long in them. Then, in your displayComposerSheet method, use the values you computed previously.
精彩评论