NBPhoneNumberUtil+ShortNumber.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // NBPhoneNumberUtil+ShortNumber.h
  3. // libPhoneNumberiOS
  4. //
  5. // Created by Paween Itthipalkul on 11/29/17.
  6. // Copyright © 2017 Google LLC. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "NBPhoneNumberUtil.h"
  10. @class NBPhoneNumber;
  11. NS_ASSUME_NONNULL_BEGIN
  12. #if SHORT_NUMBER_SUPPORT
  13. typedef NS_ENUM(NSUInteger, NBEShortNumberCost) {
  14. NBEShortNumberCostUnknown = 0,
  15. NBEShortNumberCostTollFree = 1,
  16. NBEShortNumberCostStandardRate = 2,
  17. NBEShortNumberCostPremiumRate = 3,
  18. };
  19. @interface NBPhoneNumberUtil (ShortNumber)
  20. // Short number related methods
  21. /**
  22. * Check whether a short number is a possible number when dialed from the given region. This
  23. * provides a more lenient check than {@link #isValidShortNumberForRegion}.
  24. *
  25. * @param phoneNumber the short number to check
  26. * @param regionDialingFrom the region from which the number is dialed
  27. * @return whether the number is a possible short number
  28. */
  29. - (BOOL)isPossibleShortNumber:(NBPhoneNumber *)phoneNumber forRegion:(NSString *)regionDialingFrom;
  30. /**
  31. * Check whether a short number is a possible number. If a country calling code is shared by
  32. * multiple regions, this returns true if it's possible in any of them. This provides a more
  33. * lenient check than {@link #isValidShortNumber}. See {@link
  34. * #isPossibleShortNumberForRegion(PhoneNumber, String)} for details.
  35. *
  36. * @param phoneNumber the short number to check
  37. * @return whether the number is a possible short number
  38. */
  39. - (BOOL)isPossibleShortNumber:(NBPhoneNumber *)phoneNumber;
  40. /**
  41. * Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify
  42. * the number is actually in use, which is impossible to tell by just looking at the number
  43. * itself.
  44. *
  45. * @param phoneNumber the short number for which we want to test the validity
  46. * @param regionDialingFrom the region from which the number is dialed
  47. * @return whether the short number matches a valid pattern
  48. */
  49. - (BOOL)isValidShortNumber:(NBPhoneNumber *)phoneNumber forRegion:(NSString *)regionDialingFrom;
  50. /**
  51. * Tests whether a short number matches a valid pattern. If a country calling code is shared by
  52. * multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify
  53. * the number is actually in use, which is impossible to tell by just looking at the number
  54. * itself. See {@link #isValidShortNumberForRegion(PhoneNumber, String)} for details.
  55. *
  56. * @param phoneNumber the short number for which we want to test the validity
  57. * @return whether the short number matches a valid pattern
  58. */
  59. - (BOOL)isValidShortNumber:(NBPhoneNumber *)phoneNumber;
  60. /**
  61. * Gets the expected cost category of a short number when dialed from a region (however, nothing
  62. * is implied about its validity). If it is important that the number is valid, then its validity
  63. * must first be checked using {@link #isValidShortNumberForRegion}. Note that emergency numbers
  64. * are always considered toll-free. Example usage:
  65. * <pre>{@code
  66. * // The region for which the number was parsed and the region we subsequently check against
  67. * // need not be the same. Here we parse the number in the US and check it for Canada.
  68. * PhoneNumber number = phoneUtil.parse("110", "US");
  69. * ...
  70. * String regionCode = "CA";
  71. * ShortNumberInfo shortInfo = ShortNumberInfo.getInstance();
  72. * if (shortInfo.isValidShortNumberForRegion(shortNumber, regionCode)) {
  73. * ShortNumberCost cost = shortInfo.getExpectedCostForRegion(number, regionCode);
  74. * // Do something with the cost information here.
  75. * }}</pre>
  76. *
  77. * @param phoneNumber the short number for which we want to know the expected cost category
  78. * @param regionDialingFrom the region from which the number is dialed
  79. * @return the expected cost category for that region of the short number. Returns UNKNOWN_COST if
  80. * the number does not match a cost category. Note that an invalid number may match any cost
  81. * category.
  82. */
  83. - (NBEShortNumberCost)expectedCostOfPhoneNumber:(NBPhoneNumber *)phoneNumber
  84. forRegion:(NSString *)regionDialingFrom;
  85. /**
  86. * Gets the expected cost category of a short number (however, nothing is implied about its
  87. * validity). If the country calling code is unique to a region, this method behaves exactly the
  88. * same as {@link #getExpectedCostForRegion(PhoneNumber, String)}. However, if the country
  89. * calling code is shared by multiple regions, then it returns the highest cost in the sequence
  90. * PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of
  91. * UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE
  92. * or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it
  93. * might be a PREMIUM_RATE number.
  94. * <p>
  95. * For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected
  96. * cost returned by this method will be STANDARD_RATE, since the NANPA countries share the same
  97. * country calling code.
  98. * <p>
  99. * Note: If the region from which the number is dialed is known, it is highly preferable to call
  100. * {@link #getExpectedCostForRegion(PhoneNumber, String)} instead.
  101. *
  102. * @param phoneNumber the short number for which we want to know the expected cost category
  103. * @return the highest expected cost category of the short number in the region(s) with the given
  104. * country calling code
  105. */
  106. - (NBEShortNumberCost)expectedCostOfPhoneNumber:(NBPhoneNumber *)phoneNumber;
  107. /**
  108. * Given a valid short number, determines whether it is carrier-specific (however, nothing is
  109. * implied about its validity). Carrier-specific numbers may connect to a different end-point, or
  110. * not connect at all, depending on the user's carrier. If it is important that the number is
  111. * valid, then its validity must first be checked using {@link #isValidShortNumber} or
  112. * {@link #isValidShortNumberForRegion}.
  113. *
  114. * @param phoneNumber the valid short number to check
  115. * @return whether the short number is carrier-specific, assuming the input was a valid short
  116. * number
  117. */
  118. - (BOOL)isPhoneNumberCarrierSpecific:(NBPhoneNumber *)phoneNumber;
  119. /**
  120. * Given a valid short number, determines whether it is carrier-specific when dialed from the
  121. * given region (however, nothing is implied about its validity). Carrier-specific numbers may
  122. * connect to a different end-point, or not connect at all, depending on the user's carrier. If
  123. * it is important that the number is valid, then its validity must first be checked using
  124. * {@link #isValidShortNumber} or {@link #isValidShortNumberForRegion}. Returns false if the
  125. * number doesn't match the region provided.
  126. *
  127. * @param phoneNumber the valid short number to check
  128. * @param regionDialingFrom the region from which the number is dialed
  129. * @return whether the short number is carrier-specific in the provided region, assuming the
  130. * input was a valid short number
  131. */
  132. - (BOOL)isPhoneNumberCarrierSpecific:(NBPhoneNumber *)phoneNumber forRegion:(NSString *)regionCode;
  133. /**
  134. * Given a valid short number, determines whether it is an SMS service (however, nothing is
  135. * implied about its validity). An SMS service is where the primary or only intended usage is to
  136. * receive and/or send text messages (SMSs). This includes MMS as MMS numbers downgrade to SMS if
  137. * the other party isn't MMS-capable. If it is important that the number is valid, then its
  138. * validity must first be checked using {@link #isValidShortNumber} or {@link
  139. * #isValidShortNumberForRegion}. Returns false if the number doesn't match the region provided.
  140. *
  141. * @param phoneNumber the valid short number to check
  142. * @param regionDialingFrom the region from which the number is dialed
  143. * @return whether the short number is an SMS service in the provided region, assuming the input
  144. * was a valid short number
  145. */
  146. - (BOOL)isPhoneNumberSMSService:(NBPhoneNumber *)phoneNumber forRegion:(NSString *)regionCode;
  147. /**
  148. * Returns true if the given number, exactly as dialed, might be used to connect to an emergency
  149. * service in the given region.
  150. * <p>
  151. * This method accepts a string, rather than a PhoneNumber, because it needs to distinguish
  152. * cases such as "+1 911" and "911", where the former may not connect to an emergency service in
  153. * all cases but the latter would. This method takes into account cases where the number might
  154. * contain formatting, or might have additional digits appended (when it is okay to do that in
  155. * the specified region).
  156. *
  157. * @param number the phone number to test
  158. * @param regionCode the region where the phone number is being dialed
  159. * @return whether the number might be used to connect to an emergency service in the given region
  160. */
  161. - (BOOL)connectsToEmergencyNumberFromString:(NSString *)number forRegion:(NSString *)regionCode;
  162. /**
  163. * Returns true if the given number exactly matches an emergency service number in the given
  164. * region.
  165. * <p>
  166. * This method takes into account cases where the number might contain formatting, but doesn't
  167. * allow additional digits to be appended. Note that {@code isEmergencyNumber(number, region)}
  168. * implies {@code connectsToEmergencyNumber(number, region)}.
  169. *
  170. * @param number the phone number to test
  171. * @param regionCode the region where the phone number is being dialed
  172. * @return whether the number exactly matches an emergency services number in the given region
  173. */
  174. - (BOOL)isEmergencyNumber:(NSString *)number forRegion:(NSString *)regionCode;
  175. @end
  176. #endif // SHORT_NUMBER_SUPPORT
  177. NS_ASSUME_NONNULL_END