Swift Share CoreData between App and Today Extension [duplicate] - ios

This question already has an answer here:
How to access CoreData model in today extension (iOS)
(1 answer)
Closed 6 years ago.
I want to access my CoreData from my Today Extension Widget to display some data. I already read that I have to create an app group and add this one to my App and my Widget. I already have done this, but now I'm not sure what to do next. I found an old tutorial but the methods used there aren't available or I'm not able to find them in AppDelegate. Can anybody help me please?

You need to get the URL of the group container with containerURL(forSecurityApplicationGroupIdentifier: of FileManager passing the container identifier in both targets:
lazy var secureAppGroupPersistentStoreURL : URL = {
let fileManager = FileManager.default
let groupDirectory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.mydomain.myapp")!
return groupDirectory.appendingPathComponent("databaseName.sqlite")
}()

Related

How to configure and use a bundle in Xcode for iPhone apps? [duplicate]

This question already has answers here:
How do I add an MP3 to my iOS/Xcode project?
(1 answer)
How to add sound files to your bundle in Xcode?
(4 answers)
Closed 10 months ago.
I need to use some audio content in the app I'm coding in Swift with Xcode. That's the first time I try to use a bundle with Xcode.
First I created a local folder on my Mac called "mybundle.bundle" containing my file "mysound.mp3"
I dragged and dropped it in Xcode, the bundle goes to the assets correctly.
But then things get tough. I tried different syntaxes of code to access my file in the bundle.
guard let path = Bundle.main.resourcePath!+"/bundle mybundle.bundle/mysound.mp3" else {
return print("Error")
}
That code always goes to print "Error".
Also tried this :
guard let url = Bundle.main.url(forResource: "mysound", withExtension: "mp3") else {
print("Error")
return
}
Same thing.
Is there any additional setup to do before that main bundle is available for access ?
Thanks a lot !

How can I view the contents of the iOS app document director? [duplicate]

This question already has answers here:
There's a way to access the document folder in iphone/ipad (real device, no simulator)?
(8 answers)
Closed 4 years ago.
I'm trying to troubleshoot my app, and I think my images are being saved properly to the directory, but I don't know how to view the directory to confirm. I just need to print the file list to the console. How can I do that using Swift 4?
it print all file in document Directory
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
if let allItems = try? FileManager.default.contentsOfDirectory(atPath: documentDirectory) {
print(allItems)
}
you may copy path and open in finder as well.

Not possible to use Realm in an iOS Messages extension?

Just to be clear, its currently not possible to use Realm to have a single shared database between an iOS app and a Messages extension?
It is very much possible
For Swift 3
I used the following code to share Realm db between app and app extension:
let sharedDirectory: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.your.app")! as URL
let sharedRealmURL = sharedDirectory.appendingPathComponent("db.realm")
Realm.Configuration.defaultConfiguration = Realm.Configuration(fileURL: sharedRealmURL)
Just call this code from didFinishLaunchingWithOptions of AppDelegate and viewDidLoad of MSMessagesAppViewController

How to get iOS APP archive date using Swift [duplicate]

This question already has answers here:
Get build date and time in Swift
(6 answers)
Closed 5 years ago.
I have a requirement in my application that, i need to show .ipa file creation date using swift.
Can anybody tell me how to do that.
Thanks in advance.
You can get the url of your app using Bundle property executableURL and use url method resourceValues to get the bundle creation date:
if let executableURL = Bundle.main.executableURL,
let creation = (try? executableURL.resourceValues(forKeys: [.creationDateKey]))?.creationDate {
print(creation)
}

How to remove all UserDefaults data ? - Swift [duplicate]

This question already has answers here:
Delete all keys from a NSUserDefaults dictionary iOS
(18 answers)
Closed 5 years ago.
I have this code to remove all UserDefaults data from the app:
let domain = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: domain)
print(Array(UserDefaults.standard.dictionaryRepresentation().keys).count)
But I got 10 from the print line. Shouldn't it be 0?
The problem is you are printing the UserDefaults contents, right after clearing them, but you are not manually synchronizing them.
let domain = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: domain)
UserDefaults.standard.synchronize()
print(Array(UserDefaults.standard.dictionaryRepresentation().keys).count)
This should do the trick.
Now you don't normally need to call synchronize manually, as the system does periodically synch the userDefaults automatically, but if you need to push the changes immediately, then you need to force update via the synchronize call.
The documentation states this
Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.
This answer found here https://stackoverflow.com/a/6797133/563381 but just incase here it is in Swift.
func resetDefaults() {
let defaults = UserDefaults.standard
let dictionary = defaults.dictionaryRepresentation()
dictionary.keys.forEach { key in
defaults.removeObject(forKey: key)
}
}

Resources