//
// ViewController.m
// 常用功能 Demo
//
// Created by mac on 16/3/19.
// Copyright © 2016 年 。。。。。 All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view, typically from a nib.
NSArray * arr =@[ @" 发送邮件 " , @" 发送邮件 " , @" 拨打电话 " , @" 拨打电话 " , @" 发送短信 " , @" 发送短信 " , @" 打开 AppStore" , @" 打开 AppStore" , @" 打开浏览器 " ];
for ( int i = 0 ; i< arr. count ; i++)
{
UIButton *button = [ UIButton buttonWithType : UIButtonTypeCustom ];
button. frame = CGRectMake (20.0f+i%2*150.0f, 80.0f+i/2*80.0f, 120.0f, 40.0f);
[button setTitle :arr[i] forState : UIControlStateNormal ];
button. tag = i;
[button setTitleColor :[ UIColor blackColor ] forState : UIControlStateNormal ];
[button addTarget : self action : @selector (touch:) forControlEvents : UIControlEventTouchUpInside ];
[ self . view addSubview :button];
}
}
-( void )touch:( UIButton *)btn
{
switch (btn. tag ) {
case 0:
[ self openEmail ];
break ;
case 1:
[ self sendeEmail ];
break ;
case 2:
[ self openPhone ];
break ;
case 3:
[ self callPhone ];
break ;
case 4:
[ self openSms ];
break ;
case 5:
[ self sendeSms ];
break ;
case 6:
[ self openAppStore ];
break ;
case 7:
[ self sendeAppStore ];
break ;
case 8:
[ self openSafari ];
break ;
default :
break ;
}
}
// 打开浏览器 会跳出当前应用
-( void )openSafari
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"http://www.baidu.com" ]];
}
// 打开苹果商店 会跳出当前应用
-( void )openAppStore
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"https://itunes.apple.com/cn/app/wang-yi-xin-wen/id425349261?mt=8" ]];
}
// 打开苹果商店 不会跳出当前应用 - 导入工具包 StoreKit.framework
//.h 中导入头文件 #import <StoreKit/StoreKit.h>
// 实现的类 : SKStoreProductViewController
-( void )sendeAppStore
{
SKStoreProductViewController * viewController =[[ SKStoreProductViewController alloc ] init ];
viewController. delegate = self ;
NSDictionary * dict =[ NSDictionary dictionaryWithObject : @"425349261" forKey : SKStoreProductParameterITunesItemIdentifier ];
[viewController loadProductWithParameters :dict completionBlock :^( BOOL result, NSError * _Nullable error) {
if (result) {
NSLog ( @" 加载成功 " );
}
}];
[ self presentViewController :viewController animated : YES completion : nil ];
}
#pragma mark --------------- delegate ---------------
-( void )productViewControllerDidFinish:( SKStoreProductViewController *)viewController
{
[viewController dismissViewControllerAnimated : YES completion : nil ];
}
// 打开短信 会跳出当前应用
-( void )openSms
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"sms://10086" ]];
}
// 发送短信 不会跳出当前应用 - 导入工具包 MessageUI.framework
//.h 中导入头文件 #import <MessageUI/MessageUI.h>
// 实现的类 : MFMessageComposeViewController
-( void )sendeSms
{
MFMessageComposeViewController * message =[[ MFMessageComposeViewController alloc ] init ];
message. messageComposeDelegate = self ;
NSArray *toRecipients =@[ @"10086" , @"10010" ];
message. recipients =toRecipients;
message. body = @" 你好啊 , 这是是短信的正文 " ;
[ self presentViewController :message animated : YES completion : nil ];
}
#pragma mark --------------- messageComposeDelegate ---------------
-( void )messageComposeViewController:( MFMessageComposeViewController *)controller didFinishWithResult:( MessageComposeResult )result
{
switch (result) {
case MessageComposeResultCancelled :
NSLog ( @" 取消发送 !" );
break ;
case MessageComposeResultSent :
NSLog ( @" 发送成功 !" );
break ;
case MessageComposeResultFailed :
NSLog ( @" 发送失败 !" );
break ;
default :
break ;
}
[controller dismissViewControllerAnimated : YES completion : nil ];
}
// 打电话 直接拨打电话 会跳出当前的应用
-( void )openPhone
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"tel://10086" ]];
}
// 使用 web 拨打电话 不会跳出当前应用
-( void )callPhone
{
UIWebView * webview =[[ UIWebView alloc ] init ];
[webview loadRequest :[ NSURLRequest requestWithURL :[ NSURL URLWithString : @"tel://10010" ]]];
[ self . view addSubview :webview];
}
// 发送邮件 直接发送 会跳出当前应用
-( void )openEmail
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"mailto://devprograms@apple.com" ]];
}
// 发送邮件 不会跳出当前应用
//- 导入工具包 MessageUI.framework
//.h 中导入头文件 #import <MessageUI/MessageUI.h>
// 实现的类 : MFMailComposeViewController
-( void )sendeEmail
{
MFMailComposeViewController * mailPicker =[[ MFMailComposeViewController alloc ] init ];
mailPicker. mailComposeDelegate = self ;
[mailPicker setSubject : @"eMail 主题 " ];
NSArray * toRecipients =@[ @"1@qq.com" , @"2@qq.com" ];
[mailPicker setToRecipients :toRecipients];
[mailPicker setMessageBody : @" 邮件正文 <img src='http://production.xypht.com/goods_photo/154f492/gvt_1400554431164.jpg' width=280 />" isHTML : YES ];
[ self presentViewController :mailPicker animated : YES completion : nil ];
}
#pragma mark --------------- mailComposeDelegate ---------------
-( void )mailComposeController:( MFMailComposeViewController *)controller didFinishWithResult:( MFMailComposeResult )result error:( NSError *)error
{
if (error) {
NSLog ( @"error :%@" ,error);
}
if (result == MFMailComposeResultSent ) {
}
switch (result) {
case MFMailComposeResultCancelled :
NSLog ( @" 取消发送! " );
break ;
case MFMailComposeResultFailed :
NSLog ( @" 发送失败! " );
break ;
case MFMailComposeResultSaved :
NSLog ( @" 保存发送! " );
break ;
case MFMailComposeResultSent :
NSLog ( @" 发送成功 !" );
break ;
default :
break ;
}
[controller dismissViewControllerAnimated : YES completion : nil ];
}
// ViewController.m
// 常用功能 Demo
//
// Created by mac on 16/3/19.
// Copyright © 2016 年 。。。。。 All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view, typically from a nib.
NSArray * arr =@[ @" 发送邮件 " , @" 发送邮件 " , @" 拨打电话 " , @" 拨打电话 " , @" 发送短信 " , @" 发送短信 " , @" 打开 AppStore" , @" 打开 AppStore" , @" 打开浏览器 " ];
for ( int i = 0 ; i< arr. count ; i++)
{
UIButton *button = [ UIButton buttonWithType : UIButtonTypeCustom ];
button. frame = CGRectMake (20.0f+i%2*150.0f, 80.0f+i/2*80.0f, 120.0f, 40.0f);
[button setTitle :arr[i] forState : UIControlStateNormal ];
button. tag = i;
[button setTitleColor :[ UIColor blackColor ] forState : UIControlStateNormal ];
[button addTarget : self action : @selector (touch:) forControlEvents : UIControlEventTouchUpInside ];
[ self . view addSubview :button];
}
}
-( void )touch:( UIButton *)btn
{
switch (btn. tag ) {
case 0:
[ self openEmail ];
break ;
case 1:
[ self sendeEmail ];
break ;
case 2:
[ self openPhone ];
break ;
case 3:
[ self callPhone ];
break ;
case 4:
[ self openSms ];
break ;
case 5:
[ self sendeSms ];
break ;
case 6:
[ self openAppStore ];
break ;
case 7:
[ self sendeAppStore ];
break ;
case 8:
[ self openSafari ];
break ;
default :
break ;
}
}
// 打开浏览器 会跳出当前应用
-( void )openSafari
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"http://www.baidu.com" ]];
}
// 打开苹果商店 会跳出当前应用
-( void )openAppStore
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"https://itunes.apple.com/cn/app/wang-yi-xin-wen/id425349261?mt=8" ]];
}
// 打开苹果商店 不会跳出当前应用 - 导入工具包 StoreKit.framework
//.h 中导入头文件 #import <StoreKit/StoreKit.h>
// 实现的类 : SKStoreProductViewController
-( void )sendeAppStore
{
SKStoreProductViewController * viewController =[[ SKStoreProductViewController alloc ] init ];
viewController. delegate = self ;
NSDictionary * dict =[ NSDictionary dictionaryWithObject : @"425349261" forKey : SKStoreProductParameterITunesItemIdentifier ];
[viewController loadProductWithParameters :dict completionBlock :^( BOOL result, NSError * _Nullable error) {
if (result) {
NSLog ( @" 加载成功 " );
}
}];
[ self presentViewController :viewController animated : YES completion : nil ];
}
#pragma mark --------------- delegate ---------------
-( void )productViewControllerDidFinish:( SKStoreProductViewController *)viewController
{
[viewController dismissViewControllerAnimated : YES completion : nil ];
}
// 打开短信 会跳出当前应用
-( void )openSms
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"sms://10086" ]];
}
// 发送短信 不会跳出当前应用 - 导入工具包 MessageUI.framework
//.h 中导入头文件 #import <MessageUI/MessageUI.h>
// 实现的类 : MFMessageComposeViewController
-( void )sendeSms
{
MFMessageComposeViewController * message =[[ MFMessageComposeViewController alloc ] init ];
message. messageComposeDelegate = self ;
NSArray *toRecipients =@[ @"10086" , @"10010" ];
message. recipients =toRecipients;
message. body = @" 你好啊 , 这是是短信的正文 " ;
[ self presentViewController :message animated : YES completion : nil ];
}
#pragma mark --------------- messageComposeDelegate ---------------
-( void )messageComposeViewController:( MFMessageComposeViewController *)controller didFinishWithResult:( MessageComposeResult )result
{
switch (result) {
case MessageComposeResultCancelled :
NSLog ( @" 取消发送 !" );
break ;
case MessageComposeResultSent :
NSLog ( @" 发送成功 !" );
break ;
case MessageComposeResultFailed :
NSLog ( @" 发送失败 !" );
break ;
default :
break ;
}
[controller dismissViewControllerAnimated : YES completion : nil ];
}
// 打电话 直接拨打电话 会跳出当前的应用
-( void )openPhone
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"tel://10086" ]];
}
// 使用 web 拨打电话 不会跳出当前应用
-( void )callPhone
{
UIWebView * webview =[[ UIWebView alloc ] init ];
[webview loadRequest :[ NSURLRequest requestWithURL :[ NSURL URLWithString : @"tel://10010" ]]];
[ self . view addSubview :webview];
}
// 发送邮件 直接发送 会跳出当前应用
-( void )openEmail
{
[[ UIApplication sharedApplication ] openURL :[ NSURL URLWithString : @"mailto://devprograms@apple.com" ]];
}
// 发送邮件 不会跳出当前应用
//- 导入工具包 MessageUI.framework
//.h 中导入头文件 #import <MessageUI/MessageUI.h>
// 实现的类 : MFMailComposeViewController
-( void )sendeEmail
{
MFMailComposeViewController * mailPicker =[[ MFMailComposeViewController alloc ] init ];
mailPicker. mailComposeDelegate = self ;
[mailPicker setSubject : @"eMail 主题 " ];
NSArray * toRecipients =@[ @"1@qq.com" , @"2@qq.com" ];
[mailPicker setToRecipients :toRecipients];
[mailPicker setMessageBody : @" 邮件正文 <img src='http://production.xypht.com/goods_photo/154f492/gvt_1400554431164.jpg' width=280 />" isHTML : YES ];
[ self presentViewController :mailPicker animated : YES completion : nil ];
}
#pragma mark --------------- mailComposeDelegate ---------------
-( void )mailComposeController:( MFMailComposeViewController *)controller didFinishWithResult:( MFMailComposeResult )result error:( NSError *)error
{
if (error) {
NSLog ( @"error :%@" ,error);
}
if (result == MFMailComposeResultSent ) {
}
switch (result) {
case MFMailComposeResultCancelled :
NSLog ( @" 取消发送! " );
break ;
case MFMailComposeResultFailed :
NSLog ( @" 发送失败! " );
break ;
case MFMailComposeResultSaved :
NSLog ( @" 保存发送! " );
break ;
case MFMailComposeResultSent :
NSLog ( @" 发送成功 !" );
break ;
default :
break ;
}
[controller dismissViewControllerAnimated : YES completion : nil ];
}
@end
//工具包