PSPDFKit iOS 8 PSPDFGlobalLock documentProviderForDocument:page: - ios

I've a magazine app that downloads PDF in /Library/Caches/... It works pretty well with iOS 7, but from iOS 8 ( that changes file system directories ) I can't open downloaded PDF! from PSPDFKit log error I see:
File downloaded path:
/Users/steven/Library/Developer/CoreSimulator/Devices/xxxxx/data/Containers/Data/Application/xxxxx/Library/Caches/doc1.pdf
PSPDFkit set document from path error:
Error: -[PSPDFGlobalLock documentProviderForDocument:page:]/248 Cannot get document reference, there is no file at /Users/steven/Library/Developer/CoreSimulator/Devices/xxxxx/data/Containers/Bundle/Application/xxxxx/[NAMEAPP]/Users/steven/Library/Developer/CoreSimulator/Devices/xxxxx/data/Containers/Data/Application/xxxx/Library/Caches/doc1.pdf!
Attention: I've replaced some words with xxxx
I'm using an old version of PSPDFKit( i don't known exactly version, I can't find version params )
i get "Caches" folder in this way ( called [Utility documents] in my code ):
NSArray *paths = [[NSArray alloc] initWithArray:NSSearchPathForDirectoriesInDomains( NSCachesDirectory, NSUserDomainMask, YES )];
return [paths objectAtIndex:0];
Download path PDF code:
NSString *destPath = [[Utility documents] stringByAppendingPathComponent:[[highlightedMagazine objectForKey:#"url"] lastPathComponent]];
Read PDF code for iOS 8:
NSString *destPath = [[Utility documents] stringByAppendingPathComponent:[[highlightedMagazine objectForKey:#"url"] lastPathComponent]];
pdfController.document = (PSPDFDocument *)[[Utility documents] stringByAppendingPathComponent:[[highlightedMagazine objectForKey:#"url"] lastPathComponent]];
I see also that download path is "/data/Containers/Data" and read path is "/data/Containers/Bundle", I can't change it.
Can someone help me?? Thank you!

Related

iOS10 filesystem - are files no longer in app container?

For debugging purposes I've often written data to files on iOS using code such as this...
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docsPath stringByAppendingPathComponent:testName];
FILE* resultsFile = fopen([filePath UTF8String],"w");
...and then gotten the data by downloading the container via Xcode (by selecting the app on the "Window->Devices" screen, and choosing "Download container..." from the "little gear" pop-up menu just below the list of apps.)
I recall this working on iOS 9 and previous, but trying this on iOS 10 on an iPhone 6, I'm finding it doesn't work anymore. The call to fopen is returning success for /var/mobile/Containers/Data/Application/[uuid]/Documents/testname but the file isn't in the container when I download it.
Shouldn't the file be in the container? Is it elsewhere? Or is it simply not possible to dump data to a file and pull it off the phone anymore?
I have tried to reproduce your problem (under iOS 10.3.3, Xcode 10.1) and it all worked on my end in the context of an App project. The issue you have may be related to what you do with the file object resultFile, if you could share some code containing the next lines of your code (or check that you are calling fclose() for example), it may be easier to solve it.
Also please note that it is seems NOT supported to write to the Docs directory from the code controlling an App Extension as detailed here: reading and writing to an iOS application document folder from extension
Code that worked in the context of an App project / target:
using Data in Swift like this:
guard let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else { return }
let someData = "Hello world using swift".data(using: .utf8)
do{
try someData?.write(to: documentsPath.appendingPathComponent("hello-world.txt"))
}catch{
//handle the write error
}
using NSData with Objective C:
NSString * hello = #"Hello world using NSData";
NSData * helloData = [hello dataUsingEncoding: NSUTF8StringEncoding];
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docsPath stringByAppendingPathComponent:#"fileNameObjcNSData.txt"];
[helloData writeToFile:filePath atomically:true];
using fopen:
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docsPath stringByAppendingPathComponent:#"fileNameObjcFopen.txt"]; //
FILE * fileHandle = fopen([filePath UTF8String], "w");
if (fileHandle != NULL){
fputs("Hello using fopen()", fileHandle);
fclose(fileHandle);
}
Hope it helps

NMSFTP File Download Works on Simulator, not on iPhone

I am trying to use the SFTP library in NMSSH to download files from an ftp server and have been having trouble on my actual device. It works fine in the simulator. When I check for the file's existence in the stored directory (a created folder in my app's cache directory), the check passes on the simulator but fails on the phone. Below is the code I'm using. As a note, MainPath is the directory path and the fileListLevel1 row item is the file name with the extension.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = paths[0];
cacheDirectory = [#[cacheDirectory, #"/NewDirectory", #"/"] componentsJoinedByString:#""];
[[NSFileManager defaultManager] createDirectoryAtPath:cacheDirectory withIntermediateDirectories:false attributes:nil error: nil];
NSString *filePathToOpen = [#[MainPath, [fileListLevel1 objectAtIndex:indexPath.row]] componentsJoinedByString:#""];
bool isFile = NO;
bool isDir = NO;
isFile = [session.sftp fileExistsAtPath:filePathToOpen];
isDir = [session.sftp directoryExistsAtPath:filePathToOpen];
if (isFile){
NSString *fileToOpenNameAndPath = [#[cacheDirectory, [fileListLevel1 objectAtIndex:indexPath.row]] componentsJoinedByString:#""];
[session.sftp writeFileAtPath:filePathToOpen toFileAtPath:fileToOpenNameAndPath];
//FILE NOT FOUND ON THE PHONE - FOUND IN SIMULATOR!!!!!!!!
if ([[NSFileManager defaultManager] fileExistsAtPath:fileToOpenNameAndPath]){
NSLog(#"Found you!");
}
else if(![[NSFileManager defaultManager] fileExistsAtPath:fileToOpenNameAndPath]){
NSLog(#"Why you no there?");
}
I also tried creating the paths using the stringByAppendingPathComponent method and got the same result.
I'm new to app writing and objective c so I may be missing something fundamental here but I've been unable to find anything on this.
One additional question revolves around using the writeFileAtPath:toFileAtPath:progress command. I wonder if my issue is with the simulator writing much faster so my check happens before it is done on the phone. I'm not sure how to properly use the :progress portion as I'm unsure what type of variable the (BOOL (^)(NSUInteger sent)) is referring to or how to create one to check the progress.
Any help would be very much appreciated.

Xcode 6 keeps renaming my app's directory in iOS8 simulator after each run.

I'm running Xcode 6 Beta 5 but this has been happening since the first beta. My app's directory in the simulator keeps being renamed after each run. It took me a while to figure this out. I'm using this to get the doc's dir reference.
NSString *folder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSLog(#"Documents Dir: %#",folder);
Now for example on the first run it'll be:
/Users/Joey/Library/Developer/CoreSimulator/Devices/5B9930EE-A9B4-4B36-BABB-AA864ACAF2DE/data/Containers/Data/Application/4B10C2E4-A5C3-4C64-93B1-4069FCCB9C46/Documents
Second run now it's:
/Users/Joey/Library/Developer/CoreSimulator/Devices/5B9930EE-A9B4-4B36-BABB-AA864ACAF2DE/data/Containers/Data/Application/7E9EB62D-115A-4092-AD23-CB6BA3E5E10F/Documents
Third run:
/Users/Joey/Library/Developer/CoreSimulator/Devices/5B9930EE-A9B4-4B36-BABB-AA864ACAF2DE/data/Containers/Data/Application/EC8F41E8-52ED-4B10-9808-B3ACC46FC6AA/Documents
This is wreaking havoc with my app because it stores path references for certain files within the app. It's not that my NSLog statement is returning incorrect results, I verified this is what happening in Finder. It's changing the name every time. Has anyone seen this happen? Is this a "feature" that I'm misunderstanding?
Turns out Xcode 6 does in fact change the app's UUID every run, and I'm in the wrong for storing absolute paths.
USE SIMPHOLDERS
I used this app on Xcode 5 opens the Documents folder, for the currently running app in the simulator, in Finder.
http://simpholders.com/
not ready for Xcode 6 yet (as of sep 24 2014) but saves all this hassle.
In Xcode 6 / iOS8 The bundle is now separate from the data./ The application GUID is regenerated between runs in Xcode (not sure why)
DOCUMENTS DIR:/Users/gbxc/Library/Developer/CoreSimulator/Devices/AC79941F-EC56-495E-A077-773EEE882732/data/Containers/Data/Application/C220D351-0BE7-46BA-B35E-D16646C61A3F/Documents
mainBundlePath_:/Users/gbxc/Library/Developer/CoreSimulator/Devices/AC79941F-EC56-495E-A077-773EEE882732/data/Containers/Bundle/Application/12200D1D-9B67-408B-BCF7-38206CBE0940/myappname.app/BLANK_BLOG_SCALED.jpg
1. FIND THE DEVICES FOLDER in SIMULATOR
/Users/gbxc/Library/Developer/CoreSimulator/Devices/
open each /device.plist to see which GUID is which device in XCode - I think this is static
3. FIND THE DEVICE you're running on iPad 2 - I think this is static
/Devices/AC79941F-EC56-495E-A077-773EEE882732
4. Find your application /Documents folder
/AC79941F-EC56-495E-A077-773EEE882732/data/Containers/Data/Application/C220D351-0BE7-46BA-B35E-D16646C61A3F/Documents
BEWARE the GUID C220D351-0BE7-46BA-B35E-D16646C61A3F is regenerated everytime the app is run in XCode 6
NSArray *paths_ = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if(paths_){
_docsDir = [paths_ firstObject];
DebugLog(#"DOCUMENTS DIR:%#",_docsDir);
}else{
ErrorLog(#"paths_ is nil - cant get Documents directory");
}
MAIN BUNDLE path
NSString *mainBundlePath_ = [[NSBundle mainBundle] pathForResource:#"someimageinyourbundle" ofType:#"jpg"];
/AC79941F-EC56-495E-A077-773EEE882732/data/Containers/Bundle/Application/12200D1D-9B67-408B-BCF7-38206CBE0940/clarksonsiq.app/BLANK_BLOG_SCALED.jpg
NEVER CACHE THE PATH to /Documents between runs it will change.
I was serializing it to a plist and couldnt figure out why they kept disappearing
The GUID above /Documents keeps changing between runs but if you have /Documents open in Finder the folder stays open.
https://devforums.apple.com/thread/235911?tstart=0
https://devforums.apple.com/thread/238754?tstart=0
Free Solution
Use Open Source Library OpenSim. OpenSim is an open source alternative for SimPholders, written in Swift.
Paid Solution
Use SimPholder application to know current application location.
For xcode 6.0 >
Download SimPholder 2.0 alpha 2
For xcode 5.1 <
Download SimPholders 1.5
I can confirm that this is Xcode 6 related not iOS 8.
I have two development machines. On one of them I have Xcode 5. I was working all the time on that machine and my URL's were fine (photo app, photos are visible).
Yesterday I checked in form git my source on a machine with Xcode 6. I noticed that my photos are not visible any more, only photos that are created during that app session.
After little debugging, I realized that file:///var/mobile/Applications/B6A6BAEF-C90C-4A2A-93DB-E6700B88971F/Documents/ is changing on every app run.
All that time I am working with iOS 7 device.
I am going to check once more on a machine with Xcode 5 to confirm when I get my hands on it.
You need to to save only path inside DocumentDirectory(directory/file name), and add it to the DocumentDirectory every time you load the file...
-(void)saveImage:(UIImage *)image{
NSData *pngData = UIImagePNGRepresentation(image);
NSString *pathInDocumentDirectory = [APP_DocumentDirectory stringByAppendingPathComponent:PROFILE_IMAGE_NAME];
NSString *filePath = [self documentsPathForFileName:pathInDocumentDirectory];
//Save pic file path - DirName/Filename.png
[XYZPreferencesHelper setUserImageFilePath:pathInDocumentDirectory];
//Write the file
[[NSFileManager defaultManager] createFileAtPath:filePath contents:pngData attributes:nil];
}
-(void)loadSavedUserPicture{
//Load saved DirName/Filename.png
NSString *pathInDocumentDirectory = [XYZPreferencesHelper getUserImageFilePath];
if (pathInDocumentDirectory != nil){
//Full path with new app Document Directory
NSString *filePath = [self documentsPathForFileName:pathInDocumentDirectory];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:pngData];
if (image != nil){
userPicImageView.image = image;
}
}
}
}
- (NSString *)documentsPathForFileName:(NSString *)name
{
NSString *documentsPath = [self createRestcallDirectoryIfNotExist];
return [documentsPath stringByAppendingPathComponent:name];
}
-(NSString *)createRestcallDirectoryIfNotExist{
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
path = [documentsPath stringByAppendingPathComponent:APP_DocumentDirectory];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) //Does directory already exist?
{
if (![[NSFileManager defaultManager] createDirectoryAtPath:path
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(#"Create directory error: %#", error);
}
}
return documentsPath;
}
Because app's UUID changes and not reliable 
so we should not store urls instead we should store just file names and reconstruct url at runtime , on update/reinstall iOS creates new home directory, stores app bundle in it and copies documents files so url changes


Loading image from folder using NSBundle and plist

Trying to Display the load the image from the folder present on MAC desktop using path like (/Users/sai/Desktop/images/aaa.jpg) Which is created in plist file called Data.plist at item0.
As im using NSBundle it is diaplying the image path but not loading the image from the desktop .I have done a lots of research still couldn't find the solution .Plz help me .Here is the code
NSString *path=[[NSBundle mainBundle]pathForResource:#"Data" ofType:#"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:path];
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
NSArray *array=[NSArray arrayWithArray:[temp objectForKey:#"images"]];
NSString *object=[array objectAtIndex:0];
NSLog(#"object at index i %#",[object lastPathComponent]);
NSString *image=[object lastPathComponent];
mImageView.image=[UIImage imageNamed:image];
[self.view addSubview:mImageView];
Here is the screen shot of Data.plist
Simple and Short answer : It is not Possible to get image from MAC desktop, So please it better to stop fighting with it :)
It is different for mobile (iPhone application) and desktop (mac application), but why you want to do this ?? you know that your iOS application install in Apple iPhone ???? if your application is related to iOS ? then how you connect it to MAC OS ??
Best and simple way is put this image in Application Bundle or your Application's document directory and get it.

Data txt files work when read in iPhone simulator, but do not work on iPhone device?

I have two text files (.txt) that are in the Documents directory of the Bundle for my project. The files load and work fine when I run the project on the iPhone Simulator in xCode, however the when I run the same project in xCode using my iPhone nothing gets loaded. I have checked and the files don't even exist in the file directory when the iPhone is selected to run the project.
Here is the code. Again this works fine and the file exists running under iPhone simulator in xCode, but the file does NOT exist when I run it using my iPhone as the device.
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *path = [docDir stringByAppendingPathComponent:CURRENTPUZZLENUMBERFILE];
NSLog(#"Path = %#", path); // for debugging
bool fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
NSLog(#"File exists: %d", fileExists); // for debugging
Grab the file this way then you can copy it to your doc dir if you wish
NSString *filePath = [[NSBundle mainBundle] pathForResource:CURRENTPUZZLENUMBERFILE ofType:#"txt"];

Resources