24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mailto:スキームの注意点(iOS)

Last updated at Posted at 2014-05-13

iOSでは標準のメールアプリを開くURLスキームがあります。

mailto:mail@dom.com?subject=text&body=body

のアドレスをUIApplicationの共有インスタンスでopenURLすることでメールアプリが起動し、自動的に宛先、件名を補完します。
この際に、

NSString*subject = @"こんにちは!";
NSString*text = @"今度油揚げ食べましょう";
NSString*scheme = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@",subject,text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:scheme]];

のようにしてしまいがちですが、これだと実はメールアプリは起動しません。
パラメータはUTF8エンコードする必要があります。

NSString*subject = 
[@"こんにちは!" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*text = 
[@"今度油揚げ食べましょう" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*scheme = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@",subject,text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:scheme]];

これで正常にメールアプリが起動します。

24
23
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?