I'm wondering is that possible to get the iOS device model number programmatically. I mean the device model on the back of iPhone. It's always in this format "AXXXX".
With a little use of private API it is possible
let device = UIDevice.current
var selector = NSSelectorFromString("deviceInfoForKey:")
if !device.responds(to: selector) {
selector = NSSelectorFromString("_deviceInfoForKey:")
}
if device.responds(to: selector) {
if let unmanagedModel = device.perform(selector, with:"ModelNumber") {
let model = unmanagedModel.takeRetainedValue() as! String
print("Device hardware model: \(model)")
}
}
let myDevice = UIDevice.currentDevice()
let deviceModel = myDevice.model
Related
My app has a local database of contacts not stored in the internal contact store (users can however choose to add the contacts to the internal contact store)
I'm using the CNContactViewController to show the details of the contacts - but the image is not (always) stored in the database and has to be loaded (asynchronous) on each request.
A minimalized version of the ContactModel class:
class ContactModel {
var id: String
var givenName: String?
var thumbnail: Data?
init?(_ identifier: String?, givenName: String? = nil)
{
if ((identifier ?? "").isEmpty) {
return nil
}
self.id = identifier
}
func toMutableContact() -> CNMutableContact
{
let contact = CNMutableContact()
contact.contactType = .person
contact.givenName = givenName
if let thumbnail = thumbnail {
contact.imageData = thumbnail
}
return contact
}
}
The following code shows the contact details and gets the thumbnail from an online service
func showContact(_ model : ContactModel)
{
// Create a 'CNMutableContact' from the 'ContactModel' object
let contact : CNMutableContact = model.toMutableContact()
let store = CNContactStore()
let cvc = CNContactViewController(forUnknownContact: contact)
cvc.delegate = self
cvc.contactStore = CNContactStore()
cvc.allowsEditing = false
self.navigationController?.pushViewController(cvc, animated: true)
var hasThumbnail: Bool = false
if let _ = model.thumbnail {
hasThumbnail = true
}
if !hasThumbnail {
// Get the thumbnail from the online service
getThumbnail(id: model.id) { (data, error) in
// Everything above works as expected!
// This does not work either
contact.givenName = "XXX"
if let data = data {
// How can I update the image ?
print("imageDataAvailable (before): \(contact.imageDataAvailable)") // returns: false
contact.imageData = data
print("imageDataAvailable (after): \(contact.imageDataAvailable)") // returns: true
}
}
}
}
I'm not going to save the contact to the internal contact store - just using the CNContactViewController to view the details.
If the image is set in the ContactModel then the image is shown perfectly - but if any values are changed afterwards then the view is not updated.
(The user can choose not to save the thumbnails to the local database due to space consumption)
You can't do that. The contact displayed by the CNContactViewController is an immutable contact with no connection to the CNMutableContact you created at the outset (it's a copy).
My app was rejected because there is a problem on the iPad UI of my app. I have fixed it before this getting rejected by checking if the device was an iPad to make some minor adjustments to the UI measures. The problem is that when I call:
UIDevice.currentDevice().userInterfaceIdiom //returns .Unspecified
traitCollection.userInterfaceIdiom //returns -1
On a real iPad I get .Unspecified. Why is this?
In order to fix this I have created a custom method which actually works on a real iPad
public extension UIDevice {
func isIpadDevice() -> Bool {
let identifier = deviceIdentifier()
return identifier.lowercaseString.containsString("ipad")
}
private func deviceIdentifier() -> String {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8 where value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
return identifier
}
}
In order to use it call
UIDevice.currentDevice().isIpadDevice() //wohoo
I am using the new version of retrieving contacts from the Phonebook on iOS. There is a problem with the unique identifier, which is returned by the first query. Normally the unique identifier is a UUID, but sometimes it is appended by “: ABPerson”.
Querying the Phonebook API with this identifier works sometimes, but not always. Does anybody know, if there is a way to avoid this behaviour?
Currently, I try with a safeguard of two queries. The first is using the identifier as is, the second (if the first fails) is to use the identifier stripping the “: ABPerson” extension.
The first query used is:
class func getAllPhoneBookContacts() -> [PhonebookContact] {
let contactStore = CNContactStore()
var contacts = [PhonebookContact]()
PhoneBookContactsHelper.requestForAccess { (accessGranted) -> Void in
if accessGranted {
let keys = [CNContactIdentifierKey, CNContactPhoneNumbersKey]
do {
let fetchRequest = CNContactFetchRequest(keysToFetch: keys)
try contactStore.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (contact: CNContact, _) -> Void in
for phoneNoLab in contact.phoneNumbers {
if let phoneNo = phoneNoLab.value as? CNPhoneNumber,
normalizedPhoneNumber = PhoneNumberNormalizer.normalizePhoneNumber(phoneNo.stringValue) {
let pbc = PhonebookContact(contactID: contact.identifier, phoneNumber: normalizedPhoneNumber)
contacts.append(pbc)
}
}
})
}
catch {
NSLog("Unable to fetch contacts.")
}
}
}
return contacts
}
Accessing a certain contact again later by the identifier is done by:
class func getContactNameByUUID(identifier: String) -> String?{
var name : String?
PhoneBookContactsHelper.requestForAccess()
{ (accessGranted) -> Void in
if accessGranted {
let contactStore = CNContactStore()
let keys = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName)]
do {
let cnc = try contactStore.unifiedContactWithIdentifier(identifier, keysToFetch: keys)
name = CNContactFormatter.stringFromContact(cnc, style: .FullName)!
}
catch _ {
NSLog("Could not fetch contact with id \(identifier))")
}
}
}
return name
}
I am using iOS 9 and tested on simulator and various iPhones and the unexpected behaviour is present everywhere.
My crashlog is here.
This crash occurs on some of the testers. One tester with iPhone 6 (iOS 8.4.1) crashes but another tester with the same device (also iOS 8.4.1) doesn't crash. It also works on simulator.
I know this has been asked before but I think the problem is about ABPeoplePickerNavigationController. Or maybe this is a problem about Crashlytics.
What I wonder is: why do the same devices with the same operating system works differently? Also, I appreciate any solution to this problem?
Here is my code:
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
if property != kABPersonPhoneProperty {
return
}
let phoneNumbers: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
if (ABMultiValueGetCount(phoneNumbers) > 0) {
let index = ABMultiValueGetIndexForIdentifier(phoneNumbers, identifier)
let selectedPhoneNumber = ABMultiValueCopyValueAtIndex(phoneNumbers, index).takeRetainedValue() as! String
let newNumber = "tel:\(phoneNumber)"
println(newNumber)
let url = NSURL(string: newNumber)
if UIApplication.sharedApplication().canOpenURL(url!) {
//test
//UIApplication.sharedApplication().openURL(url!)
} else {
return
}
} else {
return
}
}
Trying to get the SSID of current device. I have found plenty of examples on how to do it however I am struggling with getting the CNCopySupportedInterfaces to autocomplete. I have 'import SystemConfiguration' at the top of my swift file but no success. Can't seem to figure out what I am doing wrong.
iOS 12
You must enable Access WiFi Information from capabilities.
Important
To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Access WiFi Information entitlement to your entitlements file and App ID. Documentation link
You need: import SystemConfiguration.CaptiveNetwork
Underneath the covers, CaptiveNetwork is a C header file (.h) that is within the SystemConfiguration framework:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/SystemConfiguration.framework/Headers/CaptiveNetwork.h
If you know Objective-C, this goes into more depth:
iPhone get SSID without private library
You have to use the awkward syntax to bridge from any pure C API, so the following is required:
for interface in CNCopySupportedInterfaces().takeRetainedValue() as! [String] {
println("Looking up SSID info for \(interface)") // en0
let SSIDDict = CNCopyCurrentNetworkInfo(interface).takeRetainedValue() as! [String : AnyObject]
for d in SSIDDict.keys {
println("\(d): \(SSIDDict[d]!)")
}
}
ADDENDUM FOR SWIFT 2.2 and 3.0
The CFxxx datatypes are now bridged to native Objective-C runtime, eliminating the head-scratching retain calls. However, nullable pointers give rise to Optionals, so things don't get any shorter. At least, it's fairly clear what's going on, plus the nil helps us identify the simulator. The other answer uses an awful lot of bit-casting and unsafe operations which seems non-Swiftian, so I offer this.
func getInterfaces() -> Bool {
guard let unwrappedCFArrayInterfaces = CNCopySupportedInterfaces() else {
print("this must be a simulator, no interfaces found")
return false
}
guard let swiftInterfaces = (unwrappedCFArrayInterfaces as NSArray) as? [String] else {
print("System error: did not come back as array of Strings")
return false
}
for interface in swiftInterfaces {
print("Looking up SSID info for \(interface)") // en0
guard let unwrappedCFDictionaryForInterface = CNCopyCurrentNetworkInfo(interface) else {
print("System error: \(interface) has no information")
return false
}
guard let SSIDDict = (unwrappedCFDictionaryForInterface as NSDictionary) as? [String: AnyObject] else {
print("System error: interface information is not a string-keyed dictionary")
return false
}
for d in SSIDDict.keys {
print("\(d): \(SSIDDict[d]!)")
}
}
return true
}
Output on success:
SSIDDATA: <57696c6d 79>
BSSID: 12:34:56:78:9a:bc
SSID: YourSSIDHere
In Swift 2.0 / iOS 9 the API CaptiveNetwork is (nearly) gone or depreciated. I contacted Apple regarding this problem and I thought we could (or should) use the NEHotspotHelper instead. I got a respond from Apple today: One should continue to use CaptiveNetwork and the two relevant APIs (even tough there marked depreciated):
CNCopySupportedInterfaces
CNCopyCurrentNetworkInfo
The user braime posted an updated code-snippet for this problem on Ray Wenderlich forums:
let interfaces:CFArray! = CNCopySupportedInterfaces()
for i in 0..<CFArrayGetCount(interfaces){
let interfaceName: UnsafePointer<Void>
= CFArrayGetValueAtIndex(interfaces, i)
let rec = unsafeBitCast(interfaceName, AnyObject.self)
let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)")
if unsafeInterfaceData != nil {
let interfaceData = unsafeInterfaceData! as Dictionary!
currentSSID = interfaceData["SSID"] as! String
} else {
currentSSID = ""
}
}
Works perfect for me.
Swift:
import SystemConfiguration.CaptiveNetwork
func currentSSIDs() -> [String] {
guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
return []
}
return interfaceNames.flatMap { name in
guard let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String:AnyObject] else {
return nil
}
guard let ssid = info[kCNNetworkInfoKeySSID as String] as? String else {
return nil
}
return ssid
}
}
Then print(currentSSIDs()), not working on simulator, only real devices.
Taken from https://forums.developer.apple.com/thread/50302
func getInterfaces() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
break
}
}
}
return ssid
}
In iOS 12 and up you will need to enable the Access WiFi Information capability for your app in order to get the ssid