iOS集成指南

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

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

一、安装SDK

方式一:Cocoapods(推荐)

    pod 'SharetraceSDK', '~> 2.3.0'

注: 请确保当前Cocoapods的repo已经更新至最新,可以通过以下命令查询和更新

pod search SharetraceSDK //查询本地repo中SharetraceSDK的版本
pod repo update // 更新本地repo

更多命令可以查看Cocoapods官方文档

方式二:手动集成

前往下载页下载最新版的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, 填入以下内容:

(注: 请确保新增的Run Script放置在Embed Framework之后,可以通过拖动排序)

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.initWith(self)
        return true
    }

Objective-C


    #import <SharetraceSDK/SharetraceSDK.h>

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [Sharetrace initWithDelegate:self];
        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:  paramsData:%@", [appdata paramsData]);
    } :^(NSInteger code, NSString * _Nonnull message) {
        NSLog(@"ShareTrace fail: code:%ld; message:%@", code, message);
    }];

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

五、一键调起(如不需要此功能可忽略此步骤)

Sharetrace支持通过标准的Scheme和Universal Links(iOS>=9),接入Sharetrace SDK后,在各种浏览器,包括微信,微博等内置浏览器一键调起app,并传递网页配置等自定义动态参数。配置只需简单几个步骤即可,如下:

5.1 开启一键调起功能

登录Sharetrace的管理后台,找到iOS配置,开启相关功能和填入配置 5_apple_config_on.png

其中Team Id可以在Apple开发者后台查看

5.2 开启Associated Domains服务

方法一(推荐):Xcode一键开启

(这里以Xcode 12为例,其他Xcode版本类似)

5_1_domains.png

在如下图所示位置填入Sharetrace后台提供的applinks

5_applinks_value.png

方法二:通过Apple开发者管理后台手动开启

登录到Apple管理后台,在Identifiers找到所需开启到App ID

5_apple_dev_config.png

5.3 Scheme配置

找到项目Info配置,填入后台分配的Scheme, 如下图:

ios_scheme_value.png

5.4 代码配置

找到AppDelegate文件,填入以下配置:

Swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        Sharetrace.handleSchemeLinkURL(url)
        return true
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        Sharetrace.handleSchemeLinkURL(url)
        return true
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        Sharetrace.handleUniversalLink(userActivity)
        return true
    }
}

extension AppDelegate: SharetraceDelegate {

    func getWakeUpTrace(_ appData: AppData?) {
        if let appData = appData {
            showWakeResult(title: "getWakeUpTrace", msg: appData.paramsData)
        } else {
            showWakeResult(title: "getWakeUpTrace", msg: "Failed to getWakeUpTrace!")
        }
    }
}

Objective-C

@interface AppDelegate ()<SharetraceDelegate>

@end

@implementation AppDelegate

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

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    [Sharetrace handleUniversalLink:userActivity];
    return YES;
}

//iOS9以下
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    [Sharetrace handleSchemeLinkURL:url];
    return YES;
}

//iOS9及以上
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(nonnull NSDictionary *)options {
    [Sharetrace handleSchemeLinkURL:url];
    return YES;
}

- (void)getWakeUpTrace:(AppData * _Nullable)appData {
    if (appData != nil) {
        NSString *info = [NSString stringWithFormat:@"appData: \n %@,  %@", appData.paramsData, appData.channel];
        NSLog(@"getWakeUpTrace Success, %@", info);
    } else {
        NSLog(@"getWakeUpTrace nil");
    }
}

@end

六、配置安装方式

1、配置安装方式:SDK 集成完成后,进入sharetrace控制台 — 应用详情 — iOS配置 — 点击“去集成” — 点击“我已集成,下一步” — 选择任意一种安装方式,填写信息 — 点击“下一步”,完成安装方式配置

2、安装方式说明

  • (1)自定义链接地址:即是你APP的安装包下载链接
  • (2)App Store:填写你APP在AppStore的地址
  • (3)平台托管:将你APP的安装上传到ShareTrace平台,你的用户下载时,直接从ShareTrace服务器下载
  • (4)暂不配置:只用于本地测试,适合在没有配置web SDK的情况下使用

results matching ""

    No results matching ""