Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to ship my ios application(written in swift) with my pre populated sqlite database which will be updated through syncing with server. In android I could move the db from assests folder to databases and volla, but in ios I'm quiet lost how can I achieve this?
Thanks
In iOS you can add the database into the Project Supporting files. This will be added in with the binary, which you can then access and copy it across.
To get the location of the pre-populated database from NSBundle:
let databasePath = NSBundle.mainBundle().URLForResource("databaseName", withExtension:"sqlite3");
Get the URL of the Documents Directory using NSFileManager:
//Should have an error pointed in case there is an error.
let documentsDirectory = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain:NSSearchPathDomainMask.UserDomainMask, url:nil, shouldCreate:false, error:nil);
Finally copy the file:
if let source = databasePath, destination = documentsDirectory
{
destination = destination.URLByAppendingPathComponent("database.sqlite3")
var result = NSFileManager.defaultManager().copyItemAtURL(source, toURL:destination, error:nil)
//Should have an error pointer, check that the result is true. If not check error.
}
The simple way is:
+ Choose the persistent URL is somewhere in your library folder/sub-folder (or documents if you want).
+ when you implement your persistentStoreCoordinator, first check if the .sqlite exists at the URL above. If not, copy your .sqlite file to that location, if yes, do nothing.
+ init your persistent store with that url.
You have to sure that the .sqlite's datas match the DB model
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I'm writing an App (in swift 5 for iOS) that reads some mqtt messages coming from a remote mqtt broker. The messages format is BSON (Binary Json). Is there any library or code that allows me to read the message in this format? With the standard library for Swift I can read and decode normal JSON Files but not BSON. Some suggestion?
Found this library: https://github.com/OpenKitten/BSON
After installed the package in Xcode (using packet manager) I was able to read the BSON file received from Mqtt broker
Here the code I used:
//At the beginning of the file
import BSON
...
...
//I use BSON Document object to read the payload of the message
let info = Document(bytes: message.payload)
//I use the function isValid to check if the format is correct
if info.validate().isValid{
//If the format is correct I can read a field of the Json (in the example "MessageType")
let messageType:String = info["MessageType"] as? String ?? ""
...
...
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.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to open Google Maps app on iOS using the url scheme for showing directions with multiple stops.
The web url for testing is:
https://google.com/maps/dir//49.54643774,22.28223445/49.54679476,22.28170513/49.54726735,22.28154318/49.54760869,22.28156607/49.54820312,22.2815506/49.54856556,22.28146425/49.54907329,22.28133231/49.54989807,22.28207924/49.55017454,22.2824851/49.55064392,22.28306989/49.5508548,22.28325003/49.55143275,22.28381447/49.55169439,22.28410868/49.5520271,22.28443534
I tried many configurations of the app scheme using comgooglemaps:// without success.
You can read the Google Maps documentation for opening the app.
To simplify it, this is the format you need to follow:
comgooglemaps://?saddr=Google,+1600+Amphitheatre+Parkway,+Mountain+View,+CA+94043&daddr=Google+Inc,+345+Spear+Street,+San+Francisco,+CA¢er=37.422185,-122.083898&zoom=10
in swift you would do something like this, take note of the callback option, you can choose to not have it if you don't want to return to your app:
let testURL = URL(string: "comgooglemaps-x-callback://")!
if UIApplication.shared.canOpenURL(testURL) {
let directionsRequest = "comgooglemaps-x-callback://" +
"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" +
"&x-success=sourceapp://?resume=true&x-source=AirApp"
let directionsURL = URL(string: directionsRequest)!
UIApplication.shared.openURL(directionsURL)
} else {
NSLog("Can't use comgooglemaps-x-callback:// on this device.")
}
Edit: To use coordinates, use it this way:
comgooglemaps://?saddr=52.3668563,4.8890813&daddr=52.357516,4.902319&zoom=10
Edit 2: For for more points on the map append coordinate using +to:Latitude,Longtitude to the daddr parameter
comgooglemaps://?saddr=52.3668563,4.8890813&daddr=52.357516,4.902319+to:52.357786,4.891913&zoom=10
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")
}()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
i am trying to save a string to device's memory for a simple to do app. It works great on a simulator where i set the path for writing to memory to be - let path: String = directory + "App4.txt". But when running this on an actual iPhone it doesn't save to memory.
This is the function.
if let directory: String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path: String = directory + "App4.txt"
userInputString = userInput.joinWithSeparator("-")
// Writing.
do {
try userInputString.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)
}
catch {
print("Error")// Error handling here.
}
}
Any help is very much appreciated!! :)
As #Martin-R is alluding to, you need to have a "/" in your path between directory and the file name.