-(IBAction)SendTextTapped:(id)sender{
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController* messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
__block NSString *fullA = [[NSString alloc] init];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *newerLocation = [[CLLocation alloc]initWithLatitude:21.1700
longitude:72.8300];
[geocoder reverseGeocodeLocation:newerLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Geocode failed with error: %@", error);
return;
}
if (placemarks && placemarks.count > 0)
{
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary =
placemark.addressDictionary;
NSLog(@"%@ ", addressDictionary);
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *zip = [addressDictionary
objectForKey:(NSString *)kABPersonAddressZIPKey];
NSLog(@"%@ %@ %@ %@", address,city, state, zip);
//NSLog(fullA);
fullA=(@"%@ %@ %@ %@", address,city, state, zip);
}
}];
[messageController setBody:fullA];
[self presentModalViewController:messageController animated:YES];
}
else {
NSLog(@"%@" , @"Sorry, you need to setup mail first!");
}
}
我已经尝试使用__block,或者所有我能找到的在线教程。
这将在NSLog中打印完整的地址。然而,无论我尝试了什么,这都不会显示[messageController setBody:(@"%@ %@ %@ %@",city, state, address, zip)];
和使用(setBody:@"Hello")工作得很好…
我确信我可以做一些小事情来解决这个问题,但是我所尝试的一切都失败了。任何想法都很棒!
你不能用你现在的方式做你正在做的事。对reverseGeocodeLocation
的调用是异步的。在返回地址信息之前很久,您就会显示消息控制器。这就是为什么body总是空的原因。
您需要修改代码以在完成处理程序中创建和显示消息编写器(但是您必须在主队列上分派代码)。
- (IBAction)SendTextTapped:(id)sender{
if ([MFMessageComposeViewController canSendText]) {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *newerLocation = [[CLLocation alloc]initWithLatitude:21.1700 longitude:72.8300];
[geocoder reverseGeocodeLocation:newerLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Geocode failed with error: %@", error);
return;
}
NSString *body = @"";
if (placemarks && placemarks.count > 0) {
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary =
placemark.addressDictionary;
NSLog(@"%@ ", addressDictionary);
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *zip = [addressDictionary
objectForKey:(NSString *)kABPersonAddressZIPKey];
NSLog(@"%@ %@ %@ %@", address,city, state, zip);
//NSLog(fullA);
body = [NSString stringWithFormat:@"%@ %@ %@ %@", address,city, state, zip];
}
dispatch_async(dispatch_get_main_queue(), ^{
MFMessageComposeViewController* messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setBody:body];
[self presentModalViewController:messageController animated:YES];
});
}];
} else {
NSLog(@"%@" , @"Sorry, you need to setup mail first!");
}
}
使用stringWithFormat
来设置不同对象的body,如下所示
[messageController setBody:[NSString stringWithFormat:@"%@ %@ %@ %@",city, state, address, zip];