user831679
user831679

Reputation:

Making NSString global to whole project iPhone

In my iPhone application, on login to web services I get a session ID which gets changed on every login. I want to use that session ID for y other view controllers so that I would not have to use username password again and again and also I have to the session ID with the other links of web services to get the required JSON data to iPhone.

Should I define it as a macro or there is another way to do this.

session:"7e5085390e1877fd83f7346093c8304b"

If it can be done with macro then how can i achieve this?

On login url used as:

NSString *urlAsString = [NSString stringWithFormat:@"http://url/rest/mobile/session"];

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; 

[urlRequest setTimeoutInterval:30.0f];

[urlRequest setHTTPMethod:@"POST"];

[urlRequest addValue:user forHTTPHeaderField:@"uid" ];
[urlRequest addValue:password forHTTPHeaderField:@"passwd" ];

When getting data for other views:

NSString *urlAsString = [NSString stringWithFormat:@"http://url/rest/assetgroups/7e5085390e1877fd83f7346093c8304b"];

Please let me have your suggestions

Upvotes: 0

Views: 1580

Answers (4)

Mohammad Kamar Shad
Mohammad Kamar Shad

Reputation: 6067

For Making Global the NSString, you should declared this NSString in AppDelegate Class

// declared NSString in Delegate Class

suppose you have TestAppDelegate class

// Now you can Access that String Object As below

TestAppDelegate* appDelegate=(TestAppDelegate*)[[UIApplication     sharedApplication]delegate];

//now you can access that string in any class throughout the App

appDelegate.stringObject;

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385660

As a Global Variable

Since Objective-C is a superset of C, one approach is to simply make a global variable, just like you would in C. You want to declare the variable in a header file, like this:

Globals.h

extern NSString *sessionId;

Any .m file that needs to use sessionId can #import "Globals.h" to get the declaration.

You also need to define the variable in one of your .m files. For example, you could add this to main.m, outside of any function definition:

main.m

NSString *sessionId;

As an Application Delegate Property

Another approach is to make the variable be a property of your application delegate, since that is a globally-accessible object already. You need to declare it in AppDelegate.h:

AppDelegate.h

@interface AppDelegate

@property (strong, nonatomic) NSString *sessionId;

// other properties and methods of AppDelegate here

@end

and you synthesize it in AppDelegate.m:

@implementation AppDelegate {
    // ivars here
}

@synthesize sessionId = _sessionId;

// Rest of AppDelegate method definitions and synthesizers here

@end

and you can access the property like this:

((AppDelegate *)[UIApplication sharedApplication].delegate).sessionId = whatever();
NSLog(@"session id = %@", ((AppDelegate *)[UIApplication sharedApplication].delegate).sessionId);

Upvotes: 5

Arun
Arun

Reputation: 2281

use this set the session

      NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
 [userDefault setObject:@"7e5085390e1877fd83f7346093c8304b" forKey:@"session"];

and get like this

   NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSLog(@"%@",[userDefault objectForKey:@"session"]);

Upvotes: 1

Vishal Kardode
Vishal Kardode

Reputation: 971

As you are going to use it as Login item, you can not add it as macro. I guess NSUserDefault will be a good option .

Upvotes: 0

Related Questions