-1
-(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") 工作得很好......

我确信我可以做一些小事情来解决这个问题,但是我尝试过的一切都失败了。任何想法都会很棒!

4

2 回答 2

0

用于stringWithFormat设置具有不同对象的主体,如下所示

[messageController setBody:[NSString stringWithFormat:@"%@ %@ %@ %@",city, state, address, zip];
于 2013-03-08T19:28:20.733 回答
0

你不能按照你正在做的事情做你正在做的事情。调用reverseGeocodeLocation是异步的。在您取回地址信息之前,您最终会呈现消息控制器。这就是为什么身体总是空的。

您需要修改代码以从完成处理程序中创建和显示消息编写器(但您必须在主队列上调度代码)。

- (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!");
    }
}
于 2013-03-08T19:28:29.313 回答