NSR_SDK_v3_swift 3.1.0

NSR_SDK_v3_swift 3.1.0

Alessandro Infurna 维护。



  • Neosurance

NeosuranceSDK_v3_swift

Neosurance iOS SDK 版本 3 (swift)

iOS - NeosuranceSDK_v3_swift

  • 从设备传感器和托管应用中收集信息
  • 与人工智能引擎交换信息
  • 发送推送通知
  • 显示着陆页面
  • 显示已购政策列表

安装

iOS

  1. NeosuranceSDK_v3_swift 通过 CocoaPods 提供。要安装它,请简单地将其以下行添加到您的 Podfile 中(也请参阅 "https://github.com/neosurance/NeosuranceSDK_v3_swift_example" 中的 "NSR SDK SwiftExample")
target 'NSR SDK SwiftExample' do  
  use_frameworks!

  pod 'NSR_SDK_v3_swift'
end
  1. 在 Podfile 相同的目录下运行
pod install

需求

  1. 在您的 info.plist 文件中,请确保有以下权限
<key>NSCameraUsageDescription</key>
<string>use camera...</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Always and when in use...</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Always...</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>When in use...</string>
<key>NSMotionUsageDescription</key>
<string>Motion...</string>

设置

  1. 设置

    在您的应用程序启动流程中较早的位置(典型情况下在应用程序的 application didFinishLaunchingWithOptions 方法中)使用以下方式调用 setup 方法

    base_url:我们提供,仅在未配置 securityDelegate 时使用
    code:我们提供的社区代码
    secret_key:我们提供的社区密钥
    dev_mode 可选:[0|1] 启用 开发者模式

    let nsr = NSR.getSharedInstance()
    
    nsr.workflowDelegate = WFDelegate()
    
    let settings = NSMutableDictionary()
    settings.setValue(self.config["base_url"], forKey: "base_url")
    settings.setValue(self.config["code"], forKey: "code")
    settings.setValue(self.config["secret_key"], forKey: "secret_key")
    settings.setValue(true, forKey: "dev_mode")
    
    nsr.setup(settings: settings)	
  2. forwardNotification

    为了管理由 SDK 生成的推送,请将 forwardNotification 方法添加到您的应用通知处理程序中

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
    	if let code = response.notification.request.content.userInfo["code"] as? String {
    		print(">>> code: " + code)
    	}
        
    	if let expirationTime = response.notification.request.content.userInfo["expirationTime"] as? String {
    		print(">>> expirationTime: " + expirationTime)
    	}
        
    	if(NSR.getSharedInstance().forwardNotification(response: response)){
    		//TODO: handle notification...
    	}
        
    	completionHandler()
        
    }

    并且请记住还要实现

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            completionHandler(UNNotificationPresentationOptions.init(arrayLiteral: [.alert, .sound])) //.badge
    }
  3. securityDelegate 可选

    如果必须使用任何策略来加密通信。
    可以配置实现以下协议的 securityDelegate

    public protocol NSRSecurityDelegate: NSObject{
        func secureRequest( endpoint: String, payload: NSDictionary, headers: NSDictionary, completionHandler: @escaping(_ responseObject: NSDictionary, _ error: NSError?)->() )
    }	
    public func secureRequest(endpoint: String, payload: NSDictionary, headers: NSDictionary, completionHandler: @escaping (NSDictionary, NSError?) -> ()) {...}
  4. setWorkflowDelegate 可选项

    如果必须中断采购工作流程以执行用户登录或支付。
    必须配置实现以下接口的 workflowDelegate

    public protocol NSRWorkflowDelegate: NSObject{
        func executeLogin(url: String)->(Bool)
        func executePayment(payment: NSDictionary, url: String)->(NSDictionary)
        func confirmTransaction(paymentInfo: NSDictionary)->()
        func keepAlive()->()
    }
    let nsr = NSR.getSharedInstance()
    nsr.workflowDelegate = WFDelegate()	

    当执行登录或支付时,您必须调用 loginExecutedpaymentExecuted 方法以恢复工作流程

    NSR.getSharedInstance().loginExecuted(url: url)
    ...
    NSR.getSharedInstance().paymentExecuted(paymentInfo: paymentInfo, url: url)
  5. 注册用户

    当用户被您的应用程序识别后,在本 SDK 中注册他,创建一个 NSRUser 并使用 registerUser 方法。
    NSRUser 包含以下字段

    code:您系统中用户的代码(可以等于电子邮件地址)
    email:电子邮件地址是实际的主键
    firstname 可选项
    lastname 可选项
    mobile 可选项
    fiscalCode 可选项
    gender 可选项
    birthday 可选项
    address 可选项
    zipCode 可选项
    city 可选项
    stateProvince 可选项
    country 可选项
    extra 可选项:将与我们共享
    locals 可选项:不会在设备外部公开

    let user = NSRUser()        
    
    user.code = (self.config["user.code"] as! String)
    user.email = (self.config["user.email"] as! String)
    user.firstname = (self.config["user.firstname"] as! String)
    user.lastname = (self.config["user.lastname"] as! String)
    user.country = (self.config["user.country"] as! String)
    user.fiscalCode = (self.config["user.fiscalCode"] as! String)
    user.address = (self.config["user.address"] as! String)
    user.city = (self.config["user.city"] as! String)
    user.stateProvince = (self.config["user.stateProvince"] as! String)
    
    let locals = NSMutableDictionary()
    
    locals.setObject(user.email ?? "", forKey:"email" as NSCopying)
    locals.setObject(user.firstname ?? "", forKey:"firstname" as NSCopying)
    locals.setObject(user.lastname ?? "", forKey:"lastname" as NSCopying)
    locals.setObject(user.fiscalCode ?? "", forKey:"fiscalCode" as NSCopying)
    locals.setObject(user.address ?? "", forKey:"address" as NSCopying)
    locals.setObject(user.city ?? "", forKey:"city" as NSCopying)
    locals.setObject(user.stateProvince ?? "", forKey:"stateProvince" as NSCopying)
    locals.setObject("fake-push", forKey:"pushToken" as NSCopying)            
    
    user.locals = locals
    
    let nsr = NSR.getSharedInstance()
    nsr.registerUser(user: user)	
  6. 显示应用

    可以使用 showApp 方法显示已购买的策略列表(communityApp)。

    NSR.getSharedInstance().showApp()

    或者

    let params = NSMutableDictionary()
    params.setObject(profiles, forKey: "page" as NSCopying)
    NSR.getSharedInstance().showApp(params: params)	
  7. 显示URL 可选项

    如果需要自定义视图,可以使用 showUrl 方法。

    NSR.getSharedInstance().showUrl(url: url)

    或者

    let params = NSMutableDictionary()
    params.setObject("true", forKey: "profile" as NSCopying)
    NSR.getSharedInstance().showUrl(url: url, params: params?)	
  8. 发送事件

    应用程序可以使用 sendEvent 方法向系统发送显式事件。

    let params = NSMutableDictionary()
    params.setObject("latitude", forKey: "latitude" as NSCopying)
    params.setObject("longitude", forKey: "longitude" as NSCopying)
    NSR.getSharedInstance().sendEvent(event:"position", payload:payload)
  9. sendAction 可选

    应用程序可以使用 sendAction 方法向系统发送跟踪信息事件。

    let nsr = NSR.getSharedInstance()
    nsr.sendAction(action: "read" as! String, code: "xxxx123xxxx" as! String, details: "general condition read" as! String)

用法(示例演示流程)

请参阅 "https://github.com/neosurance/NeosuranceSDK_v3_swift_example" 中的 "NSR SDK SwiftExample"。

作者

[email protected]

许可协议

NeosuranceSDK 在 MIT 许可协议下可用。更多信息请参阅 LICENSE 文件。