Converted to ARC App crashes on any method iPhone - iphone

I converted my app to ARC and removed all the pre-build release errors. It launches, but will crash (EXC_BAD_ACCESS) as soon as I call any method (all of which are attached to UIButtons). I also noticed that it will ask if the user will allow for the app to use the user's location, but the alert will disappear before the user can answer yes or no.
I feel like there's some very basic setting I'm missing causing this.
Here's the first method called, it won't let the user actually say if they'll allow location services. The alert fires then disappears. Does this help anyone's diagnosis?
-(void)startLocation
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
Also, here's my didFinishLaunchingWithOptions method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController *nav = [[UINavigationController alloc] init];
StartPageViewController *start = [[StartPageViewController alloc]init];
NSManagedObjectContext *context = [self managedObjectContext];
if (!context)
{
// Handle the error.
}
start.managedObjectContext = context;
nav.viewControllers = [NSArray arrayWithObjects:start, nil];
[_window addSubview:[nav view]];
[self.window makeKeyAndVisible];
return YES;
}

Try retaining your navigation controller by making it a strong property on your delegate.
At the moment, I don't see any code that would cause ARC not to release nav at the end of the method. That would release start, which would release context.

All I needed to change (so far) was:
self.window.rootViewController = nav;
instead of:
[_window addSubview:[nav view]];

Related

Restart application after low memory warning

I'm using my appDelegate's applicationDidReceiveMemoryWarning method to dump some high resource objects. When the app starts back up, rather than trying to reload just those objects and return the user to his last page, I would like just to restart the app from the top (from the main page, the app only goes one level deep, so this is totally acceptable to us).
Here's my paltry attempt, but it was an abject failure. It got close, but I ended up introducing some problems that resulted in an actual memory dump and app crash.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[SplashScreenViewController alloc] initWithNibName:#"SplashScreenViewController" bundle:nil];
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navcon;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//For testing purposes only
self.lowMemoryWarning = TRUE;
NSLog(#"app did enter background");
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"app will enter foreground");
if (self.lowMemoryWarning) {
NSLog(#"recovering from low memory warning");
self.window.rootViewController = nil;
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navcon;
[self.window makeKeyAndVisible];
}
}
What's the best approach for doing something like this? Is there maybe a simple trick that I don't know about?
Thank you!
Do you mean you want the app to restart each time it started (or enter foreground) ? If yes, maybe you can just set the app to not support multi tasking
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW5
Search for "Opting out of Background Execution"
================================================================================
Ah sorry, I didn't know the nature of your application, if some process need to be run on the background then this method is no go.
I read your comment above about the UIActivityIndicator + loading the certain objects back on the current view after app enter foreground. Maybe this thread can help you, Finding the current view when application enter foreground. IOS

iOS 6 - State Preservation and Restoration

I have implemented iOS 6 API for state saving, it works - after I quit the app and launch back in for some milliseconds the restored view controller fly in, but then it's replaced by the main view controller I display at launch.
I'm setting every time the app launch the root view of the main window, so this must be the issue.
Here is my code:
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self commonInitializationLaunching:launchOptions];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self commonInitializationLaunching:launchOptions];
return YES;
}
- (void)commonInitializationLaunching:(NSDictionary *)launchOptions
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
static NSString *const kKeychainItemName = #"OAuthGoogleReader";
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
GTMOAuth2Authentication *auth;
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:kClientID
clientSecret:kClientSecret];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
BOOL isSignedIn = [auth canAuthorize];
if (isSignedIn) {
NSLog(#"Signed");
}else{
NSString *scope = #"https://www.google.com/reader/api/";
GTMOAuth2ViewControllerTouch *viewController;
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
clientID:kClientID
clientSecret:kClientSecret
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:#selector(viewController:finishedWithAuth:error:)];
[self.navController pushViewController:viewController animated:YES];
// self.window.rootViewController = viewController;
}
});
}
You can see that in -(void)commonInitializationLaunching:(NSDictionary *)launchOptions
I'm setting my window's root view. I don't know what to put in there. Perhaps check if there is saved state and then load this method? But how?
Thanks!
Here is what I've tried following Rob's advice:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (!self.isRestored) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
[self commonInitializationLaunching:launchOptions];
[self.window makeKeyAndVisible];
return YES;
}
with nothing in willFinishLaunching...
I also removed by window code from my commonInitializationLaunching method.
Storyboards will do most of the heavy lifting for you, such as restoring the window. Using code, however, will not restore the window. You will need to hold on to your root view controller using the encoder. Your code will look something like this:
NSString * const AppDelegateRootVCKey = #"AppDelegateRootVCKey";
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
[coder encodeObject:self.window.rootViewController forKey:AppDelegateRootVCKey];
}
- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {
// Grabs the preserved root view controller.
UIViewController * vc = [coder decodeObjectForKey:AppDelegateRootVCKey];
if (vc) {
UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = vc;
window.restorationIdentifier = NSStringFromClass([window class]);
// The green color is just to make it obvious if our view didn't load properly.
// It can be removed when you are finished debugging.
window.backgroundColor = [UIColor greenColor];
self.window = window;
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (!self.window) {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// The blue color is just to make it obvious if our view didn't load properly.
// It can be removed when you are finished debugging.
window.backgroundColor = [UIColor blueColor];
UIViewController *root = // However you create your root.
window.rootViewController = root;
window.restorationIdentifier = NSStringFromClass([window class]);
self.window = window;
}
[self commonInitializationLaunching:launchOptions];
[self.window makeKeyAndVisible];
return YES;
}
Another gotcha to watch out for is to make sure that your UINavigationControllers and UITabBarControllers have restoration identifiers.
State restoration is generally integrated with storyboards. If you're using a storyboard, you should not be creating your own window, view controllers, etc. You should let the storyboard do this for you. What's happening is that the storyboard is doing all the state restoration, and then you're creating a new window and laying it on top of all that. If that's the case, you're probably creating two copies of your UI on every launch. You're just not noticing it.
If you are constructing your entire interface in code (not a recommended approach, but it does work), then you need to determine whether state restoration happened before creating your UI. This is fairly simple:
In your commonInitializationLaunching:, initialize only non-UI elements (things that wouldn't ever be in state-preservation). This is the place to handle things that the UI elements might rely on during state restoration. You don't have any of these in your current code.
In application:didDecodeRestorableState:, set an app delegate ivar to indicate that state was restored.
In application:didFinishLaunchingWithOptions:, after running commonInitializationLaunching:, check your ivar. If state wasn't restored, create a UI.
Do remember that the commonInitializationLaunching: pattern only exists for backward compatibility with iOS 5. If you don't need that, then just put non-UI in willFinish and UI in didFinish (if state wasn't restored).

RestKit not mapping attributes properly when using CoreData

I am having some issues implementing RestKit/CoreData with my application. Using the RKTwitterCoreData example application and pointing it to my web service for the json feed, I get the resulting tableview:
The issues encountered so far:
Refresh generates a new core data entry even though i have primaryKeyAttribute set to the orderID.
cell textLabel displays an odd orderID that doesn't match with my web service
Here is my applicationDidFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURLString:#"http://mywebservice.com"];
// Enable automatic network activity indicator management
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
NSString *databaseName = #"RKTwitterData.sqlite";
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName usingSeedDatabaseName:nil managedObjectModel:nil delegate:self];
RKManagedObjectMapping* statusMapping = [RKManagedObjectMapping mappingForClass:[Order class] inManagedObjectStore:objectManager.objectStore];
statusMapping.primaryKeyAttribute = #"orderID";
[statusMapping mapKeyPath:#"id" toAttribute:#"orderID"];
[statusMapping mapAttributes:#"created_at", nil];
// Register our mappings with the provider
[objectManager.mappingProvider setObjectMapping:orderMapping forResourcePathPattern:#"/orders"];
// Create Window and View Controllers
RKTwitterViewController* viewController = [[[RKTwitterViewController alloc] initWithNibName:nil bundle:nil] autorelease];
UINavigationController* controller = [[UINavigationController alloc] initWithRootViewController:viewController];
UIWindow* window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[window addSubview:controller.view];
[window makeKeyAndVisible];
return YES;
}
And the json result at /orders:
Any advice for dealing with this issue? Thanks!
Doh! The reason was because the twitter API isn't key value coding compliant. Once i changed:
[objectManager.mappingProvider setObjectMapping:orderMapping forResourcePathPattern:#"/orders"];
with:
[objectManager.mappingProvider setObjectMapping:orderMapping forKeyPath:#"orders"];
It worked again!
Thanks and hope it helps someone.

Linking Model to Controller in simple application?

I am just looking at ways to access a simple model object (in the MVC sense) from my controller. Right now I am creating the model in the applicationDelegate, and passing it to the controller when I create the controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Setup Model
DataModel *tempDataModel = [[DataModel alloc] init];
[self setDataModel:tempDataModel];
[tempDataModel release];
// Setup Controllers
Controller *rootController = [[Controller alloc] initWithModel:[self dataModel]];
UINavigationController *tempNavController = [[UINavigationController alloc] initWithRootViewController:rootController];
[self setNavController:tempNavController];
[rootController release];
[tempNavController release];
[window addSubview:[[self navController] view]];
[window makeKeyAndVisible];
return YES;
}
inside the controller I have:
#property (nonatomic, retain)DataModel *dataModel;
and:
- (id)initWithModel:(id)newModel {
self = [super init];
if(self) {
NSLog(#"%s", __PRETTY_FUNCTION__);
dataModel = [newModel retain];
}
return self;
}
- (void)dealloc {
NSLog(#"%s", __PRETTY_FUNCTION__);
[dataModel release];
[super dealloc];
}
This works fine, but I am just curious if this is ok in terms of MVC and good design. In previous apps I have:
Used a shared instance (Singleton)
Created the model from inside the controller.
Any comments would me much appreciated:
I think this is perfectly good design. The controller is allowed to manipulate the model, so needs a reference to this. I think your current way of injecting the Model instance is better than a singleton approach.

Wait Until Current Location is set in didFinishLaunchingWithOptions

My Home UIView requires current Location coordinates and I wouldn't want it to be called until I have these coordinates. How best can I achieve this?
in this code, the view is called before the update of current location:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
currentLocation = [[CLLocation alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
TTNavigator *navigator = [TTNavigator navigator];
navigator.persistenceMode = TTNavigatorPersistenceModeAll;
TTURLMap *map = navigator.URLMap;
[map from:#"tt://HomeView" toViewController: viewController];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"tt://HomeView"]];
}
users can have Location service turned off. so I wouldn't do that..
Instead check for the location after the didFinishLaunchingWithOptions, so that your current problem will be solved.
and add one more screen for user to turn on the location service if it's off.