0

A "token" will be given upon authentication in a form of xml file with some other elements.

I managed to extract the token, and display it in the calling class, let's say parser.m.

However, I need to make the content of that variable available globally, so that I can reuse the same token. How do I do that?

Please note that a different token will be given upon the next authentication.

.h:

@interface Parser : NSObject <NSXMLParserDelegate>{
NSXMLParser *parser;
NSMutableString *element;
NSMutableString *token;}
@property (nonatomic, retain) NSMutableString *token;
@end

.m:

#import "Parser.h"

NSLog(@"tOKEn called from main: %@", parser->token);

It is not able to access the "token" that was declared under parser.m

How do I go around this?

Update 1: I found out that using @public serves the purpose too. Not sure whether its a good move or not since I will be needing the token in all my API calls.

Confused_person
  • 103
  • 1
  • 2
  • 11

6 Answers6

2

create a singleton class for global variable in objective c

in .h file

    #import <Foundation/Foundation.h>

    @interface Singleton : NSObject 
    {
            ///......
    }

    + (Singleton *)sharedSingleton;

in .m file

    #import "Singleton.h"

    @implementation Singleton
    static Singleton *singletonObj = NULL;

    + (Singleton *)sharedSingleton 
    {
            @synchronized(self) 
            {
                    if (singletonObj == NULL)
                            singletonObj = [[self alloc] init];
            }
            return(singletonObj);
    }

and use this in another file

    #import "Singleton.h"
    //.....

    Singleton *sinObj =  [Singleton sharedSingleton];   

and create variables inside that and also write setter and getter method for that variable in singleton class. Access this method and variable using sinObj object of singleton class

DharaParekh
  • 1,730
  • 1
  • 10
  • 17
2

IMO singletons are a overused curse.
Your (every) application should have a layers structure, and this token variable shouldn't leave communication layer. If it does this means that something is wrong with your application design. Note that token is needed only to do posts and every other part of code doesn't need it.

Marek R
  • 32,568
  • 6
  • 55
  • 140
1

For temporary variables, consider using a singleton instance, such as the MySingleton class illustrated in this answer. How to create a singleton: What should my Objective-C singleton look like?. You can access the variable like this: [MySingleton sharedSingleton].variable; Singleton is a good choice if you have a lot of global variables, but if only one better to use NSUserDefaults.

NSUserDefaults are for things that require persistance, i.e. you are planning on storing them between app starts. Good starter: http://mobile.tutsplus.com/tutorials/iphone/nsuserdefaults_iphone-sdk/

Also please read: Are global variables bad?

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276
0

Try this:

[[NSUserDefaults standardUserDefaults]setObject:_YourObject_ forKey:@"token"];

NSString *str = [[NSUserDefaults standardUserDefaults] stringForKey:@"token"];

Or You can use singleton.

user2545330
  • 408
  • 4
  • 17
0

Try to Declare that varible into Appdelegate as a property and syntesize it in .m . Or aceess that varible in any class using this.

AppDelegate * appdelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    AppDelegate.yourvarible;

or either you used NSUserDefault.

[[NSUserDefaults standardUserDefaults]setObject:token forKey:@"tokan"];
Jitendra
  • 5,055
  • 2
  • 22
  • 42
  • 3
    How do I know that you had copy the first question? Because you made the same mistake TOKAN – Andrea Jul 29 '13 at 07:42
0

You can create a property in ur appdelegate and set the property when u get the token.

//getter
NSString *token=[(MyAppDelegate *)[[UIApplication sharedApplication] delegate].recievedToken;

//setter
[(MyAppDelegate *)[[UIApplication sharedApplication] delegate].recievedToken=token;
zahreelay
  • 1,742
  • 12
  • 18