cookie在viewcontrols之间传递

时间:2014-04-25 22:11:07

标签: ios objective-c

我正在开发iPhone应用程序。我有一个问题。我创建了php - mysql脚本。然后我在xcode上创建了登录屏幕。我发送http请求,我读取cookie。但我想做一些其他的视图控制。所以我需要保存phpsessid cookie并发送带有cookie的http请求。我的问题是我怎么做的?

FirstViewControl(登录屏幕).m文件

#import "SecondViewControl.h"

// If you want to get all of the cookies:
                        NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://www.iosapp.com"]];
                        NSLog(@"How many Cookies: %d", all.count);
                        // Store the cookies:
                        // NSHTTPCookieStorage is a Singleton.
                        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:all forURL:[NSURL URLWithString:@"http://www.iosapp.com/"] mainDocumentURL:nil];
                        // Now we can print all of the cookies we have:
                        for (NSHTTPCookie *cookie in all)
                        {
                            SecondViewControl* myViewController = [[SecondViewControl alloc]initWithCookie:cookie];
                            NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate);

                        }

SecondViewControl.h

    @interface SecondViewControl : UIViewController{
    NSHTTPCookie * _cookie;//ivar declaration
}
-(id)initWithCookie :(NSHTTPCookie *)cookie;//function prototype

SecondViewControl.m

@implementation SecondViewControl
-(id)initWithCookie :(NSHTTPCookie *)cookie
{
    _cookie = cookie;
    return self;
}

SecondViewControl.m按钮

- (IBAction)homeb:(id)sender {
    NSLog(@"Name: %@ : Value: %@, Expires: %@", _cookie.name, _cookie.value, _cookie.expiresDate);

}

没有工作。 null cookie:/ NSlog。

2014-04-26 04:35:22.186 login[73801:60b] Name: PHPSESSID : Value: 49c1ed0c570dd85a3f7593c7e039086d, Expires: (null)
    2014-04-26 04:35:22.186 login[73801:60b] Login SUCCESS
    2014-04-26 04:35:29.091 login[73801:60b] Name: (null) : Value: (null), Expires: (null) (clicked seconviewcontrol button.)

1 个答案:

答案 0 :(得分:0)

那么你的第一个问题是:“我如何传递给下一个viewController”

假设这是viewController.h

 @interface viewController : UIViewController
 {
     NSHTTPCookie * _cookie;//ivar declaration
 }

-(id)initWithCookie :(NSHTTPCookie *)cookie;//function prototype
 @end

在viewController .m

  @implementation viewController
  -(id)initWithCookie :(NSHTTPCookie *)cookie
  {
       _cookie = cookie;
       return self;
  }
     ... ... ...
  @end

登录

  viewController* myViewController = [viewController alloc]initWithCookie:cookie];

或者您只需在viewController.h中创建一个setter

 @interface viewController : UIViewController
 {
     NSHTTPCookie * _cookie;//ivar declaration
 }

-(void)setCookie :(NSHTTPCookie *)cookie;//function prototype
 ...
 @end

在viewController .m

  @implementation viewController
  -(void)setCookie :(NSHTTPCookie *)cookie
  {
       _cookie = cookie;
  }
     ... ... ...
  @end

登录

  viewController* myViewController = [viewController alloc]init];//or init with xib name or whatever is that

  //and then 

   [myViewController setCookie:cookie];//here you go you just passed cookie to your viewControllers ivar.

in .h

 @interface viewController : UIViewController
 {
    NSMutableArray* _cookiesArray;
 }

-(void)addCookieCookie :(NSHTTPCookie *)cookie;//function prototype
 @end

在viewController .m

  @implementation viewController
  -(void)addCookieCookie :(NSHTTPCookie *)cookie
  {
       if(!cookiesArray)//when you are running for first time you should to init the array like this
       {
           cookiesArray=[[NSMutableArray alloc]init];
       }
       [cookiesArray addObject:cookie];
  }
     ... ... ...

现在你可以在secondViewController中拥有一个数组       @end

关于你的第二个问题:请自己单独解答并向我们展示代码。