如何讓 iOS UIWebView 連線時傳送自訂 Cookie 的方法[转]
利用 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 執行畫面如下:
參考資料
如何讓 iOS UIWebView 連線時傳送自訂 Cookie 的方法[转]的更多相关文章
- 在 Windows 上遇到非常多 TIME_WAIT 連線時應如何處理
我們公司所代管的網站裡,有幾個流量是非常大的,在尖峰的時刻同時上線人數可能高達數千到數萬人,而在這個時候如果使用 netstat 或 TCPView 查看所有 TCP 連線時就會看到非常多處於 ...
- Ubuntu 14 設定 遠端連線,讓別台電腦可以連線進來
Ubuntu 14 需 disable 加密,方可 遠端連線 此台電腦 xxx@xxx-ThinkPad-T460p:~$ gsettings set org.gnome.Vino require-e ...
- [ Windows] [ OS ] [ Remote Desktop ] 開啟同一個帳號同時2的連線RDP的方式
感謝同事 Allen 的Support :) 執行>gpedit.msc 電腦設定>Windows元件>遠端桌面服務>遠端桌面工作階段主機>連線>限制遠端桌面服務的 ...
- Iptables網路連線限制及攻擊防護和相關設定
[筆記整理]Iptables網路連線限制及攻擊防護和相關設定 1. 限制每個IP連接HTTP最大併發50個連接數 iptables -A INPUT -p tcp --dport 80 -m conn ...
- ios UIWebView自定义Alert风格的弹框
之前开发过一个App,因为公司之前写好了网页版的内容和安卓版本的App,我进去后老板要求我ios直接用网页的内容,而不需要自己再搭建框架.我一听,偷笑了,这不就是一个UIWebView吗?简单! 但是 ...
- NB實體連線到公司的網路,無法上網解決方案,需設 proxy。
未使用 VPN Cisco Anyconnect 已連線到公司的網路: google-chrome-stable --proxy-server="proxy.XXXcomm.com:3128 ...
- ios UIWebView截获html并修改便签内容(转载)
ios UIWebView截获html并修改便签内容 博客分类: iphone开发iphone开发phoneGap uiwebviewstringByEvaluatingJavaScriptFromS ...
- IOS UIWebView截获html并修改便签内容,宽度自适应
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服 ...
- ios UIWebView截获html并修改便签内容
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...
随机推荐
- Phonegap 通知 Notification
通知 Notification 一.notification.alert 对话框 notification.alert 响铃 notification.beep 震动 notification.vib ...
- usbip install
# README for usbip-utils## Copyright (C) 2011 matt mooney <mfm@muteddisk.com># 2 ...
- 二货Mysql中设置字段的默认值问题
Mysql设置字段的默认值的确很落伍 1.不支持函数 2.只支持固定常量. 经常用到的日期类型,因为不支持getdate或者now函数,所以只能设置timestamp类型 而且还必须在默认值那个地方写 ...
- 图片采集器_PHP
现在国内模仿“pinterest”的越来越多了,之前我做过一个基于chrome浏览器上的一个“图片采集工具”,类似于“花瓣网“那样的,初期我觉得挺简单,后来做起来发现还是挺复杂的,特别是整合到你自己的 ...
- 使用css弹性盒子模型
提示: 当期内容不充实, 修改后再来看吧 以下称:弹性子元素: 子元素, 弹性容器: 容器 弹性盒子的属性 1. css弹性盒子模型规定了弹性元素如何在弹性容器内展示 2. 弹性元素默认显示在弹性容器 ...
- 关于Selenium HTMLTestRunner 无法生成测试报告
解决方法 1: filename = ‘E:\testresult.html’,如果是在windows环境,文件名要使用以下几种格式. ①filename = 'E:\\testresult.html ...
- 551. Student Attendance Record I + Student Attendance Record II
▶ 一个学生的考勤状况是一个字符串,其中各字符的含义是:A 缺勤,L 迟到,P 正常.如果一个学生考勤状况中 A 不超过一个,且没有连续两个 L(L 可以有多个,但是不能连续),则称该学生达标(原文表 ...
- Javascript typeof 用法
在js里用到数组,比如 多个名字相同的input, 若是动态生成的, 提交时就需要判断其是否是数组. if(document.mylist.length != "undefined" ...
- 「小程序JAVA实战」小程序搜索功能(55)
转自:https://idig8.com/2018/09/23/xiaochengxujavashizhanxiaochengxusousuogongneng54/ 通过用户搜索热销词,将热销词添加到 ...
- Sqlserver2012的数据导入到sqlserver2008或sqlserver2008R2
我采取的是sql语句的方式 将Sqlserver2012的表结构导出成sql语句,数据也导出成sql语句 一.点击数据库名称右键=========>属性 二.导出表结构与数据为sql语句 htt ...