您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 學習筆記(五):撥打電話、傳送簡訊與寄送郵件

編輯:iOS 開發綜合

這篇把三個高頻但容易寫錯的小功能整理成可直接參考的片段,並標出各版本 SDK 的差異。

一、撥打電話

模擬器上無法實際撥號,必須用實機測試。

[[UIApplication sharedApplication] openURL:
    [NSURL URLWithString:
        [NSString stringWithFormat:@"tel://%@", selectedUser.officeTel_default]]];

這個寫法有兩個問題值得留意:號碼中若含有空格、括號或連字號(例如 (02) 2345-6789),應該先過濾掉非數字字元再組出 URL,否則 openURL 會失敗。另外,iOS 對 tel: 有頻率限制——連續快速呼叫多次會被系統阻擋。

二、傳送簡訊

先採用 MFMessageComposeViewControllerDelegate 協定。

if ([[UIDevice currentDevice].systemVersion floatValue] >= 4.0) {
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *controller =
            [[MFMessageComposeViewController alloc] init];
        controller.messageComposeDelegate = self;
        controller.recipients = [NSArray arrayWithObject:selectedUser.mobileTel_default];
        [self presentModalViewController:controller animated:YES];
        [controller release];
    } else {
        UIAlertView *alert =
            [[UIAlertView alloc] initWithTitle:@"出錯了"
                                      message:@"您的設備不能發送簡訊"
                                     delegate:nil
                            cancelButtonTitle:@"好的"
                            otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
} else {
    // iOS 4.0 以下的退路:直接用 URL scheme
    [[UIApplication sharedApplication] openURL:
        [NSURL URLWithString:
            [NSString stringWithFormat:@"sms://%@", selectedUser.mobileTel_default]]];
}

#pragma mark - 簡訊結果

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                 didFinishWithResult:(MessageComposeResult)result {
    if (result == MessageComposeResultSent) {
        [self dismissModalViewControllerAnimated:YES];
    } else {
        // failed to send text msg
    }
}

這裡的 canSendText 檢查是關鍵。在模擬器或沒有 SIM 卡的裝置上,這個方法會回傳 NO;不檢查就直接 present,畫面會一片空白甚至異常。

三、寄送郵件

同樣要先採用 MFMailComposeViewControllerDelegate 協定。

if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *controller =
        [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setToRecipients:[NSArray arrayWithObject:selectedUser.email]];
    [self presentModalViewController:controller animated:YES];
    [controller release];
} else {
    UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle:@"出錯了"
                                  message:@"您還沒有設定郵件帳戶"
                                 delegate:nil
                        cancelButtonTitle:@"好的"
                        otherButtonTitles:nil];
    [alert show];
    [alert release];
}

#pragma mark - 郵件結果

- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error {
    [self dismissModalViewControllerAnimated:YES];
}

幾個值得更新的寫法

上面的程式碼反映了當時的 API 狀態,若你在新專案中使用,有幾處應該調整:

1. presentModalViewController: 已弃用。改用:

[self presentViewController:controller animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];

2. UIAlertView 已弃用。改用 UIAlertController。

3. 系統版本判斷不必再手寫。[[UIDevice currentDevice].systemVersion floatValue] >= 4.0 這種寫法可以用 NSFoundationVersionNumber 或 @available 取代,更準確也更容易讀。

4. ARC 下不要呼叫 release。上面範例中的 [controller release] 屬於 MRC 寫法,在 ARC 專案中會直接編譯失敗。

5. 記得在委派方法中關閉視窗。三個功能都一樣——如果使用者按下取消或寄送完成後沒有 dismiss,畫面就會卡在郵件/簡訊編輯器上。

一個共同的設計建議

這三個功能的「可用性檢查」都該在點擊之前就做好,而不是等到使用者按下去才發現不能用。實務上常見的做法是:進入畫面時就呼叫 canSendMail/canSendText,把不支援的按鈕直接設為 disabled 並附上提示,讓使用者一開始就知道狀況。

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