LilMoke
LilMoke

Reputation: 3454

My AppDelegate property is null

This may be a foolish question,but I have a variable defined as follows if my AppDelegate:

@property (nonatomic, strong) NSString * m_sDevToken;

And I also have a method:

- (NSString *)getDeviceToken;

In my .m file I do the following:

@synthesize m_sDevToken;

I assign a value to m_sDevToken like this:

m_sDevToken = [[[[deviceToken description]
                 stringByReplacingOccurrencesOfString:@"<"withString:@""]
                stringByReplacingOccurrencesOfString:@">" withString:@""]
               stringByReplacingOccurrencesOfString: @" " withString: @""];

And I have the method that return it like this:

- (NSString *)getDeviceToken
{
    return m_sDevToken;
}

I try to access it in my initial View Controller from viewDidLoad as follows:

BarMateAppDelegate * appDelegate = (BarMateAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@", [appDelegate getDeviceToken]);

Now, if I log it right after I assign it, the value it correct, but in the viewDidLoad method it returns (null).

I am somewhat new to Objective-C, but similar assignments seem to work correctly, so what am I doing wrong?

Can anyone please explain to me why it would be null?

Upvotes: 0

Views: 748

Answers (3)

LilMoke
LilMoke

Reputation: 3454

I solved this problem myself based on the input from maddy. I needed to set the value elsewhere due to the delay on didRegisterForRemoteNotificationsWithDeviceToken. Once didRegisterForRemoteNotificationsWithDeviceToken gets called I set the devToken in a settings object I have for the app. The settings object stays around and is available later in the app.

Upvotes: 0

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

I have a couple guesses of what may be wrong, but there is not enough source code to be certain.

Either way, replacing all occurrences of m_sDevToken with self.m_sDevToken should help.

self.m_sDevToken = [[[[deviceToken description]
                      stringByReplacingOccurrencesOfString:@"<"withString:@""]
                     stringByReplacingOccurrencesOfString:@">" withString:@""]
                    stringByReplacingOccurrencesOfString: @" " withString: @""];

And

- (NSString *)getDeviceToken
{
    return self.m_sDevToken;
}

Either this will just start magically working, or you will get an error. If you get an error post that error.

Upvotes: 0

Zalykr
Zalykr

Reputation: 1524

define it like:

@property (strong) NSString * m_sDevToken;

otherwise it gets dealloc'd.

Upvotes: 1

Related Questions