你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS7技巧 >> iOS 調用系統發短信的例子

iOS 調用系統發短信的例子

編輯:IOS7技巧
在ios開發中我們需要用到調用短信發送接口了今天我們來看2種iOS 調用系統發短信的例子,具體細節操作如下文所示。

一、程序外調用系統發短信

這個方法其實很簡單,直接調用openURL即可:


NSURL *url = [NSURL URLWithString:@"sms://15888888888"];
[[UIApplication sharedApplication]openURL:url];

二、程序內調用系統發短信

這種方法有一個好處就是用戶發短信之後還可以回到App.
首先要導入MessageUI.framework,並引入頭文件:

#import <MessageUI/MessageUI.h>

然後要遵循代理MFMessageComposeViewControllerDelegate,並實現代理方法。

#pragma mark - 代理方法
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            //信息傳送成功
            
            break;
        case MessageComposeResultFailed:
            //信息傳送失敗
            
            break;
        case MessageComposeResultCancelled:
            //信息被用戶取消傳送
            
            break;
        default:
            break;
    }
}

發送短信方法實現

#pragma mark - 發送短信方法
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.recipients = phones;
        controller.navigationBar.tintColor = [UIColor redColor];
        controller.body = body;
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面標題
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                        message:@"該設備不支持短信功能"
                                                       delegate:nil
                                              cancelButtonTitle:@"確定"
                                              otherButtonTitles:nil, nil];
        [alert show];
    }
}

最後,調用發送短信的方法


[self showMessageView:[NSArray arrayWithObjects:@"15888888888",@"12399999999", nil] title:@"test" body:@"這是測試用短信,勿回復!"];

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved