iOS URL encode UTF8

URL轉UTF8並使用Apple URL Scheme,“Mail”、“Phone”、“Text”、“Map”、“YouTube”、“iTunes”

Mail
NSString *mail = [NSString stringWithFormat:@"Mail here"];
NSString* urlString = [NSString stringWithFormat:@"mailto:%@", mail];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];


Phone
NSString *tel = [NSString stringWithFormat:@"Phone number here"];
NSString* urlString = [NSString stringWithFormat:@"tel:%@", tel];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];

可參考"iOS dial a phone call 在app內撥電話"

Text
NSString *tel = [NSString stringWithFormat:@"Phone number here"];
NSString* urlString = [NSString stringWithFormat:@"sms:%@", tel];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];


MAP
NSString *address = [NSString stringWithFormat:@"address here"];
NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", address];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];

※地址部份可用經緯度代替 EX.25.xxxxx,121.xxxxxx(中間使用逗號隔開)

Map - Native
NSString *addressA = [NSString stringWithFormat:@"addressA here"];
NSString *addressB = [NSString stringWithFormat:@"addressB here"];
NSString* urlString = [NSString stringWithFormat:@"hhttp://maps.google.com/maps?daddr=%@&saddr=%@", addressA, addressB];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];

※地址部份可用經緯度代替 EX.25.xxxxx,121.xxxxxx(中間使用逗號隔開)

Youtube
NSString *id = [NSString stringWithFormat:@"Youtube id here"];
NSString* urlString = [NSString stringWithFormat:@"http://www.youtube.com/v/%@", id];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];


iTunes
NSString *id = [NSString stringWithFormat:@"iTunes id here"];
NSString* urlString = [NSString stringWithFormat:@"hhttp://itunes.apple.com/tw/app/halftone/id%@", id];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];

※不同國家store也會有所,此為台灣store

相關連結
iOS Developer Library
iOS Developer Library - Apple URL Scheme Reference
"iOS dial a phone call 在app內撥電話"

1 則留言: