Write/Update data SQLite database in iOS - ios

I have an application made ​​in air in Flash Builder for iOS plataform
I've compiled a version and installed it on my iPhone, the app talks to the database but it does not write/update in database. What can be the problem? Permission of the device or the path of the database?
thanks for now.
code:
private var dbFile : File;
private var dbPath : String = "SAG.sqlite"; //name of database
private function setaAPP():void{
if(app == 'ANDROID'){
dbFile = new File('/mnt/sdcard/SAG/' + dbPath); // to ANDROID
}else if(app == 'DESKTOP'){
//app == DESKTOP
dbFile = File.applicationDirectory.resolvePath(dbPath); // to DESKTOP
}else if(app == 'DEBUG'){
//only to debug in Android
dbFile = File.applicationDirectory.resolvePath(dbPath); // to DEBUG in device
}else if(app == 'IOS'){
//app == IOS
dbFile = File.applicationStorageDirectory.resolvePath(dbPath); // to iOS
}
}

For iOS, I'm guessing you want this:
dbFile = new File(dbPath);
Though you could probably also use this:
dbFile = File.applicationDirectory.resolvePath(dbPath);
From Apple Documentation:
In an iOS application, nonlocalized resources are located at the
top-level of the bundle directory, along with the application’s
executable file and the Info.plist file.
The storage or "documents" folder is going to be empty until you write files there at run time (also via iCloud syncing).

If the structure is the same as your ANDROID or DEBUG projects (i.e. you included the db in the root application directory when packaging), you should be able to use File.applicationDirectory.resolvePath(dbPath) just as you did in those cases.
EDIT:
The app directory is not writable, so if you want to write to the database, you will need to move the file to the Documents directory, which you can access with var docs:File = new File(File.userDirectory.nativePath + '/Documents');

Related

how i can crate folder with custom name in iphone using fluttter

I want to create folder in iPhone mobile (on my phone storage) using flutter,
i complete it in androids mobile but in iOS mobile i cant do it.
when i try to get the using getApplicationSupportDirectory() its return my mac storage path not the simulator storage path
if (!isTemporaryStorage) {
if (Platform.isIOS) {
var iosPath= await getApplicationSupportDirectory();
path = File('${iosPath.path}/Majd_Eng_folder/$fileName');
} else {
path = File('/storage/emulated/0/Majd_Eng_folder/$fileName');
}
Change the getApplicationSupportDirectory to getApplicationDocumentsDirectory for iOS. If you want to hide data's then try getLibraryDirectory
Note: Don't forgot to add permissions in info.plist

How to download a file in iOS (Ionic 2/3)?

I am trying to download a file and save that file in iOS. I am using ionic-native-file plugin to achieve this. The file has been download but i could not find that file in the device.
filewrite = (): void => {
let transfer = this.fileTransfer.create();
let path = "";
let dir_name = 'Download';
let file_name = "Sample.pdf";
if (this.platform.is('ios')) {
this.platform.ready().then(() => {
path = this.file.documentsDirectory;
this.file.writeFile(path,file_name,this.pdfSrc).then((entry) => {
this.showAlert("download completed");
}, (error) => {
this.showAlert("Download Failed.");
}).catch(err=>{
this.showAlert("Download Failed catch.");
this.showAlert(err.message);
});
}
)
}
};
Download completed alert shows and the downloaded path is:
file:///var/mobile/Containers/Data/Application/6DE22F30-5806-4393-830A-14C8A1F320BE/Library/Cloud/Sample.pdf
But i could not find that location in the device. Then, i google and saw here.
cordova.file.documentsDirectory - Files private to the app, but that
are meaningful to other application (e.g. Office files). Note that for
OSX this is the user's ~/Documents directory. (iOS, OSX)
So, i can't see the file actually as it is private to the application. Then i saw in the same link:
So, i tried with the both preference in my config.xml but nothing happened.
Is there any approach to download the file iOS or to dropbox or anywhere else?
I've struggled to find the right directory to save to on different platforms with this cordova plugin too. I landed on using file.externalRootDirectory on Android, and file.cacheDirectory on iOS.
The right location likely depends on what you intend to do with the file. In my case I just needed it stored short-term so I can open it using the user's native PDF reader.

How to move/copy a file from one one location to the other using phonegap

I am using the Phonegap File API but am having trouble copying a file from one location to the other.
my targeted device is iOS(ipad)
my iOS version is 9.2.1
I am working on windows platform
I am using phonegap build
my requirement :
I am capturing a video and its getting saved in some temp folder by default here is the location where its getting saved (/private/var/mobile/Containers/Data/Application/2183897E-3660-4145-A822-76F5B763E48D/tmp/capture-T0x17e7cae0.tmp.avqBi2/capturedvideo.MOV)
So I just want to move this (capturedvideo.MOV) video to photo album location in my ipad
This is my code which i am trying to make it work.
function success(entry) {
console.log("New Path: " + entry.fullPath);
}
function fail(error) {
alert(error.code);
}
function moveDir(entry) {
var parent = document.getElementById('parent').value,
parentName = parent.substring(parent.lastIndexOf('/')+1),
newName = document.getElementById('newName').value,
parentEntry = new DirectoryEntry(parentName, parent);
// move the directory to a new directory and rename it
entry.moveTo(saveToPhotoAlbum, newName, success, fail);
}
Any help or working examples would be great.
Thanks
Nik`
This is simply not possible on iOS, because Apple does not allow to move videos, images or any other files out of the app sandbox to other folders. The reason that the standard-camera on the iPad/iPhone is able to place pictures there is simply because this is a built-in feature that has special permissions which your third-party app will not be able to get.
This is of course only true as long as the device is not jailbroken.
For more infos, especially concerning the file-system layout and what you are allowed to store, you should take a look at the cordova-file-plugin Readme on Github:https://github.com/apache/cordova-plugin-file

iPhone 4S - UnAuthorized Access Exception

This is a question I am sure Xcode developers could also answer. I have a screenshot of my code below in Xamarin.
Why am I getting an Unauthorized access exception? I should be able to write to the documents folder right?
var webClient = new WebClient();
//var documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // iOS 7 and earlier
var documentsFolder = NSBundle.MainBundle.BundlePath;
var fileNameAndPath = Path.Combine (documentsFolder, "verses.xml");
if(!File.Exists(documentsFolder)){
Directory.CreateDirectory(documentsFolder);
if(!File.Exists(fileNameAndPath)){
//File.SetAttributes(fileNameAndPath, FileAttributes.Normal);
File.Create(fileNameAndPath);
//Throws exception here.
}
}
And ERRORs:
Access to the path "/var/mobile/Applications/1F95D694-BBA5-4FB3-AE6C-0C2CDD9DEDD8/comexample.app/verses.xml" is denied
Access to the path '/private/var/mobile/Applications/1F95D694-BBA5-4FB3-AE6C-0C2CDD9DEDD8/Documents/verses.xml' is denied.
I have tried both paths and I get access denied.
The BundlePath is not a writable area for iOS applications.
From the Xamarin notes at http://developer.xamarin.com/guides/ios/application_fundamentals/working_with_the_file_system/
The following snippet will create a file into the writable documents area.
var documents =
Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); // iOS 7 and earlier
var filename = Path.Combine (documents, "Write.txt");
File.WriteAllText(filename, "Write this text into a file");
There's a note on the page for iOS 8 that a change is required to get the documents folder
var documents = NSFileManager.DefaultManager.GetUrls (NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomain.User) [0];
Assuming you're on iOS 8, the documents directory isn't connected to the bundle path. Use the function NSSearchPathForDirectoriesInDomains() (or URLsForDirectory:inDomains:) to find the documents directory.
I assume it should be a Directory.Exists() where you check if the directory exists:
if(!File.Exists(documentsFolder)){

Xamarin iOS swap image in bundle at runtime

I want to allow my app to download images from the web to be placed in /images and then displayed in the UI - this is to allow branding specific images to be downloaded for a multitenant app.
The app downloads the image fine and places the image into the /Images folder
However when trying to load the image using UIImage.FromFile it always returns null - even though I know full well the image is there!
I know UIImage.FromBundle caches stuff so I chose to use FromFile but this doesn't seem to work?!
Is there something I need to do to make this work? Or am I pushing my luck a little?
I am using the following to create the file path:
public static UIImage SafeClaim_Logo = UIImage.FromFile(NSBundle.MainBundle.BundlePath +"/Images/Logo.png");
The following is the code I use to download and save the file - the file path from the above is the same below when I inspect it
HttpClient client = new HttpClient ();
var clientResponse = await client.GetByteArrayAsync(url);
using (FileStream fs = new FileStream(NSBundle.MainBundle.BundlePath + path, FileMode.OpenOrCreate))
{
var bytes = clientResponse;
fs.Write(bytes, 0, bytes.Length);
}
The app bundle is read-only so you can't change or add something in there at runtime.
All content created at runtime should be stored in your documents or cache folders.
You can get the documents folder by calling:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
One good place to store this kind of files is the Library/ folder. On iOS 7 you can access it using:
var libraryFolderPath = Path.GetFullPath(
Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments), "..", "Library"));
On iOS 8:
var url = NSFileManager.DefaultManager.GetUrls (
NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User) [0];
var libraryFolderPath = url.Path;
The reason is explained by Xamarin documentation:
The Library directory is a good place to store files that are not
created directly by the user, such as databases or other
application-generated files. The contents of this directory are never
exposed to the user via iTunes.
You can create your own subdirectories in Library; however, there are
already some system-created directories here that you should be aware
of, including Preferences and Caches.
The contents of this directory (except for the Caches subdirectory)
are backed up by iTunes. Custom directories that you create in Library
will be backed up.
If you need to enable iTunes file sharing and expose the content of the Documents/ folder, your files are not visible to the user.

Resources