-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 に答える