iOS常用小功能的實(shí)現(xiàn)
來源:程序員人生 發(fā)布時(shí)間:2015-02-03 09:05:27 閱讀次數(shù):3074次
iOS利用開發(fā)中有許多非常實(shí)用的小功能, 這些小功能的實(shí)現(xiàn)也非常的簡單, 本文將這些小功能匯總,用于備忘。
1. 打電話功能的實(shí)現(xiàn)
實(shí)現(xiàn)打電話功能的方式有多種,其中最好的方式以下:
//利用UIWebView打電話
if (_webView == nil) {
//WebView不需要顯示,只需要實(shí)現(xiàn)打電話功能
_webView = [[UIWebView alloc] initWithFrame:CGRectZero];
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];
2. 發(fā)短信功能的實(shí)現(xiàn)
實(shí)現(xiàn)發(fā)短信的功能也有多種,但是下面的方式最好:
//2.使用MessageUI框架封裝的功能
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
messageVC.body = @"你吃翔了沒?";
messageVC.recipients = @[@"10010", @"10086"];
//設(shè)置代理
messageVC.messageComposeDelegate = self;
[self presentViewController:messageVC animated:YES completion:nil];
#pragma mask MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
//關(guān)閉短信界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MessageComposeResultCancelled) {
NSLog(@"取消發(fā)送");
}else if(result == MessageComposeResultSent){
NSLog(@"發(fā)送成功");
}else{
NSLog(@"發(fā)送失敗");
}
}
3. 發(fā)郵件功能的實(shí)現(xiàn)
發(fā)郵件的功能實(shí)現(xiàn)有多種方式,推薦使用下面的方式:
// 使用MessageUI框架封裝的功能
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
//設(shè)置郵件主題
[mailVC setSubject:@"會(huì)議"];
//設(shè)置郵件內(nèi)容
[mailVC setMessageBody:@"今天下午你去吃翔吧!" isHTML:NO];
//設(shè)置收件人列表
[mailVC setToRecipients:@[@"123456@qq.com"]];
//設(shè)置抄送人列表
[mailVC setCcRecipients:@[@"987654321@qq.com"]];
//設(shè)置密送人列表
[mailVC setBccRecipients:@[@"7654@qq.com"]];
//添加1張附圖(1張圖片)
UIImage *image = [UIImage imageNamed:@"aaa.png"];
//緊縮圖片
NSData *data = UIImagePNGRepresentation(image);
[mailVC addAttachmentData:data mimeType:@"image/png" fileName:@"aaa.png"];
//設(shè)置代理
mailVC.mailComposeDelegate = self;
[self presentViewController:mailVC animated:YES completion:nil];
#pragma mask MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MFMailComposeResultCancelled) {
NSLog(@"取消發(fā)送");
}else if(result == MFMailComposeResultSent){
NSLog(@"發(fā)送成功");
}else{
NSLog(@"發(fā)送失敗");
}
}
4. 利用評(píng)分功能
下面的代碼用于實(shí)現(xiàn)利用評(píng)分功能:
//拿到利用的ID
NSString *appid = @"123456";
NSString *str = [NSString stringWithFormat:@"itms-apps://ituns.apple.com/app/id%@?mt=8", appid];
NSURL *url = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:url];
5. 打開經(jīng)常使用的文件
在iOS開發(fā)中,如果想打開1些常見文件,比如html/txt/pdf/ppt等,都可以選擇使用UIWebView打開,只需要告知UiwebView的URL便可,如果想打開1個(gè)遠(yuǎn)程的同享資源,比如http協(xié)議下的資源,也能夠調(diào)用系統(tǒng)自帶的Safari閱讀器
NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];
[[UIApplication sharedApplication] openURL:url];
6. 關(guān)聯(lián)到其他利用
有時(shí)候,需要在當(dāng)前利用A中打開其他利用B,需要經(jīng)過下面兩步:
1.)首先,B利用有自己的URL地址(在info.plist中配置)

配置好的B利用的URL地址為: mj://ios.itcast.cn
2.) 接著在A利用中使用UIApplication完成跳轉(zhuǎn)
NSURL *url = [NSURL URLWithString:@"mj://ios.itcast.cn"];
[[UIApplication sharedApplication] openURL:url];
Demo下載地址: http://download.csdn.net/detail/luozhonglan/8381037
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)