iOS ตรวจสอบว่า เราต่อ 4G หรือ WiFi และดูว่าต่อ 4G เจ้าไหนอยู่

Wasith T. (Bai-Phai)
Nov 3 · 1 min read

ใช่ครับเราสามารถดูได้คร่าว ๆ ว่าต่อ WiFi หรือ cellular อยู่หรือป่าว แต่เราบอกไม่ได้ 100% นะว่าเครื่องจะใช้ WiFi หรือใช้ cellular เพราะเป็น OS ที่จัดการ

ไปที่โค้ดเชค WiFi กันเลย

โดยโค้ดของ Reachability สามารถไปดูได้ที่ https://developer.apple.com/library/archive/samplecode/Reachability/Introduction/Intro.html

อย่าลืม#import "Reachability.h" ใน Bridging Header นะครับ

หรือเราสามารถใช้ Reachaility ตัวอื่นก็ได้ มีให้ใช้เยอะพอสมควร

และเราสามารถดูได้ด้วยว่าต่อด้วย 2G, 3G หรือ 4G

แล้วถ้าจะดูว่าต่อผู้ให้บริการโทรศัพท์เคลื่อนที่เจ้าไหน

สำหรับ code ชุดแรกเป็นของ iOS ต่ำกว่า 12

โดยเท่าที่ทดสอบ ประเทศไทยจะได้รหัส 520

และทดสอบกับแต่ละ sim card ได้ MobileNetworkCode ตามนี้

  • TruemoveH รายเดือน: 00
  • Sim2Fly: 03
  • dtac รายเดือน: 05

ถึงแม้ว่าภายนอกเราอาจจะมองว่าเป็นเจ้าเดียวกันเช่น Orange, Truemove, TruemoveH หรือ dtac, dtac TriNet, Line Mobile ก็อาจจะได้เลข MobileNetworkCode ตัวเดียวกันหรือคนละตัวก็ได้ ต้องไปทดสอบกัน หรือสอบถามไปยัง operator

สำหรับ iOS ต่ำกว่า 12 ดู code ชุดนี้ได้เลย

สำหรับ iOS 12 ขึ้นไป iPhone เริ่มรองรับ 2 sim cards แล้วทำให้ api เปลี่ยนไป โดยสามารถใช้งานได้ผ่าน CTTelephonyNetworkInfo().serviceSubscriberCellularProviders ซึ่งมีหน้าตาประมาณนี้

["0000000100000001": CTCarrier (0x2801f26d0) {
Carrier name: [TRUE-H]
Mobile Country Code: [520]
Mobile Network Code:[00]
ISO Country Code:[th]
Allows VOIP? [YES]
}]

ถ้าผมใช้เป็นแล้วเดี๋ยวค่อยมา update นะครับ

สวัสดีครับ

กูโค้ด

Wasith T. (Bai-Phai)

Written by

ตบมือเป็นกำลังใจให้ผมด้วยนะครับ 😘

กูโค้ด

ใครไม่โค้ด กูโค้ด

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade