Help us understand the problem. What is going on with this article?

URLスキームで電話がかけられるかどうかの判別方法 (not canOpenURL but CoreTelephony)

More than 3 years have passed since last update.

TL;DR

iPod touchも含め、iOSデバイスで電話がかけられるかどうかを判別するためには、CoreTelephony.frameworkを使う。

今までやっていた方法

let application = UIApplication.sharedApplication()
let canCall: Bool
if let URL = NSURL(string: "tel://") {
    canCall = application.canOpenURL(URL)
} else {
    canCall = false
}

if canCall {
    application.openURL(URL)
} else {
    print("can't call")
}

問題点

iPod touchでは判別できない。 (iOS 8, 9両方とも)

解決策

CoreTelephony.framework を使う。

CTCarrier Class Reference - iOS Developer Library

// CTCarrier
var isoCountryCode: String? { get }

The value for this property is nil if any of the following apply:
The device is in Airplane mode.
There is no SIM card in the device.
The device is outside of cellular service range.

isoCaountryCodeがnilの場合、下記のいずれかに該当する

  • 機内モード
  • SIMカードが入っていない
  • 携帯電話サービスの圏外

ドキュメントには書かれていないが、iPod touchの場合、CTCarrierが取得できず、nilになっていた。

import CoreTelephony

let netInfo = CTTelephonyNetworkInfo()
let carrier: CTCarrier? = netInfo.subscriberCellularProvider

// SIMカードが挿入されたiPadを除外するために `userInterfaceIdiom` をチェックする
let isPhone: Bool = (UIDevice.currentDevice().userInterfaceIdiom == .Phone)

let canCall: Bool = (carrier?.isoCountryCode != nil) && isPhone

if canCall {
    UIApplication.sharedApplication().openURL(URL)
} else {
    print("can't call")
}

参照

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした