I have an NSString that as an instance variable within my appdelegate as below:
distributedLCAAppDelegate.h:
@class distributedLCAViewController;
@interface distributedLCAAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
distributedLCAViewController *viewController;
NSString *token;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet distributedLCAViewController *viewController;
@property (nonatomic, copy) NSString *token;
@end
section from distributedLCAAppDelegate.m:
@implementation distributedLCAAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize token;
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
token = [NSString stringWithFormat:@"%@",deviceToken];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
token = [token substringWithRange:NSMakeRange(1, [token length]-2)];
}
I need to be able to use this token variable within the view controller. Currently this is what I have:
section from within distributedLCAViewController.m:
- (IBAction)switchWasActivated:(id)sender
{
NSString *token2 = [[[distributedLCAAppDelegate alloc] token] autorelease];
}
However, token2 = “invalid cfstringref”.
I initially tried declaring a public method called getToken, which just returned token. But I was getting the same problem in that case as well.
Any help would be appreciated!
Try this : (UPDATED) (fixed)