HHParams 0.2.1

HHParams 0.2.1

测试已测试
语言 SwiftSwift
许可 MIT
发布最后发布2017年4月
SwiftSwift 版本3.0
SPM支持 SPM

维护者 zhuimiao



HHParams 0.2.1

  • zhuimiao

HHParams

示例

要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install

要求

ios9.0 swift3.0

安装

HHParams 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile:

pod "HHParams"

使用

1. 通用参数

  • 运营商名称
   let carriername = HHParams.carriername()
        print("获取运营商名称:\(carriername)")
  • 客户端版本
 let client_v = HHParams.client_v()
        print("获取客户端版本号:\(client_v)")
  • 手机用户名称
       let phoneUserName = HHParams.phoneUserName()
        print("获取手机用户名称:\(phoneUserName)")
  • ios版本
        let osversion = HHParams.osversion()
        print("获取ios版本号:\(osversion)")```
* 手机型号

```swift
    let phoneModel = HHParams.phoneModel()
        print("获取手机型号:\(phoneModel)")
  • 广告商id (advertisingIdentifier)
   let idfa = HHParams.getIDFA()
        print("广告商id:\(idfa)")
  • 供应商id (identifierForVendor)
    let idfv = HHParams.getIDFV()
        print("供应商id:\(idfv)")

2.使用钥匙串保存登录密码

使用 UserDefault 存储敏感信息,是不安全的,幸好在 ios 为我们提供了更安全的工具:钥匙串。许多 APP 用钥匙串存储敏感信息,比如用户的密码,银行卡信息等。

  //创建钥匙串管理对象
        let tool = HHKeyChain()
        //设置服务名称
        tool.serviceName = "loginService"
        //设置 key ,value
        let key = "username"
        let value = "password"
        
        // 保存 value 到 KeyChain
        tool .hh_setValue(value, key)
        // 从 KeyChain 读取 value
        let keyChainPassword:String = tool.hh_valueForKey(key)
        print("从 KeyChain 中获取的登录密码:\(keyChainPassword)")

3.使用钥匙串保存设备唯一标示符

iOS 设备唯一标示符(udid)有两种方案:

  • 广告商id 作为设备唯一标示符
  • 供应商id 设备唯一标示符

缺陷:

  • App 卸载重装后广告商id、供应商id 都会发生改变

解决方案:

  1. 先从钥匙串里找udid, 找到就用这个
  2. 找不到就获取手机udid,并保存到钥匙串
st=>start: 开始
e=>end: 结束
e1=>end: 结束
op=>operation: 从钥匙串里查找udid
op1=>operation: 获取 App udid
op2=>operation: 存储到钥匙串
cond=>condition: 找到?
st->op->cond
cond(yes)->e
cond(no)->op1->op2->e1

        let tool = HHKeyChain()
        tool.serviceName = "udidService"
        
        // 保存 广告商id
        var idfa:String = ""
        let idfaKey = "idfa"
        
        idfa = tool.hh_valueForKey(idfaKey)
        if idfa == "" {
            idfa = HHParams.getIDFA()
            tool.hh_setValue(idfa, idfaKey)
        }
        print("广告商id为:\(idfa)")

4. 使用钥匙串获取其他 App 保存的数据

钥匙串可以分组共享数据。

要求:

  • 两个App 必须同属于一个开发者账号下

步骤:

  1. project->target->capabilities->KeychainSharing 设置为 on
  2. 添加要共享数据的 bundle id,这里不只是添加自己的bundle id,也要添加要共享的 bundle id
  3. .entitlements ->Keychain Access Groups-> 添加App id前缀。例如:574C886U7L.org.boitx.mimamiao 574C886U7L.vpn.boitx.org
  4. 添加代码
        let tool = HHKeyChain()
        tool.serviceName = "shareDataService"
        tool.accessGroup = "574C886U7L.org.boitx.mimamiao"
        let groupShareDataKey = "ShareDataKey"
        let groupShareDataValue = tool.hh_valueForKey(groupShareDataKey)
        print("groupShareDataValue:\(groupShareDataValue)")

作者

zhuimiao, [email protected]

许可

HHParams遵循MIT许可。有关更多信息,请参阅LICENSE文件。