Skip to content

Commit 5fcb291

Browse files
Add "swift" flag to firebaseUserAgent (#4448)
* Add "swift-runtime/true" to user agent when Swift detected. * Use "swift" instead of "swift-runtime" * ./scripts/style.sh * Add comment with the Swift check clarification * Fix bridging header path * Fix SwiftObject class name for old Siwft versions. * Add swift runtime flag with value true or false (to be able to distinguish from a no-value-provided case).
1 parent d6c1170 commit 5fcb291

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

Example/Core/Tests/FIRAppTest.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,9 @@ - (void)testInvalidLibraryName {
784784
}
785785

786786
- (void)testInvalidLibraryVersion {
787+
NSString *originalFirebaseUserAgent = [FIRApp firebaseUserAgent];
787788
[FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0+"];
788-
XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
789+
XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:originalFirebaseUserAgent]);
789790
}
790791

791792
- (void)testSingleLibrary {
@@ -812,6 +813,10 @@ - (void)testRegisteringNonConformingLibrary {
812813
XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"InvalidLibrary`/1.0.0"]);
813814
}
814815

816+
- (void)testSwiftFlagWithNoSwift {
817+
XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"swift/false"]);
818+
}
819+
815820
#pragma mark - Core Diagnostics
816821

817822
- (void)testCoreDiagnosticsLoggedWhenFIRAppIsConfigured {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2019 Google
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import XCTest
16+
@testable import FirebaseCore
17+
18+
class FIRAppTests: XCTestCase {
19+
func testSwiftFlagWithSwift() {
20+
XCTAssertTrue(FirebaseApp.firebaseUserAgent().contains("swift"))
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2019 Google
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import "FIRAppInternal.h"

Firebase/Core/FIRApp.m

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
#import <GoogleUtilities/GULAppEnvironmentUtil.h>
3939

40+
#import <objc/runtime.h>
41+
4042
// The kFIRService strings are only here while transitioning CoreDiagnostics from the Analytics
4143
// pod to a Core dependency. These symbols are not used and should be deleted after the transition.
4244
NSString *const kFIRServiceAdMob;
@@ -114,6 +116,7 @@ @implementation FIRApp
114116
static NSMutableDictionary *sAllApps;
115117
static FIRApp *sDefaultApp;
116118
static NSMutableDictionary *sLibraryVersions;
119+
static dispatch_once_t sFirebaseUserAgentOnceToken;
117120

118121
+ (void)configure {
119122
FIROptions *options = [FIROptions defaultOptions];
@@ -275,6 +278,7 @@ + (void)resetApps {
275278
sAllApps = nil;
276279
[sLibraryVersions removeAllObjects];
277280
sLibraryVersions = nil;
281+
sFirebaseUserAgentOnceToken = 0;
278282
}
279283
}
280284

@@ -556,8 +560,7 @@ + (void)registerInternalLibrary:(nonnull Class<FIRLibrary>)library
556560

557561
+ (NSString *)firebaseUserAgent {
558562
@synchronized(self) {
559-
static dispatch_once_t onceToken;
560-
dispatch_once(&onceToken, ^{
563+
dispatch_once(&sFirebaseUserAgentOnceToken, ^{
561564
// Report FirebaseCore version for useragent string
562565
[FIRApp registerLibrary:@"fire-ios"
563566
withVersion:[NSString stringWithUTF8String:FIRCoreVersionString]];
@@ -571,7 +574,11 @@ + (NSString *)firebaseUserAgent {
571574
if (sdkVersion) {
572575
[FIRApp registerLibrary:@"apple-sdk" withVersion:sdkVersion];
573576
}
577+
578+
NSString *swiftFlagValue = [self hasSwiftRuntime] ? @"true" : @"false";
579+
[FIRApp registerLibrary:@"swift" withVersion:swiftFlagValue];
574580
});
581+
575582
NSMutableArray<NSString *> *libraries =
576583
[[NSMutableArray<NSString *> alloc] initWithCapacity:sLibraryVersions.count];
577584
for (NSString *libraryName in sLibraryVersions) {
@@ -583,6 +590,20 @@ + (NSString *)firebaseUserAgent {
583590
}
584591
}
585592

593+
+ (BOOL)hasSwiftRuntime {
594+
// The class
595+
// [Swift._SwiftObject](https://github.com/apple/swift/blob/5eac3e2818eb340b11232aff83edfbd1c307fa03/stdlib/public/runtime/SwiftObject.h#L35)
596+
// is a part of Swift runtime, so it should be present if Swift runtime is available.
597+
598+
BOOL hasSwiftRuntime =
599+
objc_lookUpClass("Swift._SwiftObject") != nil ||
600+
// Swift object class name before
601+
// https://github.com/apple/swift/commit/9637b4a6e11ddca72f5f6dbe528efc7c92f14d01
602+
objc_getClass("_TtCs12_SwiftObject") != nil;
603+
604+
return hasSwiftRuntime;
605+
}
606+
586607
- (void)checkExpectedBundleID {
587608
NSArray *bundles = [FIRBundleUtil relevantBundles];
588609
NSString *expectedBundleID = [self expectedBundleID];

FirebaseCore.podspec

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ Firebase Core includes FIRApp and FIROptions which provide central configuration
4848
unit_tests.dependency 'OCMock'
4949
unit_tests.resources = 'Example/Core/App/GoogleService-Info.plist'
5050
end
51+
52+
s.test_spec 'swift-unit' do |swift_unit_tests|
53+
swift_unit_tests.source_files = 'Example/Core/Tests/Swift/**/*.swift',
54+
'Example/Core/Tests/Swift/**/*.h'
55+
swift_unit_tests.pod_target_xcconfig = {
56+
'SWIFT_OBJC_BRIDGING_HEADER' => '$(PODS_TARGET_SRCROOT)/Example/Core/Tests/Swift/FirebaseCore-iOS-Unit-swift-unit-Bridging-Header.h'
57+
}
58+
end
5159
end

0 commit comments

Comments
 (0)