App Crashing When Relaunching - iphone

When I hard exit (double click home and minus) and then relaunch app it is freezing up.
The Console shows this:
[app directory path .....]has changed; re-reading symbols.
I think it may have to do with this code in my appdelagate.m
- (void)applicationWillTerminate:(UIApplication *)application
{
[self saveCode];
}
- (void)saveCode
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"routine.plist"];
[NSArray writeToFile:path atomically:YES];
}

First: saving a plist to populate a UITableViewController is a really tedious way to save data persistently and an inconsistent one as you may have realized, I would recommend that you use Core Data instead.
Second: Run the debugger (before launching your app) and find any line of text that's shown in black (grey lines are for methods called by the os and the cocoa layer, black ones are for your own methods). The debugger can be run via Run > Debugger or Command + Shift + Y.

The similar kind of crashing issue - on quick relaunch is being faced when I use location manager in the application. The case is : my application quits on pressing home button rather than going into the background in iPhone 4. And on iPhone 3G (where no background processing is supported) the case is same - it crashes on relaunch [atleast it seems like a crash].
If we wait even 1 second to relaunch the app, it does not crash.

Related

Open pdf with other apps

I am displaying a pdf file in an app. I want to show "open with" option on nag bar showing apps installed on iPhone that can open same pdf and if user selects any of the app (for e.g. pdf viewer) then the pdf should get open with pdf viewer app.
How do I do this?
Please help
Thanks in advance.
To open a file in an available application on the device, use the UIDocumentInteractionController class.
A document interaction controller, along with a delegate object, provides in-app support for managing user interactions with files in the local system. For example, an email program might use this class to allow the user to preview attachments and open them in other apps. Use this class to present an appropriate user interface for previewing, opening, copying, or printing a specified file.
There are a lot of questions around it on SO if you get stuck. search results for UIDocumentInteractionController
This code will present the OpenIn interaction you're looking for. It works for iPhone and iPad. On iPad its in a popover. I'm generating the PDF but if you're just using one you have you don't need to writeToFile, just hand in the filePath.
// In your header. MyViewController.h
#interface MyViewController : UIViewController <UIDocumentInteractionControllerDelegate>
{
UIDocumentInteractionController *docController;
}
// In your implementation. MyViewController.m Open Results is hooked up to a button.
- (void)openResults
{
// Generate PDF Data.
NSData *pdfData = [self makePDF];
// Create a filePath for the pdf.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Report.pdf"];
// Save the PDF. UIDocumentInteractionController has to use a physical PDF, not just the data.
[pdfData writeToFile:filePath atomically:YES];
// Open the controller.
docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
docController.delegate = self;
docController.UTI = #"com.adobe.pdf";
[docController presentOpenInMenuFromBarButtonItem:shareButton animated:YES];
}
You can check with Apple Default api of "UIDocumentInteractionController".
Below is url:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html
Hope, it will resolve your issue.
Cheers.

EXC_BAD_ACCESS When NSSearchPathForDirectoriesInDomains in iOS5

I'm developing an App that will update the UI every time a View "A" appears. I put the related code in the ViewWillAppear and this update will load some images in the document path of the app. Thus, I will load the related path in the section. And it works fine in the beginning, but if I navigate to another page (using navigationController to push page) and back to this page again several times, I will got an EXC_BAD_ACCESS.
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // EXC_BAD_ACCESS here
NSString* docPath = [paths objectAtIndex:0];
.....
And if I temporarily change the code by hardcoding the path like:
NSString* docPath = [NSString stringWithFormat:#"****/****/***/Documents"];
then it will not crash.
I also tried to load the document path at ViewDidLoad and save the value to a variable where the property is (nonatomic, retain).
And in the ViewWillAppear, I load the path like:
NSString* docPath = self.documentPath;
but it will also got an EXC_BAD_ACCESS at this line.
Before this line, I also tried to print the self.documentPath, and it will get the correct path string.
Check out my this answer to see how to debug EXC_BAD_ACCESS error and then put up the error description here. That would help to answer your question.

how do i remove coredata from iphone

You know how you can Reset the coredata store on an iPhone simulator when you've changed your entity structure?
Do I need to perform a similar process when I've created a new version of my core data store that is different from what I last ran on my iPhone? If so, how, please?
Thanks
Just for convenience, until you code a way to remove the persistent store through your app, you can just delete the app off the phone. (Hold your finger on the home screen until icons get wiggly, then click the x on your app.) Then with your phone connected to your Mac, choose Product > Run in XCode and it will reinstall your app on the phone, but with empty data directories.
For deployment, of course, you need to come up with a way to do it without deleting the app, if you will ever change your data model after deployment (assume you will). Data migration is the best option, but if all else fails delete the persistent store file. It would be preferable to prompt for the user's approval before doing that. If they have important data they can decline and maybe get the old version of your app back to view the data and migrate it by hand, or they can wait until you release version 2.0.1 that fixes your data migration bug.
Here is the routine I use to reset my App content. It erases the store and any other file stored.
- (void) resetContent
{
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];
NSArray *content = [localFileManager contentsOfDirectoryAtURL:rootURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];
for (NSURL *itemURL in content) {
[localFileManager removeItemAtURL:itemURL error:NULL];
}
[localFileManager release];
}
If you only want to erase the store, since you know its file name, you can refrain from enumerating the document directory content:
- (void) resetContent
{
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];
NSURL *storeURL = [rootURL URLByAppendingPathComponent:#"myStore.sqlite"];
[localFileManager removeItemAtURL:storeURL error:NULL];
[localFileManager release];
}
But please note that in many cases, its better to migrate your store when you change your model, rather than to delete it.
locate your app in /Users/username/Library/Application Support/iPhone Simulator/4.3.2 (iOS Version may be different) and delete the .sqlite file
You can look at the path that is being sent to the persistentStoreCoordinator on setup, and remove that file. Usually the approach I have taken is that I set up the store to auto migrate, and if that fails I delete the store and attempt one more time to create the persistentStoreCoordinator which will use the now empty path.
Don't forget you may need to repopulate anything stored in the old database.

programmatically clearing secondary cache on iPhone

I have created an application , where i ll be downloading the images from server and storing it locally on the iPhone's file system. Its happening fine. Now the problem is , i want to clear the locally cached images on iPhone when ever i quit my application.
How do i delete those cached images on iPhone. it's using Secondary memory on iPhone, how do i access it programmatically ?
Thank You in advance.
Suse.
Clean them up in your applicationWillTerminate method.
- (void)applicationWillTerminate:(UIApplication *)application
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *imagesFiles = [fileManager contentsOfDirectoryAtPath:saveDirectory error:&error];
for (NSString *file in imagesFiles) {
error = nil;
[fileManager removeItemAtPath:[saveDirectory stringByAppendingPathComponent:file] error:&error];
/* do error handling here */
}
}
My answer would be as per Kirky Todds, but don't use applicationWillTerminate -- it's not a good strategy on iOS4+ with multitasking, because this method often won't get called -- an app will be background, and then can be dumped from memory if other apps are run (or the phone is turned off/restarted), without applicationWillTerminate getting called. (OR it will be foregrounded again. Either way, your cache doesn't get cleared.)
Instead, consider either clearing at startup (applicationDidFinishLaunching), or in applicationWillEnterForeground. The latter will tend to get called more frequently, because the former is only called on an actual proper app lauch (and not on a resume from being backgrounded).
For more info on backgrounding and the events generated, see:
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/

EXC_BAD_ACCESS on NSString,when application is brought to foreground from background [ on 4.0 OS ]

Currently My application is getting crashed on accessing NSString after bringing application to foreground from background [on 4.0 OS].
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
iDatabasePath = [documentsDirectory stringByAppendingPathComponent:KApp_DB_Name];
iDatabasePath is declared as NSString* and it is globally declared.
When Application is moved to background and brought to foreground when I make a call to
iDatabasePath = [documentsDirectory stringByAppendingPathComponent:KApp_DB_Name];
Application crashes due to EXC_BAD_ACCESS to iDatabasePath and this is happening on 4.0 OS
Please help me on this.
Thanks,
Sagar
you need to retain it...
[iDatabasePath retain] somewhere - probably applicationDidEnterBackground
so which line is the error on? If its on the line you say, it looks like documentsDirectory would be getting released, not iDatabasePath, have you tried to retain iDatabasePath?