2014-04-04 90 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]; 
}