@aaxis/react-native-cookies 中文文档教程

发布于 5年前 浏览 27 更新于 3年前

@aaxis/react-native-cookies

React Native 的 Cookie 管理器,这是从 react-native-cookies 派生出来的,AndroidX 是否支持 & 一些错误修复。

Installation

yarn add react-native-cookies

Linking

Automatic (recommended)

react-native link react-native-cookies

Manual

如果自动链接不起作用,您可以按照以下说明手动链接此库:

iOS
  1. Open your project in Xcode, right click on Libraries and click Add Files to "Your Project Name" Look under node_modules/react-native-cookies/ios and add RNCookieManagerIOS.xcodeproj.
  2. Add libRNCookieManagerIOS.a to `Build Phases -> Link Binary With Libraries.
  3. Clean and rebuild your project
Android

运行 react-native link 链接 react-native-cookies 库。

或者,如果您遇到问题,请手动向给定文件添加以下内容:

android/settings.gradle

include ':react-native-cookies'
project(':react-native-cookies').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-cookies/android')

android/app/build.gradle

dependencies {
   ...
   compile project(':react-native-cookies')
}

MainApplication.java

在顶部,导入的位置:

import com.aaxis.cookiemanager.CookieManagerPackage;

CookieManagerPackage 类添加到导出的包列表中。

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.asList(
            new MainReactPackage(),
            new CookieManagerPackage()
    );
}

Usage

import CookieManager from 'react-native-cookies';

// Set cookies 
CookieManager.set(
{
  domain: dm,
  name: key,
  value: value.value,
  origin: domain,
  path: '/',
  version: '1',
  expiration: '2035-05-30T12:30:00.00-05:00',
},
useWebKit
).then(() => {
console.log(
  `set cookie to ${domain} of key: ${key}, value: ${value.value} success.`
)
if (callback) callback()
})

// Get cookies 
CookieManager.get(domain, useWebKit).then(res => {
    console.log(`get cookie from ${domain} => ${JSON.stringify(res)}`)
})

// list cookies
CookieManager.getAll().then(res => {
    console.log(`get all cookies from => ${JSON.stringify(res)}`)
})

// clear cookies
CookieManager.clearAll(useWebKit)
  .then((res) => {
    console.log('CookieManager.clearAll =>', res);
  });

TODO

  • Proper getAll dictionary by domain
  • Proper error handling
  • Anything else?

欢迎 PR!

@aaxis/react-native-cookies

Cookie manager for react native, this is fork from react-native-cookies and did AndroidX support & some bug fixes.

Installation

yarn add react-native-cookies

Linking

Automatic (recommended)

react-native link react-native-cookies

Manual

If automatic linking does not work, you can manually link this library by following the instructions below:

iOS
  1. Open your project in Xcode, right click on Libraries and click Add Files to "Your Project Name" Look under node_modules/react-native-cookies/ios and add RNCookieManagerIOS.xcodeproj.
  2. Add libRNCookieManagerIOS.a to `Build Phases -> Link Binary With Libraries.
  3. Clean and rebuild your project
Android

Run react-native link to link the react-native-cookies library.

Or if you have trouble, make the following additions to the given files manually:

android/settings.gradle

include ':react-native-cookies'
project(':react-native-cookies').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-cookies/android')

android/app/build.gradle

dependencies {
   ...
   compile project(':react-native-cookies')
}

MainApplication.java

On top, where imports are:

import com.aaxis.cookiemanager.CookieManagerPackage;

Add the CookieManagerPackage class to your list of exported packages.

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.asList(
            new MainReactPackage(),
            new CookieManagerPackage()
    );
}

Usage

import CookieManager from 'react-native-cookies';

// Set cookies 
CookieManager.set(
{
  domain: dm,
  name: key,
  value: value.value,
  origin: domain,
  path: '/',
  version: '1',
  expiration: '2035-05-30T12:30:00.00-05:00',
},
useWebKit
).then(() => {
console.log(
  `set cookie to ${domain} of key: ${key}, value: ${value.value} success.`
)
if (callback) callback()
})

// Get cookies 
CookieManager.get(domain, useWebKit).then(res => {
    console.log(`get cookie from ${domain} => ${JSON.stringify(res)}`)
})

// list cookies
CookieManager.getAll().then(res => {
    console.log(`get all cookies from => ${JSON.stringify(res)}`)
})

// clear cookies
CookieManager.clearAll(useWebKit)
  .then((res) => {
    console.log('CookieManager.clearAll =>', res);
  });

TODO

  • Proper getAll dictionary by domain
  • Proper error handling
  • Anything else?

PR's welcome!

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文