I got a real strange problem with an iOS app I'm currently working at. The effect only exists if I test the app using ad hoc distribution. After updating the app (it has to be installed before) it wasn't working correctly. I could track the error down and it is caused by following line of code:
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:&error];
Now you could say, of course: Don't ever write to the app bundle itself, but the base path is the Documents folder via:
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
imgDir = [docsDir stringByAppendingPathComponent:#"images"];
folder = [imgDir stringByAppendingPathComponent:md5]; // md5-Hash is created before
The complete error message (logged to iphone system log) is:
Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x14d44f30 {NSFilePath=/var/mobile/Applications/280C6D36-3667-4589-A74F-42F3F17ABA71/Documents/images/39b6cd45a05a2276ef065b2ecf33b1eb, NSUnderlyingError=0x14d4e340 "The operation couldn’t be completed. Operation not permitted"}
The interesting thing is, as I noted, if I delete the app before installing via ad hoc distribution (Testflight to a iPhone 5 with iOS 7.0.4) the folder is created and the App works as expected. The only references I could find were developers not using stringByAppendingPathComponent or writing directly to the app bundle. Maybe anybody else got the problem or has an idea?
I finally found the reason why the folder could not be created. Afterwards it seems pretty simple and stupid, but if you could take a look at the complete source code you would unterstand how this could happen. In my defense I have to say that I came to this project for further development because the original developer left the project. For your better understanding I simplified the code a lot.
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
imgDir = [docsDir stringByAppendingPathComponent:#"images"];
The imgDir was actually saved in userPreferences and a proxy class always returned the saved path, which is a really bad idea. During the update process the app gets a new guid which reflects in the apps documents path. So trying to create a folder in the previous version documents folder had to fail as it doesn't exist anymore. I corrected the code to never save the path and always return the current one with the code above.
Related
Since I am a beginner in iOS development I am in a learning phase so please bear with this question.
I got an ipa from the client to install on my iPhone . However while using the app after installing through .ipa I got an error , can someone tell how can I get the logs from ipa file so that I can understand this error and pass logs to the api team? I know I can get by connecting the device and running/building through xcode directly but unfortunately code is with client and he generates the ipa. Is it possible to get the logs through ipa? I tried to use View Device Logs from Window -> Device and Simulators by selecting my device but those logs are not which I want
I found several answers like adding the below code in application:didFinishLaunchingWithOptions
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:#"%#.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
Is it possible to get logs from ipa file as we get from APK file in Android? If yes, can someone tell how to get the logs from ipa file?
With Swift or with Obj-C, I have done an app, and I store sensitive data in a specific folder in the app. Now, I would like the user not be able to see that folder and not able to copy it on his computer, or the best, to hide it with software like iMazing for example.
I tried to add a ".", this is not a solution at all: How to hide folder in NSDocumentsDirectory and disallow backup via iTunes & iCloud
I tried to store it in the Library folder, this is not a solution too, as it is accessible in iMazing: How to hide folders created in Document Directory in ios?
I don't want to use the Application supports iTunes file sharing because I need to access to the documents folder from the app with iMazing.
Does exist an intelligent and subtil solution that allows to store some files in the iPad not accessible with iMazing and not carried to the extreme as the "all or nothing" Application supports iTunes file sharing option?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:libraryDirectory isDirectory:&isDir] && isDir == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:libraryDirectory withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *pathLibToCreate = [libraryDirectory stringByAppendingPathComponent:#"testDoc"];
NSString *pathDocToCreate = [[self documentsDirectory] stringByAppendingPathComponent:#"testDoc"];
if (![[NSFileManager defaultManager] fileExistsAtPath:pathLibToCreate]) {
[[NSFileManager defaultManager] createDirectoryAtPath:pathLibToCreate withIntermediateDirectories:NO attributes:nil error:&error];
}
if ([[NSFileManager defaultManager] fileExistsAtPath:pathDocToCreate]) {
[[NSFileManager defaultManager] removeItemAtPath:pathDocToCreate error:nil];
}
NSError *copyError = nil;
if (![[NSFileManager defaultManager] copyItemAtPath:pathLibToCreate toPath:pathDocToCreate error:©Error]) {
NSLog(#"Error copying files: %#", [copyError localizedDescription]);
}
Thanks in advance.
If you use "File Sharing" permission to NO and if you use a distribution profile, you won't be able to view your files in Amazing.
I guess that it is because you use a developer profile for your app and then you're able to view these files.
I think this is not possible, at least if an user has a jailbroken device where he has access to all files on the system.
To prevent the user from having access to the content of a file, one option could be to encrypt the content of the file. Using the CommonCryptor framework that should be an easy task.
For encrypted databases you could use SQLCipher.
I'm one of the devs of iMazing. Indeed, the accepted answer is correct: iMazing and other similar tools will give access to the app's sandbox when in development. But there are 3 more cases to be aware of:
Jailbroken devices
iMazing will display all files, read/write access.
Devices running a version of iOS prior to iOS 8.3
Same here - before iOS 8.3, we could have access to app sandboxes, read/write too.
Access via iTunes backup
iMazing and similar tools have backup browsers which will let users see app sandboxes. Library and Documents folders, not Caches or tmp. If you really want to be safe, you'll need to explicitly exclude files from the backup. Of course, this has substantial drawbacks...
In my app I'm doing background sync of contacts, and when the device is locked the app crashes:
Terminating app due to uncaught exception 'RLMException', reason:
'/var/mobile/Containers/Data/Application/FF77C6DA-970E-4FDD-AB69-C10C20607243/Documents/MyApp/default.realm.lock:
Unable to open a realm at path
'/var/mobile/Containers/Data/Application/FF77C6DA-970E-4FDD-AB69-C10C20607243/Documents/MyApp/default.realm.lock':
open() failed: Operation not permitted.'
I tried:
Creating a new folder:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyApp"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error]; //Create folder
and then create the .realm file in this folder, and set the file protection to None. along with NSFilePosixPermissions
[[NSFileManager defaultManager] setAttributes:#{NSFileProtectionKey: NSFileProtectionNone,
NSFilePosixPermissions : [NSNumber numberWithShort:0777]
}
ofItemAtPath:[[[RLMRealm defaultRealm]configuration]fileURL].URLByDeletingLastPathComponent.path error:nil];
Still the app crashes when device is locked while syncing.
Any ideas on how to fix this ?
Having data protection on intentionally interferes with access in certain conditions (like when the device is locked).
The default level of protection is complete protection, in which files are encrypted and inaccessible when the device is locked. You can programmatically set the level of protection for files created by your app, as described in Protecting Data Using On-Disk Encryption in App Programming Guide for iOS. For files stored in shared containers (described in Configuring App Groups), set the level of protection programmatically.
(from https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html)
This link also shows where Data Protection is enabled in your Xcode project (specifically, the entitlements for a target), in the event that you're not sure if it's been enabled or not.
If your app needs the benefits of Data Protection but at a more granular level than all-or-nothing, you'll need to dig into the "Protecting Data Using On-Disk Encryption" section referenced in the copy from Apple's docs.
When I run my application on the simulator I found the path of file using following code:
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSLog(#"%#",basePath);
}
simulator output:
file:///Users/username/Library/Application Support/iPhone Simulator/7.0.3/Applications/E7816FF6-7E6A-4606-917E-E74CDB574EC8/Documents/images/20140306113538.png
And same code when I run in my device I found output with:
Device output:
file:///var/mobile/Applications/2D86A03F-C239-4B57-849F-019BCDCA8543/Documents/images/20140306114536.png
And when I checked, file was store in documents.
When I load this url in browser with path of simulator(which I get from simulator output) is loaded in browser.
But path of device(which I get from device output) it showing error in browser with file not found.
like: Firefox can't find the file at /var/mobile/Applications/2D86A03F-C239-4B57-849F-019BCDCA8543/Documents/image/20140306114536.png.
Only just because of this reason I am not able to send file from my device on my server using web service.
Xcode Version: 5.0.2
Device: iPhone 4(iOS 7.0.6)
Any One please help.
Because you are trying to load file from your device which is not available on your system.
And your simulator directory is exist on your system thats why its loading file from your system.
So its better you can browse files from your device and copy into your system using following software
iExplorer
Why do your folder names different ?
for simulator it is "images" and for device it is "image".
Also closely check case sensitivity, since I also had faced similar problem for asset names. MAC file system searches with case insensitivity so simulator may be finding this file. Whereas iOS device file system is case-sensitive.
I'm quite new to iOS development but this has got me absolutely baffled...
My app is doing a simple check if a file exists, like so:
if ([[NSFileManager defaultManager] fileExistsAtPath:[self.countDatabase.fileURL path]]) {....
This worked fine last week and it still works perfectly fine on the iOS device itself. But when the iPhone simulator gets to this line of code, it just... stops. No errors, it doesn't freeze, it just stops. I can start other actions in it but it never completes what was started before. I don't get it. The only thing on this macbook that's changed since last week is that I updated OSX to 10.8.2 and update to iTunes 10.7.
Has anyone ever encountered this or have some idea how I can further debug this? If I step through the code, I get to this line and it just stops stepping through things. It's as if the simulator thinks that it's done.
since I used lots of times:
[[NSFileManager defaultManager] fileExistsAtPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"file.pdf"]]]
It seems that [self.countDatabase.fileURL path] is returning wrong value as a path. I would NSLog it to see what is containing.