2013-03-08 85 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];