Skip to content

Commit

Permalink
docs: 添加 SwiftUI 使用 SDK 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Aug 21, 2023
1 parent e157f90 commit 1e422c2
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 54 deletions.
22 changes: 22 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@
34C0BF3A277EA7C90047ADC4 /* DataTrafficTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 34C0BF39277EA7C90047ADC4 /* DataTrafficTest.m */; };
34C0BF3D277EA9BA0047ADC4 /* MobileDebuggerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 34C0BF3C277EA9BA0047ADC4 /* MobileDebuggerTest.m */; };
34ECFDBF2A5C082A0059F87C /* GrowingAutotrackPageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34ECFDBE2A5C082A0059F87C /* GrowingAutotrackPageViewController.m */; };
34FCF7E12A937ECA009611B3 /* SwiftUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34FCF7E02A937ECA009611B3 /* SwiftUIView.swift */; };
34FCF7E42A937F4F009611B3 /* GIOSwiftUIContentViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34FCF7E32A937F4F009611B3 /* GIOSwiftUIContentViewController.m */; };
46B566A91FF28564AA03491F /* Pods_ProtobufTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C2E2C01C3AC89773BC3F4BE1 /* Pods_ProtobufTests.framework */; };
4916270F24E157BB00444AF2 /* GIOPresentViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4916270E24E157BB00444AF2 /* GIOPresentViewController.m */; };
4916271224E157CF00444AF2 /* GIOPagingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4916271124E157CF00444AF2 /* GIOPagingViewController.m */; };
Expand Down Expand Up @@ -400,6 +402,10 @@
34C0BF3C277EA9BA0047ADC4 /* MobileDebuggerTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MobileDebuggerTest.m; sourceTree = "<group>"; };
34ECFDBD2A5C082A0059F87C /* GrowingAutotrackPageViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GrowingAutotrackPageViewController.h; sourceTree = "<group>"; };
34ECFDBE2A5C082A0059F87C /* GrowingAutotrackPageViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GrowingAutotrackPageViewController.m; sourceTree = "<group>"; };
34F94ED82A938725002CB9FC /* Example-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "Example-Bridging-Header.h"; path = "Example/Example-Bridging-Header.h"; sourceTree = SOURCE_ROOT; };
34FCF7E02A937ECA009611B3 /* SwiftUIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIView.swift; sourceTree = "<group>"; };
34FCF7E22A937F4F009611B3 /* GIOSwiftUIContentViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GIOSwiftUIContentViewController.h; sourceTree = "<group>"; };
34FCF7E32A937F4F009611B3 /* GIOSwiftUIContentViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GIOSwiftUIContentViewController.m; sourceTree = "<group>"; };
3D63F725A31AE372B485C361 /* Pods-HostApplicationTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HostApplicationTests.debug.xcconfig"; path = "Target Support Files/Pods-HostApplicationTests/Pods-HostApplicationTests.debug.xcconfig"; sourceTree = "<group>"; };
42ADE26B250B292900CA7268 /* HybridTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HybridTest.m; sourceTree = "<group>"; };
42F5EA0EFE2552358C35A0F5 /* Pods_HostApplicationTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_HostApplicationTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -540,6 +546,7 @@
046530FF24DD4271002D254C /* AppDelegate.m */,
046530D124DD4271002D254C /* Assets.xcassets */,
04A6FAE024E2BF11006C72F0 /* Example.entitlements */,
34F94ED82A938725002CB9FC /* Example-Bridging-Header.h */,
046530FD24DD4271002D254C /* GrowingIO-Info.plist */,
046530BD24DD4271002D254C /* GrowingIO-Prefix.pch */,
046530DA24DD4271002D254C /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -933,6 +940,9 @@
34106BB328FECB0E00E7DB01 /* Crasher.mm */,
349DA46A28F2BF9400C4281F /* GIOCrashMonitorTableViewController.h */,
349DA46B28F2BF9400C4281F /* GIOCrashMonitorTableViewController.mm */,
34FCF7E02A937ECA009611B3 /* SwiftUIView.swift */,
34FCF7E22A937F4F009611B3 /* GIOSwiftUIContentViewController.h */,
34FCF7E32A937F4F009611B3 /* GIOSwiftUIContentViewController.m */,
);
path = Others;
sourceTree = "<group>";
Expand Down Expand Up @@ -1608,6 +1618,7 @@
};
6003F589195388D20070C39A = {
DevelopmentTeam = SXBU677CPT;
LastSwiftMigration = 1430;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -2274,6 +2285,7 @@
files = (
04A6FAF224E662E9006C72F0 /* GIOChildsAddViewController.m in Sources */,
348C86AF2A60FC9000310C22 /* GrowingIgnoreButton3.m in Sources */,
34FCF7E12A937ECA009611B3 /* SwiftUIView.swift in Sources */,
349F1492285B560200437F53 /* UIColor+Hex.m in Sources */,
348C86A52A60FC1100310C22 /* GrowingIgnoreViewClsViewController.m in Sources */,
349F1489285B503B00437F53 /* GIOH5BrowserViewController.m in Sources */,
Expand Down Expand Up @@ -2309,6 +2321,7 @@
0465315A24DD4272002D254C /* GIOFoodTableViewCell.m in Sources */,
0465314524DD4272002D254C /* GIOCustomEventViewController.m in Sources */,
0465314724DD4272002D254C /* GIOContainerViewController.m in Sources */,
34FCF7E42A937F4F009611B3 /* GIOSwiftUIContentViewController.m in Sources */,
0465314E24DD4272002D254C /* GIOLabelAttributeViewController.m in Sources */,
349F1488285B503B00437F53 /* GIODefaultWebViewController.m in Sources */,
0465315324DD4272002D254C /* GIOActionSheetViewController.m in Sources */,
Expand Down Expand Up @@ -2529,6 +2542,7 @@
3494DD0128597E1A00A6CE46 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
Expand Down Expand Up @@ -2574,6 +2588,7 @@
3494DD0228597E1A00A6CE46 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
Expand Down Expand Up @@ -3189,6 +3204,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 3D63F725A31AE372B485C361 /* Pods-HostApplicationTests.debug.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
Expand Down Expand Up @@ -3236,6 +3252,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = FAB05E3514907518EF7B9514 /* Pods-HostApplicationTests.release.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
Expand Down Expand Up @@ -3358,6 +3375,7 @@
baseConfigurationReference = 91EEE76F9E0E94DB389A9D21 /* Pods-Example.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
Expand All @@ -3378,6 +3396,8 @@
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SWIFT_OBJC_BRIDGING_HEADER = "$(SRCROOT)/Example/Example-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
WRAPPER_EXTENSION = app;
Expand All @@ -3389,6 +3409,7 @@
baseConfigurationReference = DDE20EE0BB4E138A407A5D1C /* Pods-Example.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
Expand All @@ -3409,6 +3430,7 @@
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SWIFT_OBJC_BRIDGING_HEADER = "$(SRCROOT)/Example/Example-Bridging-Header.h";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
WRAPPER_EXTENSION = app;
Expand Down
24 changes: 24 additions & 0 deletions Example/Example/Example-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Example-Bridging-Header.h
// GrowingAnalytics
//
// Created by YoloMao on 2023/8/21.
// Copyright (C) 2023 Beijing Yishu Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef Example_Bridging_Header_h
#define Example_Bridging_Header_h


#endif /* Example_Bridging_Header_h */
28 changes: 28 additions & 0 deletions Example/Example/Others/GIOSwiftUIContentViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// GIOSwiftUIContentViewController.h
// GrowingAnalytics
//
// Created by YoloMao on 2023/8/21.
// Copyright (C) 2023 Beijing Yishu Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface GIOSwiftUIContentViewController : UIViewController

@end

NS_ASSUME_NONNULL_END
40 changes: 40 additions & 0 deletions Example/Example/Others/GIOSwiftUIContentViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// GIOSwiftUIContentViewController.m
// GrowingAnalytics
//
// Created by YoloMao on 2023/8/21.
// Copyright (C) 2023 Beijing Yishu Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "GIOSwiftUIContentViewController.h"
#import "Example-Swift.h"

@interface GIOSwiftUIContentViewController ()

@end

@implementation GIOSwiftUIContentViewController

- (void)viewDidLoad {
[super viewDidLoad];

if (@available(iOS 13.0, *)) {
UIViewController *controller = [SwiftUIViewWrapper viewController];
UIView *view = controller.view;
view.frame = self.view.frame;
[self.view addSubview:view];
}
}

@end
68 changes: 68 additions & 0 deletions Example/Example/Others/SwiftUIView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// SwiftUIView.swift
// GrowingAnalytics
//
// Created by YoloMao on 2023/8/21.
// Copyright (C) 2023 Beijing Yishu Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// 参考:https://github.com/firebase/firebase-ios-sdk/tree/master/FirebaseAnalyticsSwift
#if canImport(SwiftUI)
import SwiftUI
import GrowingAnalytics

@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, *)
@objc class SwiftUIViewWrapper: NSObject {
@objc static func viewController() -> UIViewController {
UIHostingController(rootView: SwiftUIView())
}
}

@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, *)
struct SwiftUIView: View {
var body: some View {
VStack {
Text("Hello, World!")
.padding()
Button("TrackCustomEvent") {
GrowingAutotracker.sharedInstance().trackCustomEvent("Custom", withAttributes: ["key": "value"])
}
}
.growingTrackView("SwiftUI-View-OnAppear", attributes: ["key": "value"])
// .onAppear {
// GrowingAutotracker.sharedInstance().trackCustomEvent("SwiftUI-View-OnAppear", withAttributes: ["key": "value"])
// }
}
}

@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, *)
extension View {
func growingTrackView(_ eventName: String, attributes: [String: String]? = nil) -> some View {
onAppear {
if let attributes = attributes {
GrowingAutotracker.sharedInstance().trackCustomEvent(eventName, withAttributes: attributes)
} else {
GrowingAutotracker.sharedInstance().trackCustomEvent(eventName)
}
}
}
}

@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, *)
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
}
}
#endif
Loading

0 comments on commit 1e422c2

Please sign in to comment.