Skip to content

Commit d4b6c8b

Browse files
committed
[dev app] Add custom events examples
* Add button at the bottom of Dev App called "Track Custom Events" * Add example custom events in Objective-C and Swift
1 parent 713bdf2 commit d4b6c8b

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/Base.lproj/Main.storyboard

+12-3
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@
501501
<fontDescription key="fontDescription" type="system" pointSize="14"/>
502502
<textInputTraits key="textInputTraits"/>
503503
</textField>
504-
<button opaque="NO" contentMode="scaleToFill" selected="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="xRQ-ep-yvV" userLabel="Clear All Button">
505-
<rect key="frame" x="140" y="1762" width="148" height="30"/>
504+
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" selected="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="xRQ-ep-yvV" userLabel="Clear All Button">
505+
<rect key="frame" x="115" y="1763" width="148" height="30"/>
506506
<color key="tintColor" red="1" green="0.1367101157" blue="0.01701983743" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
507507
<state key="normal" title="Clear All Notifications"/>
508508
<connections>
@@ -528,6 +528,15 @@
528528
<action selector="requireConsent:" destination="BYZ-38-t0r" eventType="touchUpInside" id="vxn-ST-TBu"/>
529529
</connections>
530530
</button>
531+
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" selected="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Aea-nN-qtb" userLabel="Custom Events Button">
532+
<rect key="frame" x="114" y="1808" width="145" height="30"/>
533+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
534+
<color key="tintColor" red="1" green="0.1367101157" blue="0.01701983743" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
535+
<state key="normal" title="Track Custom Events"/>
536+
<connections>
537+
<action selector="trackCustomEvents:" destination="BYZ-38-t0r" eventType="touchUpInside" id="4Fa-IJ-LJT"/>
538+
</connections>
539+
</button>
531540
</subviews>
532541
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
533542
<constraints>
@@ -760,7 +769,7 @@
760769
<color white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
761770
</systemColor>
762771
<systemColor name="systemGreenColor">
763-
<color red="0.20392156859999999" green="0.78039215689999997" blue="0.34901960780000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
772+
<color red="0.20392156862745098" green="0.7803921568627451" blue="0.34901960784313724" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
764773
</systemColor>
765774
</resources>
766775
</document>

iOS_SDK/OneSignalDevApp/OneSignalDevApp/SwiftTest.swift

+40
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,49 @@
2828
import Foundation
2929
import OneSignalFramework
3030

31+
@objc
3132
class SwiftTest: NSObject {
3233
func testSwiftUserModel() {
3334
let token1 = OneSignal.User.pushSubscription.token
3435
let token = OneSignal.User.pushSubscription.token
3536
}
37+
38+
/**
39+
Track multiple events with different properties.
40+
Properties must pass `JSONSerialization.isValidJSONObject` to be accepted.
41+
*/
42+
@objc
43+
static func trackCustomEvents() {
44+
print("Dev App: track an event with nil properties")
45+
OneSignal.User.trackEvent(name: "null properties", properties: nil)
46+
47+
print("Dev App: track an event with empty properties")
48+
OneSignal.User.trackEvent(name: "empty properties", properties: [:])
49+
50+
let formatter = DateFormatter()
51+
formatter.dateStyle = .short
52+
53+
let mixedTypes = [
54+
"string": "somestring",
55+
"number": 5,
56+
"bool": false,
57+
"dateStr": formatter.string(from: Date())
58+
] as [String: Any]
59+
60+
let nestedDict = [
61+
"someDict": mixedTypes,
62+
"anotherDict": [
63+
"foo": "bar",
64+
"booleanVal": true,
65+
"float": Float("3.14")!
66+
]
67+
]
68+
let invalidProperties = ["date": Date()]
69+
70+
print("Dev App: track an event with a valid nested dictionary")
71+
OneSignal.User.trackEvent(name: "nested dictionary", properties: nestedDict)
72+
73+
print("Dev App: track an event with invalid dictionary types")
74+
OneSignal.User.trackEvent(name: "invalid dictionary", properties: invalidProperties)
75+
}
3676
}

iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.m

+11
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,15 @@ - (IBAction)dontRequireConsent:(id)sender {
277277
[OneSignal setConsentRequired:false];
278278
}
279279

280+
- (IBAction)trackCustomEvents:(id)sender {
281+
NSLog(@"Dev App: adding custom events");
282+
[OneSignal.User trackEventWithName:@"simple event" properties:@{@"foobarbaz": @"foobarbaz"}];
283+
NSMutableDictionary *dict = [NSMutableDictionary new];
284+
dict[@"dict"] = @{@"abc" : @"def"};
285+
dict[@"false"] = false;
286+
dict[@"int"] = @99;
287+
[OneSignal.User trackEventWithName:@"complex event" properties:dict];
288+
[SwiftTest trackCustomEvents];
289+
}
290+
280291
@end

0 commit comments

Comments
 (0)