存储会话以在Webview IOS上使用它

时间:2018-10-27 08:24:24

标签: ios swift3 alamofire xcode8

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

这就是我到目前为止所拥有的

Sub Makro1()
Dim wss As String
wss = InputBox("Sheet name")
Sheets("Tabelle1").Range("A1").Value = Sheets(wss).Range("b1")

End Sub

我正在使用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) } 框架来发布帖子

1 个答案:

答案 0 :(得分:-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"];
        }
    }
}