0

I have created the one mutable array in appdelegate. I am trying to retrieve values of that mutable array in another app. But it is crashing at that point. Below is the code I have declared in appdelegate:

savedLocation = [[NSMutableArray alloc] init];
 savedLocation = [[NSMutableArray arrayWithObjects:
       [NSNumber numberWithInteger:0],
       nil] retain];

Below is code in which I am trying to access the array values in another application through appdelegate:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
 NSInteger selection = [[appDelegate.savedLocation objectAtIndex:0] integerValue];

But it is crashing at:

NSInteger selection = [[appDelegate.savedLocation objectAtIndex:0] integerValue];

Please tell me the reason why it is crashing and the correct way of doing this also.

3
  • What's the crash message in the console? Commented Nov 23, 2009 at 7:38
  • Also, you're leaking at savedLocation = [[NSMutableArray alloc] init]; It's redundant as [NSMutableArray arrayWithObjects:] returns a new autoreleased object. Commented Nov 23, 2009 at 7:40
  • Please go and read Apple's memory management documentation as you clearly have no idea how Cocoa does this yet. Commented Nov 23, 2009 at 13:42

2 Answers 2

1

If you really have 2 different Objective-C applications running, keep in mind that each one will have its own [UIApplication sharedApplication]. If I understand your question correctly, you are assuming that you will be able to pass information between applications using this mechanism and that is simply not the case. Each app will have its own sharedApplication and corresponding app delegate, and will not be able to see the members of the other.

Since you are using UIApplication (rather than NSApplication), I am inferring that you are using Cocoa Touch (for iPhone and iPod Touch), and so you are probably interested in the mechanisms that are available for sharing data between applications. There are a number of possible approaches, including a custom URL handler (to allow one app to launch the other app with some particular parameters), or using a network-dependent sync mechanism where you store data from both applications in some shared server location on the Internet. You should keep in mind, however, that:

  1. Only one of your applications will ever be running on the Cocoa Touch device at any one time. So, the in-memory member variables of the app delegate will be released as soon as the app that instantiated them exits (to make way for the other app).

  2. There is no way I know of for one Cocoa Touch application to read data saved by another Cocoa Touch application onto the device. The operating system strictly sandboxes all individual applications running on the device so one cannot read data saved by another.

This question is similar to what I think you're asking.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using Objective-C and Cocoa for a Mac OS X application you can find many system to pass information between two running applications i.e. using Notifications, the File System, Distant Objects, the PasteBoard some useful information can be found here: http://developer.apple.com/mac/library/navigation/index.html#section=Topics&topic=Interapplication%20Communication

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.