【问题标题】:Store session to use it on webview IOS存储会话以在 webview IOS 上使用它
【发布时间】:2019-03-31 21:10:22
【问题描述】:

大家好。我目前面临一个问题,我不知道如何存储 Web API 将给我的会话并使用它来获取数据并将其放在 webview 上

这是我到目前为止所得到的

let URL_USER_LOGIN = "https://xxxx.xxx.xxx/xxx/xxx"

let parameters: Parameters=[
"username" : usernameTextField.text!,
"password" : passwordTextField.text!
]

Alamofire.request(URL_USER_LOGIN, method: .post, parameters: parameters).responseString
{
response in
//WEBVIEW
//if success
let url = NSURL(string:"https://xxx.xxx.xxx/xxx/dashboard")
 let requestObj = URLRequest(url: url! as URL) self.WebView_Dashboard.LoadRequest(requesObj)
}

我正在使用Alamofire 框架进行发布请求

【问题讨论】:

  • 您可以获取 cookie 并从中获取 sessionid,而不是存储它,如果您想通过 session 获取数据,您可以使用这个 sessionid。

标签: ios swift3 alamofire xcode8


【解决方案1】:

据我了解。来自我的一个旧项目:(对不起,Objective C。很容易转换成 Swift)

-(void)saveCookies:(NSString*)user_id{
    NSMutableArray *theCookies = [NSMutableArray new];
    for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [theCookies addObject:cookie];
    }
    if (theCookies.count>0){
        NSData *cd = [NSKeyedArchiver archivedDataWithRootObject:theCookies];
        NSString *cookiesFilesPath = [appDelegate cookPathForUser:user_id]; // Here your path to file (or you can store it anywhere)
        [[NSFileManager defaultManager] createFileAtPath:cookiesFilesPath contents:[NSData data] attributes:nil];
        [cd writeToFile:cookiesFilesPath atomically:YES];
    }    
}

-(NSArray*)cookiesForUser:(NSString*)user_id{
    NSData *cookiesData = [NSData dataWithContentsOfFile:[appDelegate cookPathForUser:user_id]];
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesData];
    return cookies;
}

-(void)resetCookies {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
} 

- (void)applyCookies:(NSArray*)cookies {
    for (int i = 0; i<cookies.count; i++){
      NSHTTPCookie *cookie = [cookies objectAtIndex:i];
      [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
} 

- (void)addCookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request
{
    if ([cookies count] > 0)
    {
        NSHTTPCookie *cookie;
        NSString *cookieHeader = nil;
        for (cookie in cookies)
        {
            if (!cookieHeader)
            {
                cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]];
            }
            else
            {
                cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]];
            }
        }
        if (cookieHeader)
        {
            [request setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-05-06
    • 2017-06-08
    • 2018-05-02
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多