【问题标题】:Enable Cookies in UIWebView在 UIWebView 中启用 Cookie
【发布时间】:2012-04-05 09:41:43
【问题描述】:

如何在使用 UIWebView 窗口的 iPhone 应用程序中启用 cookie,以便我的登录系统正常工作?

【问题讨论】:

    标签: ios iphone cookies ios5 uiwebview


    【解决方案1】:

    如果您登录的站点是 ASP.NET 站点,则问题可能是由于 UIWebView 发送了无法识别的用户代理。见Change User Agent in UIWebView (iPhone SDK)

    【讨论】:

      【解决方案2】:

      一定要开始

      [NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy = 
          NSHTTPCookieAcceptPolicyAlways;
      

      但是,正如@JoelFan 所提到的,问题可能是您的用户代理字符串导致 ASP.NET 尝试在无 cookie 登录时失败。而不是包含

      的响应

      Set-Cookie:.ASPXAUTH=really-long-hex-number

      它返回一个重定向到类似的东西

      位置:/(F(long-sorta-base64ish-looking-string))/

      默认的 UIWebView 用户代理字符串类似于

      用户代理:Mozilla/5.0(iPad;CPU OS 7_0_2,如 Mac OS X)AppleWebKit/537.51.1(KHTML,如 Gecko)Mobile/11A501

      但是 ASP.NET 不喜欢这样。 Safari 会发送如下内容:

      User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A501 Safari/ 9537.53

      尽早执行以下操作,可能在您的 AppDelegate.m 中

      // DON'T try to reuse a UIWebView for this. 
      UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectZero];
      // This webview has already decided to use the default user agent string.
      
      // let's use javascript to get the existing user agent string
      NSString *userAgent = [wv stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
      
      // let's tack on some stuff to make ASP.NET happy
      userAgent = [userAgent stringByAppendingString:@" Version/7.0 Safari/9537.53"];
      
      [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}];
      // New UIWebViews inited after here will use the user agent string you made.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-25
        • 2015-09-29
        • 2015-02-17
        相关资源
        最近更新 更多