使NSString全局到整个项目iPhone [英] Making NSString global to whole project iPhone

查看:26
本文介绍了使NSString全局到整个项目iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,登录到Web服务后,我得到一个会话ID,该ID在每次登录时都会更改.我想将该会话ID用于其他视图控制器,这样我就不必一次又一次地使用用户名密码,而且我也必须使用会话ID与Web服务的其他链接才能将所需的JSON数据获取到iPhone./p>

我应该将其定义为宏,还是有另一种方法可以做到这一点.

 会话:"7e5085390e1877fd83f7346093c8304b" 

如果可以使用宏完成操作,那我该如何实现呢?

在登录网址上用作:

  NSString * urlAsString = [NSString stringWithFormat:@"http://url/rest/mobile/session"];NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];[urlRequest setTimeoutInterval:30.0f];[urlRequest setHTTPMethod:@"POST"];[urlRequest addValue:HTTPHeaderField用户:@"uid"];[urlRequest addValue:HTTPHeaderField的密码:@"passwd"]; 

获取其他视图的数据时:

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

请让我提出您的建议

解决方案

作为全局变量

由于Objective-C是C的超集,因此一种方法是像在C中一样简单地创建一个全局变量.您想在头文件中声明该变量,如下所示:

Globals.h

  extern NSString * sessionId; 

任何需要使用 sessionId 的.m文件都可以 #import"Globals.h" 来获取声明.

您还需要在一个.m文件中定义变量.例如,您可以将其添加到 main.m 中的任何函数定义之外:

main.m

  NSString * sessionId; 

作为应用程序委托属性

另一种方法是使变量成为应用程序委托的属性,因为该对象已经是全局可访问的对象.您需要在 AppDelegate.h 中声明它:

AppDelegate.h

  @interface AppDelegate@属性(强的,非原子的)NSString * sessionId;//这里的AppDelegate的其他属性和方法@结尾 

,然后在 AppDelegate.m 中将其合成:

  @implementation AppDelegate {//此处为ivars}@synthesize sessionId = _sessionId;//这里的其余AppDelegate方法定义和合成器@结尾 

,您可以这样访问属性:

 (((AppDelegate *)[UIApplication sharedApplication] .delegate).sessionId = what();NSLog(@会话ID =%@",((AppDelegate *)[UIApplication sharedApplication] .delegate).sessionId); 

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

解决方案

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);

这篇关于使NSString全局到整个项目iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆