TriceKit 0.5.7

TriceKit 0.5.7

测试已测试
语言语言 Obj-CObjective C
许可 自定义
发布最后发布2015年12月

Jake Bellamy 维护。



TriceKit 0.5.7

  • 作者
  • Jake

这是 TriceKit iOS SDK,可以与 TriceKit 管理系统 一起提供愉悦的体验。

安装

安装 SDK 最简单的方法是使用 CocoaPods

在您的 Podfile

pod 'TriceKit'

在您的 AppDelegate.m 中在应用启动时设置由 TriceKit CMS 提供的 API 密钥和用户名

[[TriceSettings sharedInstance] setApiKey:<#API_KEY#>];
[[TriceSettings sharedInstance] setUsername:<#USERNAME#>];

此外,您可以通过更改 TriceSettings 中提供的设置来进一步配置 TriceKit SDK

[[TriceSettings sharedInstance] setDebugEnabled:YES]; // Log messages out to console.
[[TriceSettings sharedInstance] setUseProductionServer:NO]; // Use the testing server.

区域、触发器、动作

@property (nonatomic, strong) TriceTriggerManager *triggerManager;

self.triggerManager = [TriceTriggerManager new];
[self.triggerManager startMonitoringZones];

要允许 TriceKit 处理由它生成的本地通知,请将以下内容添加到您的 AppDelegate.m 中

[TriceAppDelegateProxy handleTriceKitLocalNotifications];

确保您在应用程序的 info.plist 中有适当的 NSLocationUsage 键!如果您没有 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription,那么 Apple 将静默拒绝使用位置服务的请求,TriceKit 将无法工作。

示例任务

以编程方式创建新的信标区域、触发器和动作

这将在进入区域时每10秒触发一个本地通知。如果 TriceKit 正在处理自己的本地通知,则在接收到通知时将显示警报。

TriceBeacon *beacon = [TriceBeacon beaconWithMajor:<#(CLBeaconMajorValue)#> minor:<#(CLBeaconMinorValue)#> proximityUuid:<#(NSUUID *)#>];
TriceZone *zone = [TriceBeaconZone beaconZoneWithBeacon:beacon proximity:CLProximityImmediate];
TriceTrigger *trigger = [TriceTrigger triggerWithEvent:TriceTriggerEventEnter frequency:10 limit:0];

[trigger addAction:[TriceAction pushNotificationAction:^(UILocalNotification *notification) {
    notification.alertBody = @"Entered!";
}]];

[zone addTrigger:trigger];
[self.triggerManager addZone:zone];
以编程方式创建新的地理坐标区域

省略创建触发器、动作和附件。有关此示例,请参阅创建信标区域。

NSArray *coordinates = @[
    [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(-41.290923539344, 174.77681817021)],
    [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(-41.291038412930, 174.77658750024)],
    [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(-41.291215761226, 174.77654726710)],
    [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(-41.291294358613, 174.77689058986)],
    [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(-41.291294358613, 174.77718295064)],
    [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(-41.291104918598, 174.77717490401)],
    [NSValue valueWithMKCoordinate:CLLocationCoordinate2DMake(-41.290988029803, 174.77703811135)]
];
TriceZone *geoPolygonZone = [TriceGeoPolygonZone geoPolygonZoneWithCoordinates:coordinates];
以编程方式创建新的地理半径区域

省略创建触发器、动作和附件。有关此示例,请参阅创建信标区域。

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(-41.290384160865536, 174.77760503743434);
TriceZone *geoRadiusZone = [TriceGeoRadiusZone geoRadiusZoneWithCoordinate:coordinate radius:20];
将自定义动作添加到在 CMS 中创建的 zone+trigger

这假设您已在 CMS 中设置了区域和触发器。

// Using the unique identifier ensures we are adding this action to only one specific trigger.
TriceObjectDescriptor *zoneDescriptor = [TriceObjectDescriptor descriptorWithUid:<#(NSString *)#>];
TriceObjectDescriptor *triggerDescriptor = [TriceObjectDescriptor descriptorWithUid:<#(NSString *)#>];

TriceAction *customAction = [TriceAction action:^{
    // This block gets executed when the trigger matching the description fires.
    NSLog(@"Wooooo!");
}];

[self.triggerManager addAction:customAction toTrigger:triggerDescriptor zone:zoneDescriptor];
将自定义动作添加到多个区域和触发器

在此示例中,当有人进入任何会议室时,我们都会显示一个警告,当他们离开时显示不同的警告。假设在 CMS 中区域的名称分别是 "会议室 1"、"会议室 2"、"会议室 3" 等...

// Describe all zones in the meeting rooms.
TriceObjectDescriptor *meetingRoomZones = [TriceObjectDescriptor descriptorWithKey:@"name" passingTest:^BOOL(NSString *name) {
    return [name hasPrefix:@"Meeting room"];
}];
// All enter triggers.
TriceObjectDescriptor *enterTriggerDescriptor = [TriceObjectDescriptor descriptorWithKey:@"event" passingTest:^BOOL(NSNumber *eventValue) {
    TriceTriggerEvent event = (TriceTriggerEvent)[eventValue integerValue];
    return (event == TriceTriggerEventEnter);
}];
// All exit triggers.
TriceObjectDescriptor *exitTriggerDescriptor = [TriceObjectDescriptor descriptorWithKey:@"event" passingTest:^BOOL(NSNumber *eventValue) {
    TriceTriggerEvent event = (TriceTriggerEvent)[eventValue integerValue];
    return (event == TriceTriggerEventExit);
}];

TriceAction *showEnterAlert = [TriceAction action:^{
    [TriceAlert displayOkAlertWithTitle:@"Welcome to the meeting!" message:nil okAction:nil];
}];

TriceAction *showExitAlert = [TriceAction action:^{
    [TriceAlert displayYesNoAlertWithTitle:@"Was this meeting productive for you?" message:nil yesAction:^{
        [TriceAlert displayOkAlertWithTitle:@"Good, we hope to see you next time!" message:nil okAction:nil];
    } noAction:^{
        [TriceAlert displayOkAlertWithTitle:@"Lets work on improving that." message:@"We will collect this response to improve your experience." okAction:^{
            // Do further actions to collect response, send request to server, perhaps display a new view controller...
        }];
    }];
}];

[self.triggerManager addAction:showEnterAlert toTrigger:enterTriggerDescriptor zone:meetingRoomZones];
[self.triggerManager addAction:showExitAlert toTrigger:exitTriggerDescriptor zone:meetingRoomZones];
观察事件

TriceKit发布了多个NSNotification对象,可以免费观察并做出反应。

当TriceZone对象的状态属性改变时,会发布一个TriceZoneStateDidChangeNotification。

TriceTrigger对象在执行其动作之前和之后,会发布一个TriceTriggerWillFireNotification和一个TriceTriggerDidFireNotification。在WillFire通知期间,您能够取消触发的触发序列。

- (void)addNotificationObservers
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(zoneDidChangeState:) name:TriceZoneStateDidChangeNotification object:nil];
    [center addObserver:self selector:@selector(triggerWillFire:) name:TriceTriggerWillFireNotification object:nil];
    [center addObserver:self selector:@selector(triggerDidFire:) name:TriceTriggerDidFireNotification object:nil];
}

- (void)zoneDidChangeState:(NSNotification *)notification
{
    // Zone was entered or exited, relevant triggers may fire.

    TriceZone *zone = notification.object;

    if (zone.state == TriceZoneStateEntered || zone.state == TriceZoneStateDwelling) { // We are now inside!
        self.view.backgroundColor = [UIColor greenColor];
    } else { // We are now outside!
        self.view.backgroundColor = [UIColor redColor];
    }
}

- (void)triggerWillFire:(NSNotification *)notification
{
    TriceTrigger *trigger = notification.object;
    if (/* we don't actually want this trigger to fire yet */ YES) {
        [trigger cancelFiring];
    }
}

- (void)triggerDidFire:(NSNotification *)notification
{
    TriceTrigger *trigger = notification.object;
    NSLog(@"A trigger fired! %@", trigger);
}
暂停或停止通知

您可以通过注册TriceTriggerWillFireNotification通知并取消所有触发事件来临时暂停通知。请参见上面的示例了解如何操作。

如果您想要在更长时间内停止,建议您通过调用以下方法来停止TriceTriggerManager的监视:

[self.triggerManager stopMonitoringZones];
离线支持

最近的区域集在无需在线的情况下,可以在应用程序启动之间保存。

self.triggerManager.allowEncodingZonesToDisk = YES;