2014-04-04 41 views
0

我有一个使用AFHTTPRequestOperationManager使用下面的代码来执行登录应用程序:创建Safari浏览器的cookie

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
          loginNameField.text, @"username", 
          loginPasswordField.text, @"password", 
          uniqueUserToken, @"device_id", 
          nil]; 

    NSMutableURLRequest* request = [self.operationManager.requestSerializer requestWithMethod:@"POST" URLString:LOGIN_URL parameters:params error:nil]; 

    AFHTTPRequestOperation* operation = [self.operationManager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { 

    //completion block 

    } 

[self.operationManager.operationQueue addOperation:operation]; 

我想要做的是,一旦用户登录,在与应用,请在Safari中创建一个cookie,以便用户在浏览器中直接转到主站点而不是登录页面。

我尝试使用以下,但仍Safari浏览器登陆该网站的登录页面:

  NSString *myRequestString = [NSString stringWithFormat:@"username=%@&password=%@&device_id=%@", loginNameField.text, loginPasswordField.text, uniqueUserToken]; 

      NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length ]]; 

      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:LOGIN_URL]]; 

      [request setHTTPMethod: @"POST" ]; 

      [request setHTTPBody:myRequestData]; 

      NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

      [connection release]; 

      [request release]; 

现在我明白了应用程序的沙盒中的iOS,因此,这里的问题。我已阅读其他问题,但我对javaScript的了解还处于起步阶段。任何帮助,将不胜感激。

回答

0

我建议您使用NSHTTPCookie或者使用类似UICKeyChainStore的库在KeychainStore中保存用户名和密码。然后在AppDelegate中,您可以检查用户是否登录。我个人使用第二个选项,因为它可以安全地保存数据,直到用户选择注销。但更临时的解决方案是保存数据NSHTTPCookie

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL: theURL]; 
for (NSHTTPCookie *cookie in cookies) 
{ 
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] delete or set here]; 
}