Using XCode to access app data - ios

I am using the Device tab in Xcode to view the content of my app. Here is what I see:
The app has iTunes file sharing enabled:
However I am unable to see the App in iTunes (under my Apps) even after synchronising the phone.
I am wondering if there is an alternative way to access the "test-data.csv" file that my app is generating.
XCode does seem only to list the file but does not allow me to click on the file to open it.
EDIT:
This is the file path I use:
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"test-data.csv"];

As you are using iTunes sharing. You will be able to see the data in iTunes only. Make sure that you are selecting your device first in the iTunes and then going to Application tab. If you are not seeing the apps there, this means your app might be showing in the sharing section. Scroll down a bit and you will see the File Sharing section-> Apps. See if you are able to find your application there.
Hope you might get your app there.
See the screeshot:-

Are you sure you are saving your data in the public folder of your app and not in the private one?
Public folder:
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
Private folder:
func getLibraryDirectory() -> NSString {
var paths = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)
let libraryDirectory = paths[0]
return libraryDirectory
}

You can send this file by attaching with email and then download from your email on other desktop etc.
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"CSV File"];
[mailer addAttachmentData:[NSData dataWithContentsOfFile:#"PathToFile.csv"]
mimeType:#"text/csv"
fileName:#"FileName.csv"];
[self presentModalViewController:mailer animated:YES];

Related

locating a text file Objective C

I am trying to get path of legal.txt in my project. However, whatever I do. I am getting a null path back. it is in the Policies directory. How do I get it's path as a string. I need to send that path to a webview controller. Can someone help.
-(void)linkPressedOnTextView:(PPLinksTextView *)tv url:(NSURL *)url{
if ([url.host isEqualToString:#"sign_up"]) {
GetStartedController *getStartedVC = [[GetStartedController alloc] init];
[self.navigationController pushViewController:getStartedVC animated:YES];
}
else if([url.host isEqualToString:#"consentMedicalSevices"]){
PPNavigationViewController *vc = [[PPNavigationViewController alloc]init];
[vc openPolicies:#"Policies/privacy.txt"];
Will this work. I need a way to direct user to a webview once he clicks on the link and privacy text opens up.
From apple doc:
pathForResource:ofType:inDirectory:
Returns the full pathname for the resource file identified by the specified name and extension and residing in a given bundle directory.
NSString *filePath = [NSBundle pathForResource:#"file" ofType:#"txt" inDirectory:[[NSBundle mainBundle] bundlePath]];
NSLog(#"File Path: %#", filePath);
Output:
File Path: /Users/Ashok/Library/Developer/CoreSimulator/Devices/F629F99F-C745-46EB-8A11-01BC9FF1E592/data/Containers/Bundle/Application/EB4EA25F-65E6-4CA8-B8C2-C7C0C64C6C0F/Sample.app/file.txt

how to send file from from one app to another app using url scheme?

Summary (iOS 8, Xcode 6.4)
First question:- Can i share my app's Documents Directory's data with my other app?
If Yes, I've seen many questions related to this;
Move data/images between two iOS apps using custom URL handler,
http://code.tutsplus.com/tutorials/ios-sdk-working-with-url-schemes--mobile-6629
But I found that these example only send text or URLs. Then I tried myself as below:
NSString* path = [NSString stringWithFormat:#"MY_URL_SCHEME://"];
NSURL* url = [NSURL URLWithString:path];
if([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
The above code works well to open my other app. But when I try like below, I can't open my other app.
NSArray* mainPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sourcePath = [mainPath objectAtIndex:0];
NSString* path = [NSString stringWithFormat:#"MY_URL_SCHEME://%#",sourcePath];
NSURL* url = [NSURL fileURLWithPath:path isDirectory:YES];
if([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
So, please help me, what am I missing?
EDIT:-
i forgot to mention that,iOS7 support is important in my App.so, i think extension might not work.
Use NSUserDefaults with app group to share the data between apps
NSUserDefaults *defaults=[[NSUserDefaults
alloc]initWithSuiteName:#"app group name"];
[defaults setObject:filedata forKey:#"keyfordata"];
[defaults synchronize];
in the app delegate of consuming app fetch the data from NSUserDefaults
another way is to use share extension-
http://easynativeextensions.com/how-to-launch-your-app-from-the-ios-8-share-menu/
You can refer MGInstagram files as Instagram mobile app works same. You can pass image from your application to Instagram application.
You can download it from here: https://github.com/mglagola/MGInstagram
Hope this helps.

Edit Info.plist programmatically in a jailbroken app

How can I edit the Info.plist file in a jailbroken app I'm writing? I know it's not normally possible but given the fact that this will be released in Cydia, I feel like there must be a way. I'm not savvy on file modifications in a jailbroken environment so any info is appreciated.
The reason I want to edit the Info.plist file is to register for a URL scheme programmatically. So if there's an alternative way to accomplish that, I'd love to hear it :-)
If you want to programmatically edit your own app's Info.plist file as it runs, you can use this code:
- (BOOL) registerForScheme: (NSString*) scheme {
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"Info"
ofType:#"plist"];
NSMutableDictionary* plist = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath];
NSDictionary* urlType = [NSDictionary dictionaryWithObjectsAndKeys:
#"com.mycompany.myscheme", #"CFBundleURLName",
[NSArray arrayWithObject: scheme], #"CFBundleURLSchemes",
nil];
[plist setObject: [NSArray arrayWithObject: urlType] forKey: #"CFBundleURLTypes"];
return [plist writeToFile: plistPath atomically: YES];
}
and if you call it like this:
BOOL succeeded = [self registerForScheme: #"stack"];
then your app can be open with URLs like this:
stack://overflow
However, if you look at the Info.plist file permissions:
-rw-r--r-- 1 root wheel 1167 Oct 26 02:17 Info.plist
You see that you cannot write to that file as user mobile, which is how your app will run normally. So, one way to get around this is to give your app root privileges. See here for how to do that.
After you use this code, and give your app root privileges, it still might be necessary to respring before you see your custom URL scheme recognized. I didn't have time to test that part.
Here is how I solved it for Facebook SDK in Swift
var appid: NSMutableDictionary = ["FacebookAppID": "123456789"]
var plistPath = NSBundle.mainBundle().pathForResource("Info", ofType: "plist")
appid.writeToFile(plistPath!, atomically: true)
var appName: NSMutableDictionary = ["FacebookDisplayName": "AppName-Test"]
appName.writeToFile(plistPath!, atomically: true)
var urlStuff = NSMutableDictionary(contentsOfFile: plistPath!)
var urlType = NSDictionary(objectsAndKeys: "com.appprefix.AppName", "CFBundleURLName", NSArray(object: "fb123456789"), "CFBundleURLSchemes")
urlStuff?.setObject(NSArray(object: urlType), forKey: "CFBundleURLTypes")
urlStuff?.writeToFile(plistPath!, atomically: true)

Loading Local PDF File Into WebView

I am attempting to put the following functionality into an iOS app I am writing:
Ship a set of PDFs in the resources folder of the project in XCode
Copy the PDFs to the app directory
Open the PDF in a webview.
As far as I can see, the first two steps work ok (I've used FileManager to check fileExistsAtPath after the copy operation).
However, the webview is empty, and is erroring out ("the requested URL does not exist on server").
My code for the file open is as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *localDocumentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = #"example.pdf";
NSString *localDocumentsDirectoryPdfFilePath = [localDocumentsDirectory
stringByAppendingPathComponent:pdfFileName];
pdfUrl = [NSURL fileURLWithPath:localDocumentsDirectoryPdfFilePath];
[webView loadRequest:[NSURLRequestWithURL:pdfUrl];
This works fine on the simulator, but doesn't work on the device
Are you sure you don't want to let the UIDocumentInteractionController do the heavy lifting for you?
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
As posted by Anna Karenina above, "The device is case-sensitive. Make sure the filename matches exactly"
As bshirley suggested UIDocumentInteractionController is a great option to present your PDF. Initially I tried using the 3rd party JSQWebViewController but I was getting a blank screen on device while on simulator it was working. UIDocumentInteractionController worked great for me! For Swift you can do:
let interactionController = UIDocumentInteractionController(url: fileURL)
interactionController.delegate = self
interactionController.presentPreview(animated: true)
and implement the delegate method:
// Mark: UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return UIApplication.shared.keyWindow!.rootViewController!
}

How to check if a file exists in Documents folder?

I have an application with In-App Purchase, that when the user buy something, download one html file into the Documents folder of my app.
Now I must check if this HTML file exists, so if true, load this HTML file, else load my default html page.
How I can do that? With NSFileManager I can't get outside of mainBundle..
Swift 3:
let documentsURL = try! FileManager().url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)
... gives you a file URL of the documents directory. The following checks if there's a file named foo.html:
let fooURL = documentsURL.appendingPathComponent("foo.html")
let fileExists = FileManager().fileExists(atPath: fooURL.path)
Objective-C:
NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString* foofile = [documentsPath stringByAppendingPathComponent:#"foo.html"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
Apple recommends against relying on the fileExistAtPath: method. It's often better to just try to open a file and deal with the error if the file does not exist.
NSFileManager Class Reference
Note: Attempting to predicate behavior based on the current state of the file system or a particular file on the file system is not recommended. Doing so can cause odd behavior or race conditions. It's far better to attempt an operation (such as loading a file or creating a directory), check for errors, and handle those errors gracefully than it is to try to figure out ahead of time whether the operation will succeed. For more information on file system race conditions, see “Race Conditions and Secure File Operations” in Secure Coding Guide.
Source: Apple Developer API Reference
From the secure coding guide.
To prevent this, programs often check to make sure a temporary file with a specific name does not already exist in the target directory. If such a file exists, the application deletes it or chooses a new name for the temporary file to avoid conflict. If the file does not exist, the application opens the file for writing, because the system routine that opens a file for writing automatically creates a new file if none exists.
An attacker, by continuously running a program that creates a new temporary file with the appropriate name, can (with a little persistence and some luck) create the file in the gap between when the application checked to make sure the temporary file didn’t exist and when it opens it for writing. The application then opens the attacker’s file and writes to it (remember, the system routine opens an existing file if there is one, and creates a new file only if there is no existing file).
The attacker’s file might have different access permissions than the application’s temporary file, so the attacker can then read the contents. Alternatively, the attacker might have the file already open. The attacker could replace the file with a hard link or symbolic link to some other file (either one owned by the attacker or an existing system file). For example, the attacker could replace the file with a symbolic link to the system password file, so that after the attack, the system passwords have been corrupted to the point that no one, including the system administrator, can log in.
If you set up your file system differently or looking for a different way of setting up a file system and then checking if a file exists in the documents folder heres an another example. also show dynamic checking
for (int i = 0; i < numberHere; ++i){
NSFileManager* fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString* imageName = [NSString stringWithFormat:#"image-%#.png", i];
NSString* currentFile = [documentsDirectory stringByAppendingPathComponent:imageName];
BOOL fileExists = [fileMgr fileExistsAtPath:currentFile];
if (fileExists == NO){
cout << "DOESNT Exist!" << endl;
} else {
cout << "DOES Exist!" << endl;
}
}
Swift 2.0
This is how to check if the file exists using Swift
func isFileExistsInDirectory() -> Bool {
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory: AnyObject = paths[0]
let dataPath = documentsDirectory.stringByAppendingPathComponent("/YourFileName")
return NSFileManager.defaultManager().fileExistsAtPath(dataPath)
}
check if file exist in side the document/catchimage path :
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *tempName = [NSString stringWithFormat:#"%#/catchimage/%#.png",stringPath,#"file name"];
NSLog(#"%#",temName);
if([[NSFileManager defaultManager] fileExistsAtPath:temName]){
// ur code here
} else {
// ur code here**
}
NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *imagePath = [directoryPath objectAtIndex:0];
//If you have superate folder
imagePath= [imagePath stringByAppendingPathComponent:#"ImagesFolder"];//Get docs dir path with folder name
_imageName = [_imageName stringByAppendingString:#".jpg"];//Assign image name
imagePath= [imagePath stringByAppendingPathComponent:_imageName];
NSLog(#"%#", imagePath);
//Method 1:
BOOL file = [[NSFileManager defaultManager] fileExistsAtPath: imagePath];
if (file == NO){
NSLog("File not exist");
} else {
NSLog("File exist");
}
//Method 2:
NSData *data = [NSData dataWithContentsOfFile:imagePath];
UIImage *image = [UIImage imageWithData:data];
if (!(image == nil)) {//Check image exist or not
cell.photoImageView.image = image;//Display image
}
NSURL.h provided - (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error to do so
NSURL *fileURL = [NSURL fileURLWithPath:NSHomeDirectory()];
NSError * __autoreleasing error = nil;
if ([fileURL checkResourceIsReachableAndReturnError:&error]) {
NSLog(#"%# exists", fileURL);
} else {
NSLog(#"%# existence checking error: %#", fileURL, error);
}
Or using Swift
if let url = URL(fileURLWithPath: NSHomeDirectory()) {
do {
let result = try url.checkResourceIsReachable()
} catch {
print(error)
}
}

Resources