Skip to content

Commit 0ac5416

Browse files
authored
Add ObjC destination compatibility (#209)
* ObjC Destination Support * ObjC Destination support additions & tests * Removed unused line * Fixed xcodeproj
1 parent 512c6d9 commit 0ac5416

File tree

11 files changed

+259
-126
lines changed

11 files changed

+259
-126
lines changed

Examples/apps/ObjCExample/ObjCExample.xcodeproj/project.pbxproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
460FF309299AAEEC00635FF9 /* Segment in Frameworks */ = {isa = PBXBuildFile; productRef = 460FF308299AAEEC00635FF9 /* Segment */; };
11+
46262AB229CB731B005C68FF /* SegmentMixpanel in Frameworks */ = {isa = PBXBuildFile; productRef = 46262AB129CB731B005C68FF /* SegmentMixpanel */; };
1112
464247BC26C6D7A700CAD301 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 464247BB26C6D7A700CAD301 /* AppDelegate.m */; };
1213
464247BF26C6D7A700CAD301 /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 464247BE26C6D7A700CAD301 /* SceneDelegate.m */; };
1314
464247C226C6D7A700CAD301 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 464247C126C6D7A700CAD301 /* ViewController.m */; };
@@ -37,6 +38,7 @@
3738
isa = PBXFrameworksBuildPhase;
3839
buildActionMask = 2147483647;
3940
files = (
41+
46262AB229CB731B005C68FF /* SegmentMixpanel in Frameworks */,
4042
460FF309299AAEEC00635FF9 /* Segment in Frameworks */,
4143
);
4244
runOnlyForDeploymentPostprocessing = 0;
@@ -104,6 +106,7 @@
104106
name = ObjCExample;
105107
packageProductDependencies = (
106108
460FF308299AAEEC00635FF9 /* Segment */,
109+
46262AB129CB731B005C68FF /* SegmentMixpanel */,
107110
);
108111
productName = ObjCExample;
109112
productReference = 464247B726C6D7A700CAD301 /* ObjCExample.app */;
@@ -131,6 +134,9 @@
131134
Base,
132135
);
133136
mainGroup = 464247AE26C6D7A700CAD301;
137+
packageReferences = (
138+
469891A529CB72D80021B552 /* XCRemoteSwiftPackageReference "analytics-swift-mixpanel" */,
139+
);
134140
productRefGroup = 464247B826C6D7A700CAD301 /* Products */;
135141
projectDirPath = "";
136142
projectRoot = "";
@@ -356,11 +362,27 @@
356362
};
357363
/* End XCConfigurationList section */
358364

365+
/* Begin XCRemoteSwiftPackageReference section */
366+
469891A529CB72D80021B552 /* XCRemoteSwiftPackageReference "analytics-swift-mixpanel" */ = {
367+
isa = XCRemoteSwiftPackageReference;
368+
repositoryURL = "https://github.com/segment-integrations/analytics-swift-mixpanel";
369+
requirement = {
370+
kind = upToNextMajorVersion;
371+
minimumVersion = 1.0.0;
372+
};
373+
};
374+
/* End XCRemoteSwiftPackageReference section */
375+
359376
/* Begin XCSwiftPackageProductDependency section */
360377
460FF308299AAEEC00635FF9 /* Segment */ = {
361378
isa = XCSwiftPackageProductDependency;
362379
productName = Segment;
363380
};
381+
46262AB129CB731B005C68FF /* SegmentMixpanel */ = {
382+
isa = XCSwiftPackageProductDependency;
383+
package = 469891A529CB72D80021B552 /* XCRemoteSwiftPackageReference "analytics-swift-mixpanel" */;
384+
productName = SegmentMixpanel;
385+
};
364386
/* End XCSwiftPackageProductDependency section */
365387
};
366388
rootObject = 464247AF26C6D7A700CAD301 /* Project object */;

Examples/apps/ObjCExample/ObjCExample/AppDelegate.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import "AppDelegate.h"
99

1010
@import Segment;
11+
@import SegmentMixpanel;
1112

1213
@interface AppDelegate ()
1314

@@ -40,6 +41,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4041
return event;
4142
}];
4243

44+
//[self.analytics addDestination:[[SEGMixpanelDestination alloc] init]];
45+
46+
4347
dispatch_async(dispatch_get_main_queue(), ^{
4448
[self.analytics track:@"booya"];
4549
});

Segment.xcodeproj/project.pbxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@
3737
4663C729267A799100ADDD1A /* QueueTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4663C728267A799100ADDD1A /* QueueTimer.swift */; };
3838
466EC2CE28FB7D5D001B384E /* OutputFileStream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466EC2CD28FB7D5D001B384E /* OutputFileStream.swift */; };
3939
4697A238290341EF00FAE14F /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4697A237290341EF00FAE14F /* Errors.swift */; };
40-
469891A329C11A890021B552 /* ObjCPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 469891A229C11A890021B552 /* ObjCPlugin.swift */; };
4140
46A018C225E5857D00F9CCD8 /* Context.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018C125E5857D00F9CCD8 /* Context.swift */; };
4241
46A018D425E6C9C200F9CCD8 /* LinuxUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018D325E6C9C200F9CCD8 /* LinuxUtils.swift */; };
4342
46A018DA25E97FDF00F9CCD8 /* AppleUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018D925E97FDF00F9CCD8 /* AppleUtils.swift */; };
4443
46A018EE25E9A74F00F9CCD8 /* VendorSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018ED25E9A74F00F9CCD8 /* VendorSystem.swift */; };
44+
46A9439F29CE162A00D65DC3 /* ObjCDestinationSupport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A9439D29CE162A00D65DC3 /* ObjCDestinationSupport.swift */; };
45+
46A943A029CE162A00D65DC3 /* ObjCPluginSupport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A9439E29CE162A00D65DC3 /* ObjCPluginSupport.swift */; };
4546
46B1AC6927346D3D00846DE8 /* StressTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46B1AC6827346D3D00846DE8 /* StressTests.swift */; };
4647
46E382E72654429A00BA2502 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E382E62654429A00BA2502 /* Utils.swift */; };
4748
46F7485D26C718710042798E /* ObjCAnalytics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F7485B26C718710042798E /* ObjCAnalytics.swift */; };
@@ -125,11 +126,12 @@
125126
4663C728267A799100ADDD1A /* QueueTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueTimer.swift; sourceTree = "<group>"; };
126127
466EC2CD28FB7D5D001B384E /* OutputFileStream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OutputFileStream.swift; sourceTree = "<group>"; };
127128
4697A237290341EF00FAE14F /* Errors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Errors.swift; sourceTree = "<group>"; };
128-
469891A229C11A890021B552 /* ObjCPlugin.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjCPlugin.swift; sourceTree = "<group>"; };
129129
46A018C125E5857D00F9CCD8 /* Context.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Context.swift; sourceTree = "<group>"; };
130130
46A018D325E6C9C200F9CCD8 /* LinuxUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxUtils.swift; sourceTree = "<group>"; };
131131
46A018D925E97FDF00F9CCD8 /* AppleUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleUtils.swift; sourceTree = "<group>"; };
132132
46A018ED25E9A74F00F9CCD8 /* VendorSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VendorSystem.swift; sourceTree = "<group>"; };
133+
46A9439D29CE162A00D65DC3 /* ObjCDestinationSupport.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjCDestinationSupport.swift; sourceTree = "<group>"; };
134+
46A9439E29CE162A00D65DC3 /* ObjCPluginSupport.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjCPluginSupport.swift; sourceTree = "<group>"; };
133135
46B1AC6827346D3D00846DE8 /* StressTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StressTests.swift; sourceTree = "<group>"; };
134136
46D98E3D26D6FEF300E7A86A /* FlurryDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlurryDestination.swift; sourceTree = "<group>"; };
135137
46D98E3E26D6FEF300E7A86A /* AdjustDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdjustDestination.swift; sourceTree = "<group>"; };
@@ -222,7 +224,8 @@
222224
children = (
223225
46F7485B26C718710042798E /* ObjCAnalytics.swift */,
224226
46F7485C26C718710042798E /* ObjCConfiguration.swift */,
225-
469891A229C11A890021B552 /* ObjCPlugin.swift */,
227+
46A9439D29CE162A00D65DC3 /* ObjCDestinationSupport.swift */,
228+
46A9439E29CE162A00D65DC3 /* ObjCPluginSupport.swift */,
226229
);
227230
path = ObjC;
228231
sourceTree = "<group>";
@@ -528,6 +531,7 @@
528531
46022764261E64A800A9E913 /* iOSLifecycleEvents.swift in Sources */,
529532
96A9624E2810C6B80011DE54 /* macOSLifecycleEvents.swift in Sources */,
530533
460FF30B29BA525900635FF9 /* Logging.swift in Sources */,
534+
46A9439F29CE162A00D65DC3 /* ObjCDestinationSupport.swift in Sources */,
531535
4621080C2605332D00EBC4A8 /* KeyPath.swift in Sources */,
532536
A31A16262576B6F200C9CDDF /* Timeline.swift in Sources */,
533537
96C33AB1258961F500F3D538 /* Settings.swift in Sources */,
@@ -543,11 +547,11 @@
543547
46210836260BBEE400EBC4A8 /* DeviceToken.swift in Sources */,
544548
9692724E25A4E5B7009B5298 /* Startup.swift in Sources */,
545549
4663C729267A799100ADDD1A /* QueueTimer.swift in Sources */,
546-
469891A329C11A890021B552 /* ObjCPlugin.swift in Sources */,
547550
46FE4C9C25A3F41C003A7362 /* LinuxLifecycleMonitor.swift in Sources */,
548551
460227422612987300A9E913 /* watchOSLifecycleEvents.swift in Sources */,
549552
46F7485E26C718710042798E /* ObjCConfiguration.swift in Sources */,
550553
759D6CD127B48ABB00AB900A /* DestinationMetadataPlugin.swift in Sources */,
554+
46A943A029CE162A00D65DC3 /* ObjCPluginSupport.swift in Sources */,
551555
A31A162F2576B73F00C9CDDF /* State.swift in Sources */,
552556
9692726825A583A6009B5298 /* SegmentDestination.swift in Sources */,
553557
4602276C261E7BF900A9E913 /* iOSDelegation.swift in Sources */,

Sources/Segment/Configuration.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public class Configuration {
3131

3232
public init(writeKey: String) {
3333
self.values = Values(writeKey: writeKey)
34+
// enable segment destination by default
35+
var settings = Settings(writeKey: writeKey)
36+
settings.integrations = try? JSON([
37+
"Segment.io": true
38+
])
39+
40+
self.defaultSettings(settings)
3441
}
3542
}
3643

Sources/Segment/ObjC/ObjCAnalytics.swift

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,23 @@ extension ObjCAnalytics {
7979

8080
/// Track a screen change with a title, category and other properties.
8181
/// - Parameters:
82-
/// - screenTitle: The title of the screen being tracked.
82+
/// - title: The title of the screen being tracked.
8383
@objc(screen:)
8484
public func screen(title: String) {
8585
screen(title: title, category: nil, properties: nil)
8686
}
8787

8888
/// Track a screen change with a title, category and other properties.
8989
/// - Parameters:
90-
/// - screenTitle: The title of the screen being tracked.
90+
/// - title: The title of the screen being tracked.
9191
/// - category: A category to the type of screen if it applies.
9292
@objc(screen:category:)
9393
public func screen(title: String, category: String?) {
9494
analytics.screen(title: title, category: category, properties: nil)
9595
}
9696
/// Track a screen change with a title, category and other properties.
9797
/// - Parameters:
98-
/// - screenTitle: The title of the screen being tracked.
98+
/// - title: The title of the screen being tracked.
9999
/// - category: A category to the type of screen if it applies.
100100
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
101101
@objc(screen:category:properties:)
@@ -129,62 +129,6 @@ extension ObjCAnalytics {
129129
}
130130
}
131131

132-
// MARK: - ObjC Plugin Functionality
133-
134-
@objc
135-
extension ObjCAnalytics {
136-
/// This method allows you to add middleware to an Analytics instance, similar to Analytics-iOS.
137-
/// However, it is **strongly encouraged** that Enrichments/Plugins/Middlewares be written in swift
138-
/// to avoid the overhead of type conversion back and forth. This exists solely for compatibility
139-
/// purposes.
140-
///
141-
/// Example:
142-
/// [self.analytics addSourceMiddleware:^NSDictionary<NSString *,id> * _Nullable(NSDictionary<NSString *,id> * _Nullable event) {
143-
/// // drop all events named booya
144-
/// NSString *eventType = event[@"type"];
145-
/// if ([eventType isEqualToString:@"track"]) {
146-
/// NSString *eventName = event[@"event"];
147-
/// if ([eventName isEqualToString:@"booya"]) {
148-
/// return nil;
149-
/// }
150-
/// }
151-
/// return event;
152-
/// }];
153-
///
154-
/// - Parameter middleware: The middleware to execute at the source level.
155-
@objc(addSourceMiddleware:)
156-
public func addSourceMiddleware(middleware: @escaping ((_ event: [String: Any]?) -> [String: Any]?)) {
157-
analytics.add(plugin: ObjCShimPlugin(middleware: middleware))
158-
}
159-
160-
/// This method allows you to add middleware to an Analytics instance, similar to Analytics-iOS.
161-
/// However, it is **strongly encouraged** that Enrichments/Plugins/Middlewares be written in swift
162-
/// to avoid the overhead of type conversion back and forth. This exists solely for compatibility
163-
/// purposes.
164-
///
165-
/// Example:
166-
/// [self.analytics addDestinationMiddleware:^NSDictionary<NSString *,id> * _Nullable(NSDictionary<NSString *,id> * _Nullable event) {
167-
/// // drop all events named booya on the amplitude destination
168-
/// NSString *eventType = event[@"type"];
169-
/// if ([eventType isEqualToString:@"track"]) {
170-
/// NSString *eventName = event[@"event"];
171-
/// if ([eventName isEqualToString:@"booya"]) {
172-
/// return nil;
173-
/// }
174-
/// }
175-
/// return event;
176-
/// }, forKey: @"Amplitude"];
177-
///
178-
/// - Parameters:
179-
/// - middleware: The middleware to execute at the source level.
180-
/// - destinationKey: A string value representing the destination. ie: @"Amplitude"
181-
@objc(addDestinationMiddleware:forKey:)
182-
public func addDestinationMiddleware(middleware: @escaping ((_ event: [String: Any]?) -> [String: Any]?), destinationKey: String) {
183-
// couldn't find the destination they wanted
184-
guard let dest = analytics.find(key: destinationKey) else { return }
185-
_ = dest.add(plugin: ObjCShimPlugin(middleware: middleware))
186-
}
187-
}
188132

189133
// MARK: - ObjC Peripheral Functionality
190134

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// ObjCDestinationSupport.swift
3+
//
4+
//
5+
// Created by Brandon Sneed on 3/23/23.
6+
//
7+
8+
#if !os(Linux)
9+
10+
import Foundation
11+
12+
@objc(SEGDestination)
13+
public protocol ObjCDestination {}
14+
15+
public protocol ObjCDestinationShim {
16+
func instance() -> DestinationPlugin
17+
}
18+
19+
// NOTE: Destination plugins need something similar to the following to work
20+
// in objective-c.
21+
22+
/*
23+
24+
@objc(SEGMixpanelDestination)
25+
public class ObjCSegmentMixpanel: NSObject, ObjCDestination, ObjCDestinationShim {
26+
public func instance() -> DestinationPlugin { return MixpanelDestination() }
27+
}
28+
29+
*/
30+
31+
32+
#endif // !os(Linux)

Sources/Segment/ObjC/ObjCPlugin.swift

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)