NSURL类

NSURL是常用的类,用来描述一段URL的,当然它能做的不止这些。
1. 需要取得URL中不同部分的时候,我们应该用URL提供的接口,而不是把它当做一个普通的字符串去手工分析。

比如:

http://www.testurl.com:8080/subpath/subsubpath?uid=123&gid=456

NSURL *url = [NSURL URLWithString:@"http:/www.testurl.com:8080/subpath/subsubpath?uid=123&gid=456"];

下面是常用的几个接口,和它们的输出。接口意思都符合相关RFC里的定义。

[url scheme]
http

[url host]
www.testurl.com

[url port]
8080

[url path]
/subpath/subsubpath

[url lastPathComponent]
subsubpath

[url query]
uid=123&gid=456

如果希望在safari中打开某个url,则

NSString *stringURL = @"http://blog.langyufei.com";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

2. 如果想打开map app的话(iOS6之前适用)

NSString *title = @"title";
float latitude = 34.3244;
float longitude = 12.3543;
int zoom = 12;
NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@@%1.6f,%1.6f&z=%d", title, latitude, longitude, zoom];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

更新:
iOS6之后苹果改用自家的map app,所以方法略有不同,详细资料参考苹果官方文档.
更新后的url应该像这样
http://maps.apple.com/maps?q=cupertino
打开这样一个url将使你的app推出并跳转到map app.
如果需要打开map app并显示一个driving route,可以使用类似如下的rul
http://maps.apple.com/maps?daddr=San+Francisco,+CA&saddr=cupertino

3. 如果想拨打电话

// 字符可以有+但是不可以有空格和括号,比如美国电话一般为+1(123)1234567
NSString *phone = @"+8613478586661";
// 这个"tel:"是关键
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phone]];
[[UIApplication sharedApplication] openURL:url];

4. 如果想使用MSM app

NSString *stringURL = @"sms:+12345678901";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

如果想获得更多的控制可以使用这个方法

// 如果设备支持MSM
if([MFMessageComposeViewController canSendText])
        {
            MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
            controller.recipients = [NSArray arrayWithObject:@"phoneNumberHere"];
            controller.body = @"your main text string here";
            controller.messageComposeDelegate = self;
            [self.navigationController pushViewController:controller animated:YES];
            [controller release];
        }
 // else
 //     Device does not support SMS;       

5. 如果想发送email

NSString *stringURL = @"mailto:test@email.com";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

同样,如果想获得更多的控制可以使用这个方法

NSString *subject = @"Message subject";
NSString *body = @"Message body";
NSString *address = @"test@email.com";
NSString *cc = @"test2@email.com";
NSString *path = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@", address, cc, subject, body];
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];

或者

// 如果设备支持EMAIL
if ([MFMailComposeViewController canSendMail])
        {
            MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
            [controller setToRecipients:[NSArray arrayWithObject:@"mailto:test@email.com"];
            [controller setMessageBody:@"your email text here" isHTML:YesOrNo];
            controller.mailComposeDelegate = self;
            [self.navigationController pushViewController:controller animated:YES];
            [controller release];
            
        }
 // else
 //     Device does not support SMS;       

6. 如果想播放音乐

NSString *stringURL = @"music:";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

7. Videos

NSString *stringURL = @"videos:";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

8. Youtube

NSString *stringURL = @"http://www.youtube.com/something...";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

9. iTunes

NSString *stringURL = @"http://phobos.apple.com/something";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

10. Apple store

NSString *stringURL = @"http://itunes.apple.com/something";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

更多更详细的信息可以在这里找到.

发表评论

电子邮件地址不会被公开。 必填项已用*标注