I've spent two days no reading and testing as there is a lot of info about this topic.
Unfortunately I've found no solution yet. I can't implement my own authentication as this doesn't help with the issue I want to solve (see Backgrounding at the end of the question).
Here is my current best approach:
I'm generating a UUID thanks to https://stackoverflow.com/a/8677177/1443733 and storing it in the KeyChain as suggested with SwiftKeychainWrapper (https://github.com/jrendel/SwiftKeychainWrapper)
The short nice and sweet code for that is:
let stored = KeychainWrapper.stringForKey("UUID")
if stored != nil {
Helper.log(TAG, msg: "retrieved from keychain: \(stored!)")
} else {
let theUUID = CFUUIDCreate(nil)
let str = CFUUIDCreateString(nil, theUUID)
Helper.log(TAG, msg: "generated UUID: \(str)")
let ret = KeychainWrapper.setString(str, forKey: "UUID")
Helper.log(TAG, msg: "setkeychain: \(ret)")
}
But the UUID stored in the keychain seems to be per device and not per store ID as well.
When I store the UUID like above and login with a different Store ID on the device KeychainWrapper.stringForKey("UUID")still returns the value of the other user.
Isn't their a way to store a value in a store-id keychain?
It seems that I'm so close so I hope someone could point me in the right direction.
If that approach (with a keychain) can't succeed please let me know as well.
I reckon you can ask a different question as well: Is there some cryptic data I can read/generate or a store which changes with the Store Id currently used on a device?
Ohh... and Swift examples preffered ;)
Backgroundinfo:
I use IAPs in my app and want to save the Store-Id of the user once a refresh of the receipt is valid.
On each start of the app I check if the current Store-Id is the same as the saved one. If not I trigger immediately a refresh of the receipt. If it fails I fall back to the free version of the app.
iOS devices do not support multiple users.
If you want to differentiate between users you will have to do that in your app, perhaps with a user login. Then save a UUID per userID in the Keychain.
As NSUserdefaults temporarily stores the UUID.so,after unistalling the app and again installing,the UID changes. So, UUID must be stored in keychain
Related
So, I have been coming across a problem where my Firebase app does not update user values when a user makes an update. To be more clear: Lets say user 1 has a photo of a dog and then changes it to a cat.
Once they change it to a cat, my node value in Firebase is successfully updated but the user themselves won't be able to see the change in other previously loaded areas in the app (other places with the dog picture) until they log out and then log back in.
For this reason I was wondering if there was any way to conduct a background app refresh that way all previous dog values in the app are changed to cat values without the user having to log out and then log back in. Please note that this same problem occurs not only with my user's profile picture but also any other user field I have setup.
Here is how I am updating a node value for my user in Firebase:
let storageRef = FIRStorage.storage().reference()
_ = FIRStorageMetadata()
let filePath = "\(FIRAuth.auth()?.currentUser?.uid)/\("userPhoto")"
let profileImageData = UIImageJPEGRepresentation(self.profilePicture.image!, 1.0)
if let data = profileImageData {
storageRef.child(filePath).put(data, metadata: nil){(metaData,error) in
if let error = error {
print(error.localizedDescription)
return
} else {
let downloadURL = metaData!.downloadURL()!.absoluteString
let userPhotoUpdateRef = FIRDatabase.database().reference().child("users").child(self.currentUser).child("userPhoto")
userPhotoUpdateRef.setValue(downloadURL)
}
}
}
If you have any questions please ask! Any help would be appreciated!
The Firebase SDK for Cloud Storage provides an easy way to read file from and write files to cloud storage. It does not provide a way to monitor those files.
The easiest way to provide a monitoring approach is to write the metadata of the files to the Firebase Realtime Database. See this short section in the Storage docs for a brief mention of that: https://firebase.google.com/docs/storage/ios/file-metadata#custom_metadata
When you write data to a location in the Firebase Database, all apps that are actively monitoring that location will be instantly updated. When they get that update, you can reload the image from Cloud Storage for Firebase.
I can get CKSubscription work using CKNotificationInfo() and CKNotificationInfo.alertBody. So I can send one piece of information. I'm trying to make CKSubscription send the user something like message, username, location, etc in a dictionary format. I've dabbled with CKNotificationInfo.alertLocaliztionKey and CKNotificationInfo.alertLocaliztionArgs but just can't seem to make it work. It feels like i'm missing something small because CKSubscription shouldn't be this troublesome to make it work.
Because that is not what is intended in the notification framework. What you do get back is information about WHAT has changed, and then you have to fetch this data and do what ever you want to do. I have made an app which both tells the user that something has changed and silently in the back refreshes the local data:
let cloudKitNotifiction = CKQueryNotification(fromRemoteNotificationDictionary: uif)
if cloudKitNotifiction.notificationType == CKNotificationType.Query{
if let recordId = cloudKitNotifiction.recordID{
let ccs = CloudCoreSynchronizer()
ccs.syncOneCustomerFromCloudToCore(recordId)
resetBadgeCounter()
}
}
To make this work you have to enable push notifications and background modes, if you want it to happen when the app is in the background.Hope this helps. PS: Just disregard the inapp purchase thing, it has nothing to do with this
I implemented this code https://github.com/exchangegroup/keychain-swift to make use of the keychain. My question as a bloody beginner:
I use parse.com as API.
I would like to save username, password and nickname to the keychain in order to login the user automatically. What works right now quite well, but when I try to print out the nickname with
let KeyChainNickname = "LocalNickname"
let currentNickname = TegKeychain.get(KeyChainNickname)
self.helloUserLabel.text = "Welcome \(currentNickname)"
I get this: Welcome Optional("MyNickname") which is the right nickname but surrounded by Optional("")
How can I print out the name without that Optional("")?
As I use parse.com I could also just pin the user data, where is really the difference between Parse local data store and the keychain? Beside the fact, that the keychain can by synced to other ios devices? Thats why I would prefer the keychain method.
Thanks so much!!
Add this ! in your code like this:
let currentNickname = TegKeychain.get(KeyChainNickname)!
self.helloUserLabel.text = "Welcome \(currentNickname)"
But you can use this ! if you are sure that currentNickname will defiantly get a value.
And your output will be "MyNickname".
EDIT:
if let currentNickname = TegKeychain.get(KeyChainNickname){
self.helloUserLabel.text = "Welcome \(currentNickname)"
}
You can try this too.
Just trying to update some Core Data apps with Continuity and have run into a bit of an issue with using the selected objects ID in the userInfo dictionary to display the correct data on the continuing device.
My first thought was to use the ObjectID, however on the receiving device this would never find a corresponding object in the Core Data store.
As it turns out the URL representation of the objectID contains the UUID of the store itself, and because the two stores UUID's are different this is obviously going to fail.
So I guess I could replace the Core Data store's UUID in the URL with the continuing devices UUID and use this, and no doubt it would work.
The Url seems to be of the following format
Does anyone know what the correct way would be to pass a reference to an object between two devices with core data stores that are synchronised via iCloud?
I'll answer this one myself and see if there are any better answers...
I pass the url of the objectID (from objectID.URIRepresentation) using Continuity API and on the receiving device create a new URL using the following:
url is the url passed in the NSUserActivity.userInfo dictionary
let storeUUID = self.identifierForStore()
// Switch the host component to be the local storeUUID
let newURL = NSURL(scheme: url.scheme!, host: storeUUID, path: url.path!)
func identifierForStore()->NSString? {
if let store = self.persistentStoreCoordinator?.persistentStores[0] as? NSPersistentStore {
return store.identifier
} else {
return nil
}
}
This seems to work just fine - hope it helps someone
Goal is to update audio metadata with iTunes store for files which only reside on the iCloud.
I found a handy Ruby script which would perform the task if the file was re-downloaded locally http://cl.ly/C3kK
The script enlightened me to the itunes store api, however, I still need the itunes store trackId which is not to be confused with the local/internal trackId or persistentId. The above script reads in the first MB of the physical audio file looking for a magic number and storing the subsequent integer. I peeked at the itunes music library.xml with no luck. Itunes is storing this information somewhere I would think. Or at least the another ID can be used to retrieve the metadata from the iCloud.
In the end I would simply update the itunes music library.xml with the results from the itunes store api.
I realize there are iCloud iTunes api calls, but before I delve into that subject I would rather post a question to the experts.
Any help on the subject would be amazing.
I know it's been a while since you posted this question, but I encountered the same issue you had and was able to figure it out. The iTunes store trackId is stored in the downloaded version of an iTunes matched file. If you grab the first 1024 bytes of data from the file, the track id is the first 4 bytes of data after the string 'song'. You'll need to convert it to a decimal from a signed 32-bit integer.
Example: No Cars Go (Arcade Fire)
file_string = File.open(path, 'r').read(1024)
index = file_string.index('song')
#iTunes_id = file_string[index+4,4].unpack('N')[0]
print "Song ID: #{#iTunes_id}"
Results in:
Song ID: 81607936
Now, you can take that ID and look up the track data from iTunes:
https://itunes.apple.com/lookup?id=81607936&country=US
Results in:
{
"resultCount":1,
"results": [
{"wrapperType":"track", "kind":"song", "artistId":23203991, "collectionId":81607965, "trackId":81607936, "artistName":"Arcade Fire", "collectionName":"Arcade Fire EP", "trackName":"No Cars Go", "collectionCensoredName":"Arcade Fire EP", "trackCensoredName":"No Cars Go", "artistViewUrl":"https://itunes.apple.com/us/artist/arcade-fire/id23203991?uo=4", "collectionViewUrl":"https://itunes.apple.com/us/album/no-cars-go/id81607965?i=81607936&uo=4", "trackViewUrl":"https://itunes.apple.com/us/album/no-cars-go/id81607965?i=81607936&uo=4", "previewUrl":"http://a644.phobos.apple.com/us/r1000/060/Music2/v4/ba/95/be/ba95be41-4a03-4dea-2965-57dd5f0b66c0/mzaf_6092501386391248238.m4a", "artworkUrl30":"http://a5.mzstatic.com/us/r30/Music/y2005/m10/d01/h10/mzi.yfrupnuj.30x30-50.jpg", "artworkUrl60":"http://a2.mzstatic.com/us/r30/Music/y2005/m10/d01/h10/mzi.yfrupnuj.60x60-50.jpg", "artworkUrl100":"http://a5.mzstatic.com/us/r30/Music/y2005/m10/d01/h10/mzi.yfrupnuj.100x100-75.jpg", "collectionPrice":6.93, "trackPrice":0.99, "releaseDate":"2005-01-10T08:00:00Z", "collectionExplicitness":"notExplicit", "trackExplicitness":"notExplicit", "discCount":1, "discNumber":1, "trackCount":7, "trackNumber":3, "trackTimeMillis":364071, "country":"USA", "currency":"USD", "primaryGenreName":"Alternative", "radioStationUrl":"https://itunes.apple.com/station/idra.81607936"}]
}
I hope this helps the OP or anyone else, I wasn't able to find this information anywhere else.
I know it's been a while since you posted this question, but I encountered the same issue you had and was able to figure it out. The iTunes store trackId is stored in the downloaded version of an iTunes matched file. If you grab the first 1024 bytes of data from the file, the track id is the first 4 bytes of data after the string 'song'. You'll need to convert it to a decimal from a signed 32-bit integer.