Getting two different device IDs from same iphone - ios

I am getting different UDIDs for my iphone when i get it from itunes and programatically like this
UDID:String = UIDevice.current.identifierForVendor!.uuidString
Basically im trying to acquire a unique identifier for my iphone just like we have mac address for android phones.

one easiest way is to solve this issue by storing the identifierForVendor in keychain. even if you uninstall app ,value for the key remains same and its unchanged. many third party libraries available to perform this . one of them https://github.com/jrendel/SwiftKeychainWrapper.
func getGlobalUniqueIdentifierFromKeyChain()->String{
let retrievedString: String? = KeychainWrapper.standard.string(forKey: "DeviceId")
if retrievedString == nil{
if let deviceKey = UIDevice.current.identifierForVendor?.uuidString{
let _ = KeychainWrapper.standard.set(deviceKey, forKey: "DeviceId")
}
}
if let globalID = KeychainWrapper.standard.string(forKey: "DeviceId"){
return globalID
}else{
return UIDevice.current.identifierForVendor?.uuidString ?? ""
}
}

Related

Can I generate a QR code that contains both URL and text values?

This question has been asked, but I'm not quite asking the same thing. Using iOS Swift, I am trying to store 2 values in a QR code. One is a URL of an app on the store. The other is a string that can be picked up by that app (it has its own scanner with logic to obtain the string value). The second part works fine, as I can easily parse the whole string. I tried putting a comma between the values, and it almost works, but I get a "Can't connect to the App Store" message when I use a generic scanner. It picks up the URL and tries to connect, but the extra data seems to screw it up. If I take out the comma and string, the URL then works.
Here is a subset of my code...
override func viewDidLoad() {
super.viewDidLoad()
let payload = "https://apps.apple.com/ca/app/.../,<my string value>"
let image = generateQRCode(from: payload)
qrCodeImage.image = image
}
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output)
}
}
return nil
}
Does anyone know if this is even possible? e.g. could I use json or a vcard, or would the generic scanner not be able to pick out the URL?
You can add the data you want as a query parameter on the QR code's URL, unless there are privacy issues with the data you're appending to the URL.

SystemConfiguration.CaptiveNetwork doesn't work on iOS 12

I have a function that detects the current SSID from the user. Unfortunately this doesn't work anymore with iOS 12. This means it just jumps over the if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? { part. Maybe it's just a bug or it's deprecated. I've found nothing on Apple Docs.
On older iOS 11, 10, and 9 devices, it works well.
Here's my Code:
func getWiFiSsid() -> 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
}
}
}
return ssid
}
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.
https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo?language=objc

Retrieving value from firebase in iOS

I want to retrieve the database value on firebase in an iOS application. I've already installed firebase on my iOS project but I've tried multiple times but it shows me errors. Someone can help me?
This is an example f collecting something from the database:
let userID = FIRAuth.auth()?.currentUser?.uid
FIRDatabase.database().reference().child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { error, snapshot in
if error != nil {
print(error)
} else {
// Get user value
let test_retrieve = snapshot.value!["name"] as! String
let user = User.init(username: username)
}
})

Unable to persist 'posts' reference for 'users' in Firebase

I'm creating a social app and a common aspect of it is keeping tabs of the posts that users make. I'm using Firebase as the backend for this and here's my schema
SocialApp
posts
-KGsBG5TPYBtzRivZnbf
users
facebook:10154108240254134
cachedProfile
displayName: "Karthik Kannan"
imageURL: "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100..."
posts
-KGsBG5TPYBtzRivZnbf: true
provider: "facebook"
When i go ahead and add posts from one device(or simulator) this works as planned but when i delete the app from a device and reinstall it, the posts relationship in the users dictionary disappears. I would like it to persist across devices and keep the relationship intact so if a user decides to use another iDevice to login his posts don't get orphaned.
Here's the code I wrote for adding a post to Firebase and how I currently keep track of the user.
func postToFirebase(imgUrl: String) {
var gift: Dictionary<String, AnyObject> = [
"giftName": giftName.text!,
"giftDescription":giftDescription.text!,
"giftAvailableAt": giftAvailableAt.text!,
"giftPrice": Int(giftPrice.text!)!,
"username": NSUserDefaults.standardUserDefaults().valueForKey("uid")!,
"giftImage":imgUrl,
]
let firebasePost = DataService.ds.REF_POSTS.childByAutoId()
firebasePost.setValue(gift)
firebasePost.observeSingleEventOfType(.Value, withBlock: { snapshot in
if let postID = snapshot.key {
DataService.ds.REF_USER_CURRENT.childByAppendingPath("posts").childByAppendingPath(postID).setValue(true)
}
})
}
This is how I set current users:
var REF_USER_CURRENT:Firebase {
if let uid = NSUserDefaults.standardUserDefaults().valueForKey("uid") as? String {
let user = Firebase(url:"\(BASE_URL)").childByAppendingPath("users").childByAppendingPath(did){
return user!
} else {
return Firebase()
}
}
func createFirebaseUser(uid:String, user:Dictionary<String, AnyObject>) {
REF_USERS.childByAppendingPath(uid).setValue(user)
}
And this is the code in my login controller.
NSUserDefaults.standardUserDefaults().setValue(authData.uid, forKey: "uid")
let user = ["provider":authData.provider!,"displayName":authData.providerData["displayName"]!,"cachedProfile":authData.providerData["cachedUserProfile"]!, "imageURL":authData.providerData["profileImageURL"]!]
DataService.ds.createFirebaseUser(authData.uid, user: user)
self.performSegueWithIdentifier("loggedIn", sender: nil)
This is all the code I've written. I suspect it's something to do with NSUserDefaults. Any help would be greatly appreciated by this beginner.
Check your code. I found a typo here:
var REF_USER_CURRENT:Firebase {
if let uid = NSUserDefaults.standardUserDefaults().valueForKey("uid") as? String {
let user = Firebase(url:"\(BASE_URL)").childByAppendingPath("users").childByAppendingPath(did){
return user!
} else {
return Firebase()
}
}
Instead of "...childByAppendingPath(did)"
You might want to change "did" to "uid"
Other than that, I have not tested out the rest of your code.
Seems like You have watched the Mark Price course, for making this app.
So have I :)

Swift CNCopySupportedInterfaces not valid

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

Resources