2013-03-08 57 views
-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或在线教程中找到的所有东西。MessageController(文本)设置主体不设置主体。 (iOS 6)

这确实打印出NSLog中的完整地址。但是,不管是什么我已经试过这不露面[messageController setBody:(@"%@ %@ %@ %@",city, state, address, zip)];

和使用(setBody:@“你好”)的作品就好了... ...

我敢肯定,有一些小的,我可以做解决这个问题,但我所尝试过的一切都失败了。任何想法都会很棒!

回答

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!"); 
    } 
} 
+0

还请记住,该地址查找可能需要一些时间。你可能想要显示一个活动指示器(或类似的东西),让用户知道发生了什么。否则,在点击按钮和出现消息编辑器之间可能会有很长的延迟。事实上,用户可以离开当前的视图控制器,这可能会导致崩溃。 – rmaddy 2013-03-08 19:45:28

0

使用stringWithFormat以设置机身采用不同的对象,像这样

[messageController setBody:[NSString stringWithFormat:@"%@ %@ %@ %@",city, state, address, zip];