是否可以使用sharedHTTPCookieStorage为UIWebView手动设置cookie?

时间:2011-05-10 18:05:51

标签: objective-c ios uiwebview

我在iOS应用程序中有Web视图,需要对身份验证cookie进行适当的身份验证。我正在寻找一种在iOS应用程序的webview中设置cookie的方法,而不必发出设置cookie的出站请求,因为我已经在客户端上有auth信息。

这个post向我们展示了UIWebView cookie的存储位置。

现在我正在加载一个隐藏的Web视图来发出一个出站请求,但是我不想做一个设置简单cookie的外部请求。

4 个答案:

答案 0 :(得分:138)

是的,你可以这样做。首先,在applicationDidBecomeActive中添加此行

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

CookieAcceptPolicy在各个应用之间共享,可以在您不知情的情况下进行更改,因此您希望确保每次应用运行时都拥有所需的接受政策。

然后,设置cookie:

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"someValue123456" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];

// set expiration to one month from now or any NSDate of your choosing
// this makes the cookie sessionless and it will persist across web sessions and app launches
/// if you want the cookie to be destroyed when your app exits, don't set this
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

此cookie的名称为testCookie,值为someValue123456,并将随任何http请求一起发送至www.example.com。

关于设置cookie的一个重要提示,请在此处查看我的问题!

NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there?

答案 1 :(得分:14)

编辑:适应已修改的问题

NSHTTPCookieStorage有一个-setCookies:forURL:mainDocumentURL:方法,因此最简单的方法是使用NSURLConnection并实施-connection:didReceiveResponse:,提取Cookie并将其填充到Cookie jar中:

- ( void )connection: (NSURLConnection *)connection
          didReceiveResponse: (NSURLResponse *)response
{
    NSHTTPURLResponse        *httpResponse = (NSHTTPURLResponse *)response;
    NSArray                  *cookies;

    cookies = [ NSHTTPCookie cookiesWithResponseHeaderFields:
                             [ httpResponse allHeaderFields ]];
    [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
            setCookies: cookies forURL: self.url mainDocumentURL: nil ];
}

(您也可以使用NSDictionaryNSHTTPCookie中简单地提取properties对象,然后将字典写入磁盘。重新读取它就像使用{{}一样简单1}} NSDictionary,然后使用-dictionaryWithContentsOfFile:创建Cookie。)

然后,您可以在需要时将cookie从存储中拉出来:

-initWithProperties:

答案 2 :(得分:6)

在Swift 3中,所有键都包含在 public PXAction<EPEmployee> testAction; [PXButton] [PXUIField(DisplayName = "EXTENDED")] protected virtual IEnumerable TestAction(PXAdapter adapter) { EmployeeMaint employeeMaintGraph = PXGraph.CreateInstance<EmployeeMaint>(); employeeMaintGraph.Clear(); EPEmployee epEmployeeRow = new EPEmployee(); epEmployeeRow.AcctCD = "CODED"; epEmployeeRow.AcctName = "employee"; epEmployeeRow.Status = "A"; employeeMaintGraph.Employee.Insert(epEmployeeRow); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.vendorClassID>(epEmployeeRow, "EMPDEFAULT"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.classID>(epEmployeeRow, "EMPDEFAULT"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.departmentID>(epEmployeeRow, "ADMIN"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.positionLineCntr>(epEmployeeRow, 1); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.consolidateToParent>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.allowOverrideCury>(epEmployeeRow, true); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.allowOverrideRate>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.payToParent>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.acctName>(epEmployeeRow, "employee"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.vendor1099>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.taxAgency>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.updClosedTaxPeriods>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.taxReportRounding>(epEmployeeRow, "R"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.taxUseVendorCurPrecision>(epEmployeeRow, true); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.taxReportFinPeriod>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.taxPeriodType>(epEmployeeRow, "M"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.enableTaxStartDate>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.landedCostVendor>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.isLaborUnion>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.lineDiscountTarget>(epEmployeeRow, "E"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.ignoreConfiguredDiscounts>(epEmployeeRow, false); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.sVATReversalMethod>(epEmployeeRow, "D"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.sVATInputTaxEntryRefNbr>(epEmployeeRow, "M"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.sVATOutputTaxEntryRefNbr>(epEmployeeRow, "M"); employeeMaintGraph.CurrentEmployee.Cache.SetValueExt<EPEmployee.type>(epEmployeeRow, "EP"); employeeMaintGraph.CurrentEmployee.Update(epEmployeeRow); Contact contactRow = new Contact(); contactRow.LastName = "lastname"; employeeMaintGraph.Contact.Insert(contactRow); Address addressRow = new Address(); addressRow.CountryID = "US"; employeeMaintGraph.Address.Insert(addressRow); employeeMaintGraph.Actions.PressSave(); return adapter.Get();} 结构中:

HTTPCookiePropertyKey

答案 3 :(得分:1)

需要解决iOS 10引入的cookie限制,这使得它们对不同的进程不可见。

这意味着在具有多处理功能的设备上,webview是一个与您的应用程序截然不同的流程,这意味着您的应用程序可以使用#34;会话不再自动传输到webview。

所以在本质上,你需要这样做(即使之前的海报正确,它在iOS10之前自动运行)。