前面大概介绍了react-native的运行helloword级别的入门,因此以后简单的东西就不写了,毕竟官网上都可以找到。javascript
reactnative官网:https://facebook.github.io/react-native/docs/getting-started.htmlhtml
reactnative中文网:http://reactnative.cn/docs/0.25/getting-started.htmljava
以后我会把工做中遇到的一些react-native的深坑分享一下。react
正题===========================git
客户端cookies的获取就是一个大坑。github
1.使用三方web
研究ReactNative的源码发现,框架中并无实现iOS的NSHTTPCookieStorage的api,因此搜遍百度谷歌和bing,最终找到了一个哥们写的第三方cookies工具:react-native
https://github.com/joeferraro/react-native-cookiesapi
这里须要一提的是,我须要取得cookie不只是dictionary形式的cookies,而是用于http请求的cookiesHeader(比较特殊),其格式是string,在OC中取得的方式是:cookie
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
NSDictionary *header = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
但这一块原做者的框架中貌似存在必定的问题,因此我写了一个pull request,具体能够访问我fork的项目:
https://github.com/rayshen/react-native-cookies
(若是你须要取得的Cookie是用来解析取值或是保存从新加入的,用get("url",res)或者getAll()函数取得的比较适合)
2.获取当前url
这就须要结合webview控件来进行操做了。
首先咱们须要肯定当前的url,当前的url能够从webview控件绑定的事件onNavigationStateChange去取得:
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
onNavigationStateChange(navState) { console.log(navState.url); this.curUrl = navState.url; }
3.取得url的cookie header:
CookieManager.getHeader(this.curUrl, (err, res) => { console.log('Got cookies for url: ' + res.Cookie); })
4.取得url的全部cookies
CookieManager.get('http://example.com', (err, res) => { console.log(res); })
5.取得当前全部的cookies
CookieManager.getAll((err, res) => { console.log('cookies!'); console.log(err); console.log(res); });
须要注意的是,getAll()和set()都是iOS Only的函数。