<file_sep>//
//  TypeDefs.h
//  bongSDK
//
//  Created by mario on 15/1/15.
//  Copyright (c) 2015年 Ginshell Inc. All rights reserved.
//

#ifndef bongSDK_TypeDefs_h
#define bongSDK_TypeDefs_h

/**
 *  bongSDK 返回码类型
 */
typedef NS_ENUM(int, bongSDKErrorCode){
    /**
     *  无错误
     */
    bongSDKErrorCodeNone,
    /**
     *  配置无效，即 `appID`、`appKey` 等配置无效
     */
    bongSDKErrorCodeInvalidateConfiguration,
    /**
     *  token 无效，需重新调用 `requestAccessTokenWithCallback:` 来获取
     */
    bongSDKErrorCodeInvalidateToken,
    /**
     *  参数错误
     */
    bongSDKErrorCodeParameterError,
    /**
     *  正在
     */
    bongSDKErrorCodeBusy,
    /**
     *  服务器返回未知错误
     */
    bongSDKErrorCodeResponseUnknown,
    /**
     *  bong 标识符错误
     */
    bongSDKErrorCodeInvalidatebongIdentifier,
    /**
     *  蓝牙未打开，需提醒用户开启蓝牙
     */
    bongSDKErrorCodeBLEPowerOff,
    /**
     *  不支持蓝牙低功耗
     */
    bongSDKErrorCodeBLEUnsupported,
    /**
     *  未获取到手环数据
     */
    bongSDKErrorCodeDataFetchNothing,
    /**
     *  接口调用错误
     */
    bongSDKErrorCodeInvalidAPI,
    /**
     *  产品 ID 不支持
     */
    bongSDKErrorCodeInvalidProductID,
    /**
     *  bong X/XX 事件通知错误
     */
    bongSDKErrorCodeXNoteError,
    /**
     *  意外断开设备
     */
    bongSDKErrorCodeUnexpectedDisconnection,
    /**
     *  网络连接错误
     */
    bongSDKErrorCodeNetworkError,
    /**
     *  请求成功
     */
    bongSDKErrorCodeRequestSuccess = 200,
    /**
     *  请求未授权
     */
    bongSDKErrorCodeReuqestUnAutherized = 401,
    /**
     *  签名错误
     */
    bongSDKErrorCodeRequestSignError = 403,
    /**
     *  越权错误
     */
    bongSDKErrorCodeRequestExceedingAuthority = 405,
    
    /**
     *  服务器内部错误
     */
    bongSDKErrorCodeRequestInternalError = 500,
    /**
     *  服务器返回未知错误
     */
    bongSDKErrorCodeRequestUnknownError = 2001,
    /**
     *  服务器返回错误
     */
    bongSDKErrorCodeRequestUpperLimit = 2002,
    /**
     *  服务器返回错误
     */
    bongSDKErrorCodeRequestLowerLimit = 2003,
    /**
     *  服务器返回错误
     */
    bongSDKErrorCodeRequestInEvent = 2004,
    /**
     *  服务器返回错误
     */
    bongSDKErrorCodeRequestOffEvent = 2005,
    /**
     *  服务器返回错误
     */
    bongSDKErrorCodeRequestDateLimit = 2007,
    /**
     *  服务器返回错误
     */
    bongSDKErrorCodeRequestDataLimit = 2007,
    /**
     *  服务器返回错误
     */
    bongSDKErrorCodeRequestUnbond = 2012,
};

/**
 *  bong 产品 ID
 */
typedef NS_ENUM(NSInteger, bongSDKProductID){
    /**
     *  无产品 ID
     */
    bongSDKProductIDNone = 0,
    /**
     *  bong II
     */
    bongSDKProductIDTwo = 101002,
    /**
     *  bong X
     */
    bongSDKProductIDX = 101003,
    /**
     *  bong XX
     */
    bongSDKProductIDXX = 101004,
};

/**
 *  Yes! 键触摸类型
 */
typedef NS_ENUM(int, YESKeyTouchType){
    /**
     *  未定义类型，任意触摸 Yes! 键都可能产生此类型
     */
    YESKeyTouchTypeNone,
    /**
     *  长触摸，Yes! 键灯闪烁两次松开
     */
    YESKeyTouchTypeLong,
    /**
     *  短触摸，Yes! 键灯闪烁一次松开
     */
    YESKeyTouchTypeShort,
};


/**
 *  bong X/XX 按键通知类型
 */
typedef NS_ENUM(NSInteger, bongSDKXNoteType){
    /**
     *  无类型
     */
    bongSDKXNoteTypeNone = 0,
    /**
     *  向上滑动
     */
    bongSDKXNoteTypeSwipeUp = 6,
    /**
     *  向下滑动
     */
    bongSDKXNoteTypeSwipeDown = 7,
    /**
     *  向左滑动
     */
    bongSDKXNoteTypeSwipeLeft = 8,
    /**
     *  向右滑动
     */
    bongSDKXNoteTypeSwipeRight = 9,
    /**
     *  入睡
     */
    bongSDKXNoteTypeSleepIn = 12,
    /**
     *  起床
     */
    bongSDKXNoteTypeSleepOut = 13,
    /**
     *  进入 bong 状态
     */
    bongSDKXNoteTypebongIn = 14,
    /**
     *  退出 bong 状态
     */
    bongSDKXNoteTypebongOut = 15,
    /**
     *  长按
     */
    bongSDKXNoteTypeLongPress = 25,
};

/**
 *  数据同步状态
 */
typedef NS_ENUM(int, bongSDKDataFetchStatus){
    /**
     *  初始状态
     */
    bongSDKDataFetchStatusNone,
    /**
     *  连接
     */
    bongSDKDataFetchStatusConnecting,
    /**
     *  同步
     */
    bongSDKDataFetchStatusFetching,
    /**
     *  上传
     */
    bongSDKDataFetchStatusUploading,
    /**
     *  成功结果
     */
    bongSDKDataFetchStatusSucceed,
    /**
     *  失败结果
     */
    bongSDKDataFetchStatusFailed,
    /**
     *  通讯成功，但设备上并未有此段数据
     */
    bongSDKDataFetchStatusFetchNothing,
};

/**
 *  bong 天数据的类型
 */
typedef NS_ENUM(int, bongSDKActivityType){
    /**
     *  无
     */
    bongSDKActivityTypeNone,
    /**
     *  睡眠类型
     */
    bongSDKActivityTypeSleep,
    /**
     *  运动类型
     */
    bongSDKActivityTypebong,
    /**
     *  非运动类型
     */
    bongSDKActivityTypeNotbong,
    /**
     *  摘下类型
     */
    bongSDKActivityTypeTakeOff,
    /**
     *  充电类型（bongII 手环无此类型，但 bongI 用户升级至 bongII 后，其历史数据可能有此类型）
     */
    bongSDKActivityTypeCharge,
};

/**
 *  运动类型数据的具体运动类型
 */
typedef NS_ENUM(int, bongSDKActivitybongType){
    /**
     *  无
     */
    bongSDKActivitybongTypeNone,
    /**
     *  热身
     */
    bongSDKActivitybongTypeWarmUp,
    /**
     *  健走
     */
    bongSDKActivitybongTypeStride,
    /**
     *  运动
     */
    bongSDKActivitybongTypeSport,
    /**
     *  跑步
     */
    bongSDKActivitybongTypeRun,
    /**
     *  游泳
     */
    bongSDKActivitybongTypeSwim,
    /**
     *  自行车
     */
    bongSDKActivitybongTypeCycling
};

/**
 *  非运动类型数据的具体运动类型
 */
typedef NS_ENUM(int, bongSDKActivityNotbongType){
    /**
     *  无
     */
    bongSDKActivityNotbongTypeNone,
    /**
     *  静
     */
    bongSDKActivityNotbongTypeStill,
    /**
     *  散步
     */
    bongSDKActivityNotbongTypeWalk,
    /**
     *  交通工具
     */
    bongSDKActivityNotbongTypeTrans
};

#endif
<file_sep># Uncomment this line to define a global platform for your project
platform :ios, '8.0'
# Uncomment this line if you're using Swift
use_frameworks!

target 'BongMe' do
  pod "OAuthSwift", "~> 0.5.0"
  pod 'JSONModel'
end

<file_sep>//
//  BongXTableViewController.swift
//  BongMe
//
//  Created by Ryan on 1/17/16.
//  Copyright © 2016 J.A.V.I.S. All rights reserved.
//

import UIKit

class BongXTableViewController: UITableViewController, bongSDKDelegate, UIPopoverPresentationControllerDelegate {

    private var popCtlr:ActivityIndicatorViewController?
    private var popMsg:String = ""
    private var scanning:Bool = false
    
    private var notifying:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        bongSDK.addDelegate(self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        bongSDK.removeDelegate(self)
        bongSDK.enableXNote(false)
    }
    

    // MARK: - UITableViewDelegate
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.row == 0 {
            let now = NSDate()
            bongSDK.fetchbongDataWithStartDate(now.dateByAddingTimeInterval(-60 * 30), endDate: now)
            performSegueWithIdentifier("Popover", sender: self)
            self.popMsg = "同步中..."
            return
        }
        performSegueWithIdentifier("Popover", sender: self)
        self.popMsg = "设置中..."
        bongSDK.enableXNote(!self.notifying)
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath == 1 {
            cell.textLabel!.text = self.notifying ? "停止时间通知" : "开启事件通知"
        }
    }

    //MARK: - bongSDKDelegate
    func bongSDKDataFetchStatusDidChange(status: bongSDKDataFetchStatus, error: NSError!) {
        if (error != nil) {
            NSLog("bongSDKDataFetchStatusDidChange error: %@", error)
            self.popCtlr?.dismissViewControllerAnimated(true, completion: nil)
            return
        }
        NSLog("bongSDKDataFetchStatusDidChange status %d", status.rawValue)
        if (status == bongSDKDataFetchStatus.Succeed ||
            status == bongSDKDataFetchStatus.FetchNothing) {
                self.popCtlr?.dismissViewControllerAnimated(true, completion: nil)
                return
        }
    }
    
    func bongSDKDidSwitchXNote(error: NSError!) {
        self.popCtlr?.dismissViewControllerAnimated(true, completion: nil)
        
        if ((error) != nil) {
            NSLog("bongSDKDidSwitchXNote error: %@", error);
            self.notifying  = false;
        } else {
            self.notifying = !self.notifying;
        }
        
        self.tableView.reloadData();
    }
    
    func bongSDKDidUpdateXNote(type: bongSDKXNoteType, error: NSError!) {
        if ((error) != nil) {
            NSLog("bongSDKDidUpdateXNote error: %@", error);
            return;
        }
        
        NSLog("bongSDKDidUpdateXNote type %td", type.rawValue);
    }

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if segue.identifier == "Popover" {
            self.popCtlr = segue.destinationViewController as? ActivityIndicatorViewController
            let naviRect = self.navigationController!.navigationBar.frame
            self.popCtlr!.popoverPresentationController!.sourceRect = CGRectMake(self.view.frame.width / 2 , self.view.frame.height/2 - naviRect.height, 1, 1)
            self.popCtlr!.popoverPresentationController!.delegate = self
            self.popCtlr!.message = self.popMsg
        }
        
    }
    
    // MARK: - UIPopoverPresentationControllerDelegate
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return !scanning
    }

}
<file_sep>//
//  ActivityTableViewController.swift
//  BongMe
//
//  Created by Ryan on 1/17/16.
//  Copyright © 2016 J.A.V.I.S. All rights reserved.
//

import UIKit

class ActivityTableViewController: UITableViewController,bongSDKDelegate,UIPopoverPresentationControllerDelegate {

    private var popCtlr:ActivityIndicatorViewController?
    private var popMsg:String = ""
    private var scanning:Bool = false
    
    private var resultText:String = ""
    @IBOutlet weak var resultTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        bongSDK.addDelegate(self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        bongSDK.removeDelegate(self)
    }

    // MARK: - UITableViewDelegate
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.row == 0 {
            performSegueWithIdentifier("Popover", sender: self)
            self.popMsg = "获取中..."
            bongSDK.fetchbongActivityWithDate(NSDate(), isDetailed: indexPath.row == 1)
        }
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.section == 0 && indexPath.row == 1 {
            self.resultTextView.text = self.resultText
        }
    }
    
    // MARK: bongSDKDelegate
    func bongSDKDidFetchBriefActivity(activity:BriefActivity, error:NSError!) {
        self.popCtlr!.dismissViewControllerAnimated(true) {
            if (error != nil) {
                self.resultText = error.description;
            } else {
                self.resultText = NSString(format: "摘要数据：\n\n%@", activity.description) as String
            }
        }
        self.resultTextView.text = self.resultText
    }

    func bongSDKDidFetchDetailedDayActivity(activity:DetailedDayActivity, error:NSError!) {
        self.popCtlr!.dismissViewControllerAnimated(true) {
            if (error != nil) {
                self.resultText = error.description;
            } else {
                self.resultText = NSString(format: "详细数据：\n\n%@", activity.description) as String
            }
        }
        self.resultTextView.text = self.resultText
    }
    
    
    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if segue.identifier == "Popover" {
            self.popCtlr = segue.destinationViewController as? ActivityIndicatorViewController
            let naviRect = self.navigationController!.navigationBar.frame
            self.popCtlr!.popoverPresentationController!.sourceRect = CGRectMake(self.view.frame.width / 2 , self.view.frame.height/2 - naviRect.height, 1, 1)
            self.popCtlr!.popoverPresentationController!.delegate = self
            self.popCtlr!.message = self.popMsg
        }
        
    }
    
    // MARK: - UIPopoverPresentationControllerDelegate
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return !scanning
    }


}
<file_sep>//
//  AuthorizeTableViewController.swift
//  BongMe
//
//  Created by Ryan on 1/16/16.
//  Copyright © 2016 J.A.V.I.S. All rights reserved.
//

import UIKit

class AuthorizeTableViewController: UITableViewController, bongSDKDelegate, UIPopoverPresentationControllerDelegate {
    
//    @IBOutlet weak var actIndicator: UIActivityIndicatorView!
    private var fetchingBongID = false
    private var popCtlr:ActivityIndicatorViewController?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        bongSDK.addDelegate(self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewWillDisappear(animated: Bool) {
        bongSDK.removeDelegate(self)
    }
    
    // MARK: - bongSDKDelegate
    func bongSDKDidAutherize(errorCode: bongSDKErrorCode) {
        NSLog("bongSDKDidAuthorize errorCode: @%", errorCode.rawValue)
    }
    
    func bongSDKDidFetchbongID(errorCode: bongSDKErrorCode) {
        fetchingBongID = false
        popCtlr?.dismissViewControllerAnimated(true) {
            let alert = UIAlertController(title: (errorCode == bongSDKErrorCode.None) ? "获取成功" : "获取失败", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
            let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Cancel, handler: nil)
            alert.addAction(okAction)
            self.presentViewController(alert, animated: true, completion: nil)
        }
        NSLog("bongSDKDidFetchbongID errorCode: %d", errorCode.rawValue);
        return
    }
    

    // MARK: - UITableViewDelegate
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.row == 0 {
            if bongSDK.isAuthorizationValid() {
                let alert = UIAlertController(title: "已授权", message: "再次授权前, 先清空已有授权信息", preferredStyle: UIAlertControllerStyle.Alert)
                let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Cancel, handler: nil)
                alert.addAction(okAction)
                presentViewController(alert, animated: true, completion: nil)
                return
            }
            bongSDK.requestAuthorization()
            return
        }
        if indexPath.row == 1 {
            if bongSDK.isbongIDValid() {
                let alert = UIAlertController(title: "已获取 bong ID", message: "再次授权前, 先清空已有 ID 信息", preferredStyle: UIAlertControllerStyle.Alert)
                let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Cancel, handler: nil)
                alert.addAction(okAction)
                presentViewController(alert, animated: true, completion: nil)
                return
            }
            fetchingBongID = true
            performSegueWithIdentifier("Popover", sender: self)
            bongSDK.requestbongID()
            return
        }
        
        bongSDK.clearAuthorization()
        bongSDK.clearbongID()
        let alert = UIAlertController(title: "已清空", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
        let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Cancel, handler: nil)
        alert.addAction(okAction)
        presentViewController(alert, animated: true, completion: nil)

        
    }
    
        
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if segue.identifier == "Popover" {
            self.popCtlr = segue.destinationViewController as? ActivityIndicatorViewController
            let naviRect = self.navigationController!.navigationBar.frame
            self.popCtlr!.popoverPresentationController!.sourceRect = CGRectMake(self.view.frame.width / 2 , self.view.frame.height/2 - naviRect.height, 1, 1)
            self.popCtlr!.popoverPresentationController!.delegate = self
            self.popCtlr!.message = "获取中..."
        }
        
    }
    
    // MARK: - UIPopoverPresentationControllerDelegate
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return !fetchingBongID
    }


}
<file_sep>//
//  BongIITableViewController.swift
//  BongMe
//
//  Created by Ryan on 1/16/16.
//  Copyright © 2016 J.A.V.I.S. All rights reserved.
//

import UIKit

class BongIITableViewController: UITableViewController, bongSDKDelegate,UIPopoverPresentationControllerDelegate {
    private var scanning:Bool = false
    private var syncWhenScanned:Bool = false
    
    @IBOutlet weak var switcher: UISwitch!
    
    
    private var popCtlr:ActivityIndicatorViewController?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        bongSDK.addDelegate(self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewWillDisappear(animated: Bool) {
        bongSDK.removeDelegate(self)
        bongSDK.stop()
    }
    

    @IBAction func switchValueChanged(sender: UISwitch) {
        self.syncWhenScanned = sender.on
    }
    
    
    
    
    // MARK: - UITableViewDelegate


    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.row == 0 {
            if self.scanning {
                bongSDK.stop()
            } else {
                bongSDK.run()
            }
        }
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath == 0 {
            cell.textLabel!.text = self.scanning ? "停止扫描" : "开始扫描"
            return
        }
        cell.textLabel!.text = "扫描到即同步"
        cell.textLabel!.backgroundColor = UIColor.clearColor()
        self.switcher.setOn(self.syncWhenScanned, animated: true)
    }
    
    // MARK: - bongSDKDelegate
    
    func bongSDKDidRun(code: bongSDKErrorCode) {
        NSLog("bongSDKDidRun code: %d", code.rawValue)
        if code == bongSDKErrorCode.None {
            self.scanning = true
            self.tableView.reloadData()
        }
    }
    
    func bongSDKDidStop(code: bongSDKErrorCode) {
        NSLog("bongSDKDidStop code: %d", code.rawValue)
        if code == bongSDKErrorCode.None {
            self.scanning = false
            self.tableView.reloadData()
        }
    }
    
    func bongSDKDidScanbongIIWithPressYesKey(touchType: YESKeyTouchType) {
        NSLog("bongSDKDidScanbongIIWithPressYesKey touchType: %d", touchType.rawValue)
        if self.syncWhenScanned {
            let now = NSDate()
            bongSDK.fetchbongDataWithStartDate(now.dateByAddingTimeInterval(-60*30), endDate: now)
            performSegueWithIdentifier("Popover", sender: self)
        }
    }
    
    func bongSDKDataFetchStatusDidChange(status: bongSDKDataFetchStatus, error: NSError!) {
        if (error != nil) {
            NSLog("bongSDKDataFetchStatusDidChange error: %@", error)
            popCtlr?.dismissViewControllerAnimated(true,completion: nil)
            return
        }
        NSLog("bongSDKDataFetchStatusDidChange status %d", status.rawValue)
        if status == bongSDKDataFetchStatus.Succeed || status == bongSDKDataFetchStatus.FetchNothing {
            popCtlr?.dismissViewControllerAnimated(true,completion: nil)
        }
    }

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if segue.identifier == "Popover" {
            self.popCtlr = segue.destinationViewController as? ActivityIndicatorViewController
            let naviRect = self.navigationController!.navigationBar.frame
            self.popCtlr!.popoverPresentationController!.sourceRect = CGRectMake(self.view.frame.width / 2 , self.view.frame.height/2 - naviRect.height, 1, 1)
            self.popCtlr!.popoverPresentationController!.delegate = self
            self.popCtlr!.message = "同步中..."
        }
        
    }
    
    // MARK: - UIPopoverPresentationControllerDelegate
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return !scanning
    }

}
<file_sep># BongMe

> Start Date: @20160114

## Framework Installation

### CocoaPods

> https://cocoapods.org

This project use CocoaPods to handle framework dependancy.

Here is a tutorial to using CocoaPods wit Swift:

> http://www.raywenderlich.com/97014

#### OAuthSwift

> https://github.com/OAuthSwift/OAuthSwift

``` ruby
# Podfile
platform :ios, '8.0'
use_frameworks!
pod "OAuthSwift", "~> 0.5.0"
```

#### JSONModel

> https://github.com/icanzilb/JSONModel
> 
> NB: **Swift works in a different way under the hood than Objective-C. Therefore I can't find a way to re-create JSONModel in Swift. JSONModel in Objective-C works in Swift apps through CocoaPods or as an imported Objective-C library.**

``` ruby
# Podfile
pod 'JSONModel'
```

#### bongSDK

> https://github.com/Ginshell/bongSDK-iOS

1. `TARGET`->`BongMe`->`General`->`Linked Frameworks and Libraries`  添加`bongSDK.framework`
   
2. 添加`BongMe-Bridging-Header.h`
   
3. `TARGET`->`BongMe`->`Build Settings`->`Other Linker Flags` 加入`$(OTHER_LDFLAGS) -ObjC`
   
4. `TARGET`->`BongMe`->`Build Settings`->`Objective-C Bridging Header` 添加刚才创建的头文件`BongMe/BongMe-Bridging-Header.h`, 注意文件路径.
   
5. 测试. 在`ViewController`的`viewDidLoad()`中添加
   
   ``` swift
   NSLog("bongSDK version: %@", bongSDK.version())	
   ```
   
   运行APP, 查看控制台输出:
   
   ``` 
   $ 2016-01-16 11:53:02.617 BongMe[16494:7577534] bongSDK version: 2.0.2
   ```<file_sep>//
//  ActivityIndicatorViewController.swift
//  BongMe
//
//  Created by Ryan on 1/17/16.
//  Copyright © 2016 J.A.V.I.S. All rights reserved.
//

import UIKit

class ActivityIndicatorViewController: UIViewController {

    @IBOutlet weak var lblMessage: UILabel!
    @IBOutlet weak var actIndicator: UIActivityIndicatorView!
    
    var message:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.lblMessage.text = self.message
        self.lblMessage.sizeToFit()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        self.actIndicator.startAnimating()
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(true)
        self.actIndicator.stopAnimating()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    deinit {
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
<file_sep>//
//  MainTableViewController.swift
//  BongMe
//
//  Created by Ryan on 1/15/16.
//  Copyright © 2016 J.A.V.I.S. All rights reserved.
//

import UIKit


class MainTableViewController: UITableViewController, bongSDKDelegate {
    

    @IBOutlet weak var lblAuthorization: UILabel!
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        NSLog("bongSDK version: %@", bongSDK.version())
        // bongSDK 配置，此处替换成自己的配置
        bongSDK.setAppID("1419735044202", appSecret: "558860f5ba4546ddb31eafeee11dc8f4", appKey: "7ae31974a95fec07ad3d047c075b11745d8ce989")
        // 设置开发者环境，即服务器为测试环境
        bongSDK.enableDebugMode(true)
        bongSDK.addDelegate(self)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        lblAuthorization.text = bongSDK.isAuthorizationValid() ? (bongSDK.isbongIDValid() ? "已授权并获取 bong ID" : "已授权") : "授权"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: - UITableViewDelegate
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    //MARK: - bongSDKDelegate
    func bongSDKDidSwitchXNote(error: NSError!) {
        NSLog("error: %@", error)
    }
    
    func bongSDKDidUpdateXNote(type: bongSDKXNoteType, error: NSError!) {
        NSLog("type: %td errorL %@", type.rawValue, error)
    }
    
    //MARK: - Naigation
    override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
        if identifier == "Authorize" {
            return true
        }
        
        if !bongSDK.isAuthorizationValid() {
            let alert = UIAlertController(title: "无效授权信息", message: "请进行授权操作", preferredStyle: UIAlertControllerStyle.Alert)
            let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Cancel, handler: nil)
            alert.addAction(okAction)
            presentViewController(alert, animated: true, completion: nil)
            return false
        }
        
        if identifier == "bongII" || identifier == "bongX" {
            if !bongSDK.isbongIDValid() {
                let alert = UIAlertController(title: "无效 bong ID", message: "请先获取 bong ID", preferredStyle: UIAlertControllerStyle.Alert)
                let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Cancel, handler: nil)
                alert.addAction(okAction)
                presentViewController(alert, animated: true, completion: nil)
                return false
            }
            let productID = bongSDK.bongProductID()
            if (identifier == "bongII" && productID != bongSDKProductID.IDTwo) || (identifier == "bongX" && (productID != bongSDKProductID.IDX && productID != bongSDKProductID.IDXX)) {
                let alert = UIAlertController(title: "不兼容设备", message: "请先绑定正确的设备", preferredStyle: UIAlertControllerStyle.Alert)
                let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Cancel, handler: nil)
                alert.addAction(okAction)
                presentViewController(alert, animated: true, completion: nil)
                return false
            }
        }
        return true
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if ((sender?.isKindOfClass(UITableViewCell)) != nil) {
            let cell = sender as! UITableViewCell
            self.tableView.deselectRowAtIndexPath(self.tableView.indexPathForCell(cell)!, animated: true)
        }
    }
    
    


}

