exolaris
exolaris

Reputation: 27

Objective-c: AppDelegate NSString

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!

Upvotes: 1

Views: 929

Answers (4)

Saroj Kumar ojha
Saroj Kumar ojha

Reputation: 485

distributedLCAAppDelegate* delegateobj = [(distributedLCAAppDelegate*)[UIApplication sharedApplication] delegate];
NSString *token_ = delegateobj.token;
NSLog(@"token_ :%@",token_);

Try This , it should work

Upvotes: 0

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Try this : (UPDATED) (fixed)

NSString* token2 = ((distributedLCAAppDelegate*)[[UIApplication sharedApplication] delegate]).token;

Upvotes: 2

blauzahn
blauzahn

Reputation: 351

Can you check the Files' Owner in "MainWindow.xib" if its delegate outlet is set to your distributedLCAAppDelegate? Just Ctrl-click on the yellow box-icon in Interface Builder.

Upvotes: 0

rishi
rishi

Reputation: 11839

Try following -

- (IBAction)switchWasActivated:(id)sender {
    distributedLCAAppDelegate *delegate = (distributedLCAAppDelegate *) [[UIApplication sharedApplication] delegate];
    NSString *token2 = delegate.token;

}

Upvotes: 2

Related Questions