iOS iCloud document change notification - swift

Can you, in Swift get a change notification when a document in the apps's iCloud Drive container changes?
For example I am testing a simple concept where I am storing all app data as a json string in the app's iCloud Drive container and I am loading it when the app launches. I can retrieve and save the json string on demand on multiple devices, but I am wondering if there is a way to get a change notification sent to an app when the file is changed on another device.
If so, some direction would be appreciated.

If you are using UIDocument, you can register for UIDocumentStateChangedNotification (for a particular document), otherwise if you are looking for changes in the App's iCloud container you need to use NSMetaDataQuery which is described in Discovering an App's Documents in Document-Based App Programming Guide for iOS

Related

How do I save text to a flutter web app's website subdirectory and then have the app read it later?

So I have an app on website.com/app
I want the app to save a text variable, and then have the app parse the same text variable to use it later, in a different session, or even on a completely different computer.
It would be even better if website.com/app2 could access the file, but the first case is fine as well.
Would shared preferences work for this, or does every user have one shared preferences? Or does it get wiped when the website is closed?
Thanks!
What you are talking about is cross device sharing. SharedPreferences can only help you in case of a single device.
If you want to persist your value against a user across devices, you need to use some backend storage mechanism. Something like firestore which is beginner friendly.
More on it here.

Is there a path every app can write files in the jailbreak iPhone?

I should hook UIResponder of every app, including SpringBoard and any others. In the hooking, I will write something to the specified file. If I set the path to /var/mobile/Library/MyApp, recommended by Cydia, I found that only the SpringBoard and MyApp could write successfully.
So is there a place every app can write and read?
I admit that I'm not 100% sure on this one, but my guess would be no, there is not a path that every app can writes files to on a jailbroken iPhone.
Certainly, jailbreak apps (installed in /Applications/) on a jailbroken phone can write to locations that can be shared between those jailbreak apps. But, as I understand your question, you would like to inject code into normal, App Store apps, so that those apps can also read and write to the shared location. That part I don't think is possible, because jailbreaking does not completely disable the sandbox for 3rd-party apps installed normally, under /var/mobile/Applications/.
Now, there might be a workaround. There are some shared folders that are accessible to all apps for certain purposes. For example, any app can write images to the saved photos album. What you could try is to take the content of the file you want to write, and encode it as fake image data, in a UIImage (e.g. with [UIImage imageWithData:]). You'd probably need to add a valid image header to the data. Then, you save the file to the photos album, using something like
writeImageToSavedPhotosAlbum:orientation:completionBlock:.
Another app could then find the fake photo by enumerating the saved photos album, and then converting the asset back to image representation to pull the real data back out.
However, this seems quite complicated, and possibly wouldn't work (I haven't tried it). Perhaps you could tell us why you want this shared file. Maybe there's a better way to share the data, without using a globally-accessible file?
Notifications can help you with this. Every app will send interprocess notifications about the events. You could start a daemon that will listen for this notifications and save them in a file. Or you could listen for them in SpringBoard as he can write, for example, to /var/mobile/Media. Depends on what you want to do with this file. Check out my answer here How to create a global environment variable that can be accessed by SpringBoard or other applications in the jailbroken iPhone?

Access and save location information when app run in background - iOS

So my question is whether an app, which is running in the background, can access the device's location information and save it in the heap or send data to a server?
I know it would have to do something with the delegate but I'm not sure a process of this complexity can be done while the app is in the background
Yes, you can!
In the Location Awareness Guide of apple its is described that application that need to receive GPS in the background have to set a specif value:
Set key location in the UIBackgroundModes array of your Info.plist file.
Yes. You can do it.
By setting the proper key-value in info.plist file, your application will able to fetch locations even when its in background.
Using ASIHttpRequest (Link), you can upload the data using web service.
For that, create the object of ASIHTTPRequest and keep the value of shouldContinueWhenAppEntersBackground to TRUE.

How can I store image to iCloud from my iphone application?

I got a project like MyPhoto Pro app in iPhone App Store (website).
Please, can any one help or guide me what are the technologies and frameworks i should use for developing?
But my primary things is that image should stored in iCloud.
For iCloud file storage see Designing for Documents in iCloud in docs.
Then check NSFileManager class, section Managing ICloud-Based Items.
-ubiquityIdentityToken – detect iCloud availability
-URLForUbiquityContainerIdentifier: – obtain URL for iCloud directory (container)
-setUbiquitous:itemAtURL:destinationURL:error: – move any local file to iCloud (or vice versa); one URL must be local and one must point into one of the iCloud containers
-startDownloadingUbiquitousItemAtURL:error: – download the file content
-evictUbiquitousItemAtURL:error: – delete iCloud file.
Also check NSMetadataQuery and NSMetadataItem classes used to discover new iCloud files. iCloud will automatically give you only metadata of the file, but content must be downloaded explicitely (see the methods above)

Is there a way to completely disconnect an App from iCloud?

My App can work with or without iCloud support.
If a user connects a device to iCloud and then wants to disconnect it, it's pretty easy for me to copy all data back to the local sandbox and stop using iCloud, and this device won't contribute anything new to iCloud anymore. However, changes from other devices will still be received (although not handled) on this device.
Is there a way to completely disconnect the device from iCloud, so that new changes won't be received?
iCloud stores data in a folder called "Mobile Documents." Your app's container resides in this folder. iOS devices know about new files and changes immediately. However, they do not actually download the file until the app specifically requests it. Here's an example scenario:
Someone is running your app on their iPhone and their iPad. They use iCloud on both. However, on their iPhone they disable your app's iCloud but leave their iCloud account active. This means that their device always knows about changes. But since your app never requests those documents, they are never downloaded to the device and therefore do not take up space. Also, iOS will automatically remove the local copy of an iCloud file to free up space if necessary.
For more information, see developer.apple.com/icloud, specifically the videos on how to use iCloud.
You can simply stop responding to the NSNotifications received by your app. You can either unregister your views from these notifications, or ignore them when they're received.