您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 撥打電話與傳送簡訊的功能實作

編輯:iOS 相關資訊

最簡便的做法:透過 openURL

iOS 提供了最直接的方式來撥打電話與開啟簡訊:

// 撥打電話
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];

// 開啟簡訊
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10000"]];

這種方式會呼叫系統內建的介面,簡單可靠,也是最推薦的做法。

進階做法:以 MFMessageComposeViewController 傳送簡訊

如果希望按下按鈕後直接進入簡訊撰寫畫面(甚至進一步做到近一步的自動化),可以使用 MessageUI 框架。首先需引入 MFMessageComposeViewControllerDelegate 代理,並實作其回呼方法:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                 didFinishWithResult:(MessageComposeResult)result {
    // 依結果通知使用者
    switch (result) {
        case MessageComposeResultCancelled:
            if (DEBUG) NSLog(@"Result: canceled");
            break;
        case MessageComposeResultSent:
            if (DEBUG) NSLog(@"Result: Sent");
            break;
        case MessageComposeResultFailed:
            if (DEBUG) NSLog(@"Result: Failed");
            break;
        default:
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

群發簡訊

- (IBAction)sendSMS {
    BOOL canSendSMS = [MFMessageComposeViewController canSendText];
    NSLog(@"can send SMS [%d]", canSendSMS);
    if (canSendSMS) {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate = self;
        picker.navigationBar.tintColor = [UIColor blackColor];
        picker.body = @"test";
        picker.recipients = [NSArray arrayWithObject:@"10086"];
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
}

傳送單一簡訊(從網頁取得內容)

- (void)displaySMSComposerSheet {
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate = self;

    UIWebView *web = nil;
    NSMutableString *absUrl = [[NSMutableString alloc] initWithString:web.request.URL.absoluteString];
    [absUrl replaceOccurrencesOfString:@"https://i.aizheke.com"
                            withString:@"https://m.aizheke.com"
                               options:NSCaseInsensitiveSearch
                                 range:NSMakeRange(0, [absUrl length])];

    picker.body = [NSString stringWithFormat:@"我在愛折客上看到:%@,可能對你有用,推薦給你!link:%@",
                   [web stringByEvaluatingJavaScriptFromString:@"document.title"], absUrl];
    [absUrl release];

    [self presentModalViewController:picker animated:YES];
    [picker release];
}

事件綁定

- (IBAction)showSMSPicker:(id)sender {
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if (messageClass != nil) {
        if ([messageClass canSendText]) {
            [self displaySMSComposerSheet];
        } else {
            // 裝置不支援簡訊功能
        }
    } else {
        // iOS 版本過低,iOS 4.0 以上才支援程式內傳送簡訊
    }
}

需要特別注意的是,第二種「後台直接傳送」的做法能否通過 App Store 審核仍有不確定性,因此相較之下,還是建議優先採用第一種透過 openURL 呼叫系統介面的方式。

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