利用 NSHTTPCookieStorage 管理 Cookie 傳送

在 iOS 中如果自行建立 UIWebView 來開啟遠端站台資料,這時可以透過以下方法加入 Cookie。原理是透過 iOS 提供的 NSHTTPCookieStorage 元件來控制所有從這個 Application 發出的 HTTP Request,如果在 UIWebView 有使用 iFrame 或者 AJAX 發出的 Request 同樣會受到影像,算是一個方便的功能,讓 Cookie 可以集中管理。

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#import "AppDelegate.h"
 
@implementation AppDelegate
 
@synthesize window = _window;
 
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
 
    // 關閉 Statusbar
    [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
 
    // 定義 cookie 要設定的 host
    NSURL *cookieHost = [NSURL URLWithString:@"http://blog.toright.com:80/"];
 
    // 設定 cookie
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
                          [NSDictionary dictionaryWithObjectsAndKeys:
                           [cookieHost host], NSHTTPCookieDomain,
                           [cookieHost path], NSHTTPCookiePath,
                           @"COOKIE_NAME",  NSHTTPCookieName,
                           @"COOKIE_VALUE", NSHTTPCookieValue,
                           nil]];
 
    // 設定 cookie 到 storage 中
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
 
    // 建立 NSURLRequest 連到 cookie.php,連線的時候會自動加入上面設定的 Cookie
    NSString *urlAddress = @"http://blog.toright.com/cookie.php";
    NSURL *myurl = [NSURL URLWithString:urlAddress];  
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:myurl];
 
    // 建立 UIWebView
    UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
    // 設定 UIWebView 讀取的位置
    [self.window addSubview:webView];
    [webView loadRequest:requestObj];
    [webView release];
    [self.window makeKeyAndVisible];
 
    return YES;
}
 
@end

用來驗證是否有傳送 Cookie 的程式,cookie.php 的程式碼如下,單純顯示收到的 Cookie:

 
1
2
3
4
<?php
// 顯示收到的 Cookie
print_r($_COOKIE);
?>

App 執行畫面如下:

參考資料

最新文章

  1. ytu 2011: C语言实验——找中间数(水题)
  2. 所学新知——int、char型转string 类型等
  3. VMware虚拟机打开不了操作系统的解决方案
  4. 还是说Memory Model,gcc的__sync_synchronize真是太坑爹了
  5. 反射给对象赋值遇到的问题——类型转换[转http://blog.csdn.net/xiaohan2826/article/details/8536074]
  6. android开发launcher
  7. c语言-扑克牌小魔术
  8. Go 语言运算符
  9. 在MySQL和PostgreSQL之外,为什么阿里要研发HybridDB数据库?
  10. scheduling while atomic和bad: scheduling from the idle thread(转)
  11. AngelToken——富有价值的区块链服务平台
  12. 设置Linux环境变量的方法和区别_Ubuntu
  13. std::lock_guard和std::unique_lock
  14. winserver2012 自启动软件
  15. Vue2.0,Express实现的简单跨域
  16. BZOJ.1013.[JSOI2008]球形空间产生器(高斯消元)
  17. 转:[大数据竞赛]协同过滤在这个问题上是否work
  18. Beta阶段——3
  19. ArcEngine中的缩放地图
  20. GoLand 调试 Go

热门文章

  1. kettle环境变量的设置和获取
  2. WCF揭秘学习笔记(1):基础知识
  3. Java与SQL Server, MySql, Oracle, Access的连接方法以及一些异常解决
  4. Tomcat 容器的设计和实现
  5. &lt;转--大话session&gt;
  6. mysql/oracle ip地址比较
  7. 【UVA】11825 Hackers&#39; Crackdown(状压dp)
  8. Windows RDP远程连接CentOS 7
  9. 使用TensorFlow识别照片中的物体
  10. Python 使用 Postfix 发送邮件