1.3.1 iOS集成指南

优质
小牛编辑
136浏览
2023-12-01

请先确保当前环境符合以下最低要求:

  • SDK 支持 iOS 8+
  • SDK 支持 Xcode 8.+
  • SDK 支持 bitcode

一、安装SDK

方式一:Cocoapods(推荐)

    pod 'SharetraceSDK'

方式二:手动集成

前往下载页下载最新版的iOS SDK 。前往下载

  1. 解压下载的ShareTraceSDK压缩包, 找到ShareTraceSDK.framework, 拖动添加到工程, 添加时请确保选中("Copy items if needed")
  2. 找到设置中的 General -> Embedded binaries, 增加ShareTraceSDK.framework, 确保选中Embed & Sign
  3. 找到Build Phases 选项, 在顶部Editor中增加Run Script Phase, 填入以下内容:
if [[ "$ACTION" != "install" ]]; then
exit 0;
fi
APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"
echo "start removes unused architectures..."
echo "$APP_PATH"
# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find "$APP_PATH" -name 'SharetraceSDK.framework' -type d | while read -r FRAMEWORK
do
FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"

EXTRACTED_ARCHS=()

for ARCH in $ARCHS
do
echo "Extracting $ARCH from $FRAMEWORK_EXECUTABLE_NAME"
lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH"
EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")
done

echo "Merging extracted architectures: ${ARCHS}"
lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}"
rm "${EXTRACTED_ARCHS[@]}"

echo "Replacing original executable with thinned version"
rm "$FRAMEWORK_EXECUTABLE_PATH"
mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH"

done

二、配置APP_KEY

在Info.plist文件中配置appKey

<key>com.sharetrace.APP_KEY</key>
<string>[SHARETRACE_APPKEY]</string>

请将 [SHARETRACE_APPKEY] 替换成 sharetrace 为应用分配的 appkey

三、初始化ShareTrace

AppDelegateapplication:didFinishLaunchingWithOptions中初始化ShareTrace。

Swift

    import SharetraceSDK

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Sharetrace.start()
        return true
    }

Objective-C

    #import <SharetraceSDK/SharetraceSDK.h>

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [Sharetrace start];
        return YES;
    }

四、获取安装携带的参数

当 APP 需要获取安装参数时(由 web 网页中传递过来的,如邀请码、用户id、来源网页等动态参数),调用ShareTrace.getInstallTrace方法,在回调方法中获取相应参数。

Swift

    Sharetrace.getInstallTrace ({[weak self] (traceData) in
        guard let self = self else {
            return
        }
        print(traceData)
    }) {[weak self] (code, message) in
        guard let self = self else {
            return
        }
        print("code: \(code), message: \(message)")
    }

Objective-C

    [Sharetrace getInstallTrace:^(AppData * _Nullable appdata) {
        NSLog(@"ShareTrace success: resumePage:%@; paramsData:%@", [appdata resumePage], [appdata paramsData]);
    } :^(NSInteger code, NSString * _Nonnull message) {
        NSLog(@"ShareTrace fail: code:%ld; message:%@", code, message);
    }];

注意: 该方法可以重复调用, 请处理好反复调用的逻辑。

五、配置安装方式

SDK 集成完成后,按照sharetrace控制台接入流程完后后续的配置。