18
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-04-22

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")
}

参照

18
15
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?