14

当我第一次在 iphone 上安装 whatsapp 时,当我打开它时,第一个屏幕已经知道我的 sim 卡位置并显示国家代码。whatsapp 是如何知道这一点的?由于苹果拒绝所有以编程方式使用 sim 卡信息的应用程序。

4

3 回答 3

14

在您的 .h 文件中,您需要:

#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

这个函数会得到国家代码:

-(NSString*)getCountryDialingCode {


NSDictionary *dialingCodes = [[NSDictionary alloc]initWithObjectsAndKeys:
                    @"972", @"IL",
                    @"93", @"AF",
                    @"355", @"AL",
                    @"213", @"DZ",
                    @"1", @"AS",
                    @"376", @"AD",
                    @"244", @"AO",
                    @"1", @"AI",
                    @"1", @"AG",
                    @"54", @"AR",
                    @"374", @"AM",
                    @"297", @"AW",
                    @"61", @"AU",
                    @"43", @"AT",
                    @"994", @"AZ",
                    @"1", @"BS",
                    @"973", @"BH",
                    @"880", @"BD",
                    @"1", @"BB",
                    @"375", @"BY",
                    @"32", @"BE",
                    @"501", @"BZ",
                    @"229", @"BJ",
                    @"1", @"BM", @"975", @"BT",
                    @"387", @"BA", @"267", @"BW", @"55", @"BR", @"246", @"IO",
                    @"359", @"BG", @"226", @"BF", @"257", @"BI", @"855", @"KH",
                    @"237", @"CM", @"1", @"CA", @"238", @"CV", @"345", @"KY",
                    @"236", @"CF", @"235", @"TD", @"56", @"CL", @"86", @"CN",
                    @"61", @"CX", @"57", @"CO", @"269", @"KM", @"242", @"CG",
                    @"682", @"CK", @"506", @"CR", @"385", @"HR", @"53", @"CU",
                    @"537", @"CY", @"420", @"CZ", @"45", @"DK", @"253", @"DJ",
                    @"1", @"DM", @"1", @"DO", @"593", @"EC", @"20", @"EG",
                    @"503", @"SV", @"240", @"GQ", @"291", @"ER", @"372", @"EE",
                    @"251", @"ET", @"298", @"FO", @"679", @"FJ", @"358", @"FI",
                    @"33", @"FR", @"594", @"GF", @"689", @"PF", @"241", @"GA",
                    @"220", @"GM", @"995", @"GE", @"49", @"DE", @"233", @"GH",
                    @"350", @"GI", @"30", @"GR", @"299", @"GL", @"1", @"GD",
                    @"590", @"GP", @"1", @"GU", @"502", @"GT", @"224", @"GN",
                    @"245", @"GW", @"595", @"GY", @"509", @"HT", @"504", @"HN",
                    @"36", @"HU", @"354", @"IS", @"91", @"IN", @"62", @"ID",
                    @"964", @"IQ", @"353", @"IE", @"972", @"IL", @"39", @"IT",
                    @"1", @"JM", @"81", @"JP", @"962", @"JO", @"77", @"KZ",
                    @"254", @"KE", @"686", @"KI", @"965", @"KW", @"996", @"KG",
                    @"371", @"LV", @"961", @"LB", @"266", @"LS", @"231", @"LR",
                    @"423", @"LI", @"370", @"LT", @"352", @"LU", @"261", @"MG",
                    @"265", @"MW", @"60", @"MY", @"960", @"MV", @"223", @"ML",
                    @"356", @"MT", @"692", @"MH", @"596", @"MQ", @"222", @"MR",
                    @"230", @"MU", @"262", @"YT", @"52", @"MX", @"377", @"MC",
                    @"976", @"MN", @"382", @"ME", @"1", @"MS", @"212", @"MA",
                    @"95", @"MM", @"264", @"NA", @"674", @"NR", @"977", @"NP",
                    @"31", @"NL", @"599", @"AN", @"687", @"NC", @"64", @"NZ",
                    @"505", @"NI", @"227", @"NE", @"234", @"NG", @"683", @"NU",
                    @"672", @"NF", @"1", @"MP", @"47", @"NO", @"968", @"OM",
                    @"92", @"PK", @"680", @"PW", @"507", @"PA", @"675", @"PG",
                    @"595", @"PY", @"51", @"PE", @"63", @"PH", @"48", @"PL",
                    @"351", @"PT", @"1", @"PR", @"974", @"QA", @"40", @"RO",
                    @"250", @"RW", @"685", @"WS", @"378", @"SM", @"966", @"SA",
                    @"221", @"SN", @"381", @"RS", @"248", @"SC", @"232", @"SL",
                    @"65", @"SG", @"421", @"SK", @"386", @"SI", @"677", @"SB",
                    @"27", @"ZA", @"500", @"GS", @"34", @"ES", @"94", @"LK",
                    @"249", @"SD", @"597", @"SR", @"268", @"SZ", @"46", @"SE",
                    @"41", @"CH", @"992", @"TJ", @"66", @"TH", @"228", @"TG",
                    @"690", @"TK", @"676", @"TO", @"1", @"TT", @"216", @"TN",
                    @"90", @"TR", @"993", @"TM", @"1", @"TC", @"688", @"TV",
                    @"256", @"UG", @"380", @"UA", @"971", @"AE", @"44", @"GB",
                    @"1", @"US", @"598", @"UY", @"998", @"UZ", @"678", @"VU",
                    @"681", @"WF", @"967", @"YE", @"260", @"ZM", @"263", @"ZW",
                    @"591", @"BO", @"673", @"BN", @"61", @"CC", @"243", @"CD",
                    @"225", @"CI", @"500", @"FK", @"44", @"GG", @"379", @"VA",
                    @"852", @"HK", @"98", @"IR", @"44", @"IM", @"44", @"JE",
                    @"850", @"KP", @"82", @"KR", @"856", @"LA", @"218", @"LY",
                    @"853", @"MO", @"389", @"MK", @"691", @"FM", @"373", @"MD",
                    @"258", @"MZ", @"970", @"PS", @"872", @"PN", @"262", @"RE",
                    @"7", @"RU", @"590", @"BL", @"290", @"SH", @"1", @"KN",
                    @"1", @"LC", @"590", @"MF", @"508", @"PM", @"1", @"VC",
                    @"239", @"ST", @"252", @"SO", @"47", @"SJ", @"963",
                    @"SY",@"886",
                    @"TW", @"255",
                    @"TZ", @"670",
                    @"TL",@"58",
                    @"VE",@"84",
                    @"VN",
                    @"284", @"VG",
                    @"340", @"VI",
                    @"678",@"VU",
                    @"681",@"WF",
                    @"685",@"WS",
                    @"967",@"YE",
                    @"262",@"YT",
                    @"27",@"ZA",
                    @"260",@"ZM",
                    @"263",@"ZW",
                    nil];



CTTelephonyNetworkInfo *network_Info = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = network_Info.subscriberCellularProvider;

NSString *icc = [dialingCodes objectForKey:[carrier.isoCountryCode uppercaseString]];

return icc;

}

*这仅适用于内置 SIM 卡的 iPhone

于 2014-10-13T19:54:26.323 回答
3

这是你要找的吗?

NSString *countryCode = [locale objectForKey:NSLocaleCountryCode]; 
于 2013-07-10T17:59:50.720 回答
3

我的两分钱 :-) 使用 Swift 3

注意:即使用户在 iPhone 中没有 SIM,此方法也可以使用。

创建一个新Property List文件IntlDialingCodes.plist并粘贴以下行:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>ac</key>
        <string>247</string>
        <key>ad</key>
        <string>376</string>
        <key>ae</key>
        <string>971</string>
        <key>af</key>
        <string>93</string>
        <key>ag</key>
        <string>1</string>
        <key>ai</key>
        <string>1</string>
        <key>al</key>
        <string>355</string>
        <key>am</key>
        <string>374</string>
        <key>an</key>
        <string>599</string>
        <key>ao</key>
        <string>244</string>
        <key>aq</key>
        <string>672</string>
        <key>ar</key>
        <string>54</string>
        <key>as</key>
        <string>1</string>
        <key>at</key>
        <string>43</string>
        <key>au</key>
        <string>61</string>
        <key>aw</key>
        <string>297</string>
        <key>az</key>
        <string>994</string>
        <key>ba</key>
        <string>387</string>
        <key>bb</key>
        <string>1</string>
        <key>bd</key>
        <string>880</string>
        <key>be</key>
        <string>32</string>
        <key>bf</key>
        <string>226</string>
        <key>bg</key>
        <string>359</string>
        <key>bh</key>
        <string>973</string>
        <key>bi</key>
        <string>257</string>
        <key>bj</key>
        <string>229</string>
        <key>bm</key>
        <string>1</string>
        <key>bn</key>
        <string>673</string>
        <key>bo</key>
        <string>591</string>
        <key>br</key>
        <string>55</string>
        <key>bs</key>
        <string>1</string>
        <key>bt</key>
        <string>975</string>
        <key>bw</key>
        <string>267</string>
        <key>by</key>
        <string>375</string>
        <key>bz</key>
        <string>501</string>
        <key>ca</key>
        <string>1</string>
        <key>cd</key>
        <string>243</string>
        <key>cf</key>
        <string>236</string>
        <key>cg</key>
        <string>242</string>
        <key>ch</key>
        <string>41</string>
        <key>ci</key>
        <string>225</string>
        <key>ck</key>
        <string>682</string>
        <key>cl</key>
        <string>56</string>
        <key>cm</key>
        <string>237</string>
        <key>cn</key>
        <string>86</string>
        <key>co</key>
        <string>57</string>
        <key>cr</key>
        <string>506</string>
        <key>cu</key>
        <string>53</string>
        <key>cv</key>
        <string>238</string>
        <key>cy</key>
        <string>357</string>
        <key>cz</key>
        <string>420</string>
        <key>de</key>
        <string>49</string>
        <key>dj</key>
        <string>253</string>
        <key>dk</key>
        <string>45</string>
        <key>dm</key>
        <string>1</string>
        <key>do</key>
        <string>1</string>
        <key>dz</key>
        <string>213</string>
        <key>ec</key>
        <string>593</string>
        <key>ee</key>
        <string>372</string>
        <key>eg</key>
        <string>20</string>
        <key>er</key>
        <string>291</string>
        <key>es</key>
        <string>34</string>
        <key>et</key>
        <string>251</string>
        <key>fi</key>
        <string>358</string>
        <key>fj</key>
        <string>679</string>
        <key>fk</key>
        <string>500</string>
        <key>fm</key>
        <string>691</string>
        <key>fo</key>
        <string>298</string>
        <key>fr</key>
        <string>33</string>
        <key>ga</key>
        <string>241</string>
        <key>gb</key>
        <string>44</string>
        <key>gd</key>
        <string>1</string>
        <key>ge</key>
        <string>995</string>
        <key>gf</key>
        <string>594</string>
        <key>gh</key>
        <string>233</string>
        <key>gi</key>
        <string>350</string>
        <key>gl</key>
        <string>299</string>
        <key>gm</key>
        <string>220</string>
        <key>gn</key>
        <string>224</string>
        <key>gp</key>
        <string>590</string>
        <key>gq</key>
        <string>240</string>
        <key>gr</key>
        <string>30</string>
        <key>gt</key>
        <string>502</string>
        <key>gu</key>
        <string>1</string>
        <key>gw</key>
        <string>245</string>
        <key>gy</key>
        <string>592</string>
        <key>hk</key>
        <string>852</string>
        <key>hn</key>
        <string>504</string>
        <key>hr</key>
        <string>385</string>
        <key>ht</key>
        <string>509</string>
        <key>hu</key>
        <string>36</string>
        <key>id</key>
        <string>62</string>
        <key>ie</key>
        <string>353</string>
        <key>il</key>
        <string>972</string>
        <key>in</key>
        <string>91</string>
        <key>io</key>
        <string>246</string>
        <key>iq</key>
        <string>964</string>
        <key>ir</key>
        <string>98</string>
        <key>is</key>
        <string>354</string>
        <key>it</key>
        <string>39</string>
        <key>ja</key>
        <string>81</string>
        <key>jm</key>
        <string>1</string>
        <key>jo</key>
        <string>962</string>
        <key>jp</key>
        <string>81</string>
        <key>ke</key>
        <string>254</string>
        <key>kg</key>
        <string>996</string>
        <key>kh</key>
        <string>855</string>
        <key>ki</key>
        <string>686</string>
        <key>km</key>
        <string>269</string>
        <key>kn</key>
        <string>1</string>
        <key>kp</key>
        <string>850</string>
        <key>kr</key>
        <string>82</string>
        <key>kw</key>
        <string>965</string>
        <key>ky</key>
        <string>1</string>
        <key>kz</key>
        <string>7</string>
        <key>la</key>
        <string>856</string>
        <key>lb</key>
        <string>961</string>
        <key>lc</key>
        <string>1</string>
        <key>li</key>
        <string>423</string>
        <key>lk</key>
        <string>94</string>
        <key>lr</key>
        <string>231</string>
        <key>ls</key>
        <string>266</string>
        <key>lt</key>
        <string>370</string>
        <key>lu</key>
        <string>352</string>
        <key>lv</key>
        <string>371</string>
        <key>ly</key>
        <string>218</string>
        <key>ma</key>
        <string>212</string>
        <key>mc</key>
        <string>377</string>
        <key>md</key>
        <string>373</string>
        <key>me</key>
        <string>382</string>
        <key>mg</key>
        <string>261</string>
        <key>mh</key>
        <string>692</string>
        <key>mk</key>
        <string>389</string>
        <key>ml</key>
        <string>223</string>
        <key>mm</key>
        <string>95</string>
        <key>mn</key>
        <string>976</string>
        <key>mo</key>
        <string>853</string>
        <key>mp</key>
        <string>1</string>
        <key>mq</key>
        <string>596</string>
        <key>mr</key>
        <string>222</string>
        <key>ms</key>
        <string>1</string>
        <key>mt</key>
        <string>356</string>
        <key>mu</key>
        <string>230</string>
        <key>mv</key>
        <string>960</string>
        <key>mw</key>
        <string>265</string>
        <key>mx</key>
        <string>52</string>
        <key>my</key>
        <string>60</string>
        <key>mz</key>
        <string>258</string>
        <key>na</key>
        <string>264</string>
        <key>nc</key>
        <string>687</string>
        <key>ne</key>
        <string>227</string>
        <key>ng</key>
        <string>234</string>
        <key>ni</key>
        <string>505</string>
        <key>nl</key>
        <string>31</string>
        <key>no</key>
        <string>47</string>
        <key>np</key>
        <string>977</string>
        <key>nr</key>
        <string>674</string>
        <key>nu</key>
        <string>683</string>
        <key>nz</key>
        <string>64</string>
        <key>om</key>
        <string>968</string>
        <key>pa</key>
        <string>507</string>
        <key>pe</key>
        <string>51</string>
        <key>pf</key>
        <string>689</string>
        <key>pg</key>
        <string>675</string>
        <key>ph</key>
        <string>63</string>
        <key>pk</key>
        <string>92</string>
        <key>pl</key>
        <string>48</string>
        <key>pm</key>
        <string>508</string>
        <key>pr</key>
        <string>1</string>
        <key>ps</key>
        <string>970</string>
        <key>pt</key>
        <string>351</string>
        <key>pw</key>
        <string>680</string>
        <key>py</key>
        <string>595</string>
        <key>qa</key>
        <string>974</string>
        <key>re</key>
        <string>262</string>
        <key>ro</key>
        <string>40</string>
        <key>rs</key>
        <string>381</string>
        <key>ru</key>
        <string>7</string>
        <key>rw</key>
        <string>250</string>
        <key>sa</key>
        <string>966</string>
        <key>sb</key>
        <string>677</string>
        <key>sc</key>
        <string>248</string>
        <key>sd</key>
        <string>249</string>
        <key>se</key>
        <string>46</string>
        <key>sg</key>
        <string>65</string>
        <key>sh</key>
        <string>290</string>
        <key>si</key>
        <string>386</string>
        <key>sk</key>
        <string>421</string>
        <key>sl</key>
        <string>232</string>
        <key>sm</key>
        <string>378</string>
        <key>sn</key>
        <string>221</string>
        <key>so</key>
        <string>252</string>
        <key>sr</key>
        <string>597</string>
        <key>st</key>
        <string>239</string>
        <key>sv</key>
        <string>503</string>
        <key>sx</key>
        <string>1</string>
        <key>sy</key>
        <string>963</string>
        <key>sz</key>
        <string>268</string>
        <key>tc</key>
        <string>1</string>
        <key>td</key>
        <string>235</string>
        <key>tg</key>
        <string>228</string>
        <key>th</key>
        <string>66</string>
        <key>tj</key>
        <string>992</string>
        <key>tk</key>
        <string>690</string>
        <key>tm</key>
        <string>993</string>
        <key>tn</key>
        <string>216</string>
        <key>to</key>
        <string>676</string>
        <key>tr</key>
        <string>90</string>
        <key>tt</key>
        <string>1</string>
        <key>tv</key>
        <string>688</string>
        <key>tw</key>
        <string>886</string>
        <key>tz</key>
        <string>255</string>
        <key>ua</key>
        <string>380</string>
        <key>ug</key>
        <string>256</string>
        <key>uk</key>
        <string>44</string>
        <key>us</key>
        <string>1</string>
        <key>uy</key>
        <string>598</string>
        <key>uz</key>
        <string>998</string>
        <key>va</key>
        <string>379</string>
        <key>vc</key>
        <string>1</string>
        <key>ve</key>
        <string>58</string>
        <key>vg</key>
        <string>1</string>
        <key>vi</key>
        <string>1</string>
        <key>vn</key>
        <string>84</string>
        <key>vu</key>
        <string>678</string>
        <key>wf</key>
        <string>681</string>
        <key>ws</key>
        <string>685</string>
        <key>xt</key>
        <string>800</string>
        <key>ye</key>
        <string>967</string>
        <key>za</key>
        <string>27</string>
        <key>zm</key>
        <string>260</string>
        <key>zw</key>
        <string>263</string>
    </dict>
</plist>    

在 Helper 类中粘贴以下类或getDialingCode方法,记住要导入CoreTelephony

import Foundation
import CoreTelephony

class Helper {

    static func getDialingCode() {
        let countryCode = CTTelephonyNetworkInfo().subscriberCellularProvider?.isoCountryCode ?? Locale.current.regionCode!
        let path = Bundle.main.path(forResource: "IntlDialingCodes", ofType: "plist")!
        let dict = NSDictionary(contentsOfFile: path)!

        if let dialingCode = dict[countryCode.lowercased()] as? String {
            print("Dialing code for \"\(countryCode.uppercased())\" is \"+\(dialingCode)\"")
        } else {
            print("Dialing code for \"\(countryCode.uppercased())\" not found in plist file")
        }
    }

}

用法:

Helper.getDialingCode()
于 2017-02-18T14:30:49.910 回答