iOS APP接入有赞商城SDK

之前项目中有接入过有赞商城得SDK,实现了商城H5嵌入到APP中,快速实现商城相关功能。本文主要记录一下接入过程中的知识点。

在APP接入SDK之前,默认你们的服务端已完成了接入。

1、初始化

我们是放在延迟配置项中的,延迟启动配置项是在首页viewDidAppear中进行调用的。

YZConfig *config = [[YZConfig alloc]initWithClientId:kYouZanClientId];
config.scheme = @"yourappscheme";
[YZSDK.shared initializeSDKWithConfig:config];

2、接入YZWebView

有赞商城的页面展现主要是通过YZWebView来实现的,通过加载对应的页面地址来实现不同的功能,例如商城首页、我的订单列表页等等。

[self.view addSubview:self.webView];
[self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.bottom.equalTo(self.view);
    make.top.equalTo(self.view.mas_top).offset(topMargin);
}];
switch (self.pageType) {
    case YouZanWebPageTypeHome:
        self.pageURLString = kYouZanHomePageURLString;
        break;
    case YouZanWebPageTypeOrderList:
        self.pageURLString = kYouZanOrderListPageURLString;
        break;
    default:
        self.pageURLString = @"";
        break;
}
NSURL *url = [NSURL URLWithString:self.pageURLString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];

YZWebView和Delegate相关

- (YZWebView *)webView {
    if (!_webView) {
        _webView = [[YZWebView alloc] initWithWebViewType:YZWebViewTypeWKWebView];
        _webView.noticeDelegate = self;
        _webView.delegate = self;
    }
    return _webView;
}

在需要登录的时候,YZWebView会通过delegate回调相关事件,需要在处理type=YZNoticeTypeLogin的回调,通过服务端接口获取cookieKey和cookieValue,然后再将认证信息同步给有赞SDK,同步完成后在主线程reload webview即完成登录。下图是有赞云文档中的认证方案,具体的详细认证方案,可以去有赞云文档中心查看。


- (void)webView:(YZWebView *)webView didReceiveNotice:(YZNotice *)notice
{
    switch (notice.type) {
        case YZNoticeTypeOther: {
            
            break;
        }
        case YZNoticeTypeLogin: {// 收到登陆请求
            [self loginToYouzan];
            break;
        }
        case YZNoticeTypeShare: {// 收到分享请求
            [self shareWithData:notice.response];
            break;
        }
        case YZNoticeTypeReady: {
            
            break;
        }
        case YZNoticeTypeAddToCart: {
            
            break;
        }
        case YZNoticeTypeBuyNow: {
            
            break;
        }
        case YZNoticeTypeAddUp: {
            
            break;
        }
        case YZNoticeTypePaymentFinished: {
            
            break;
        }
    }
}

- (void)loginToYouzan {
    WeakSelf(self)
    [YourServiceAPI loginYouZanWithCompletionBlock:^(ResponseModel *respModel) {
        if (respModel.isSuccess) {
            // 将认证信息同步给 SDK.
            NSDictionary *resultInfo = respModel.resultDictionary;
            NSString *cookieKey = resultInfo[@"cookieKey"];
            NSString *cookieValue = resultInfo[@"cookieValue"];
            
            [YZSDK.shared synchronizeCookieKey:cookieKey andCookieValue:cookieValue];
            dispatch_async(dispatch_get_main_queue(), ^{
                // 重新加载页面
                [weakself.webView reload];
            });
        } else {
            //提示错误信息
        }
    }];
}

分享事件处理,在didReceiveNotice中处理type=YZNoticeTypeShare的事件,我们的产品让直接调用了系统的分享界面。

- (void)shareWithData:(id)data {
    NSDictionary *shareDic = (NSDictionary *)data;
    
    NSString *link = shareDic[@"link"];
    if (!link) {
        return;
    }
    NSArray *activityItemsArray = @[link];
    NSArray *activityArray = @[];
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItemsArray applicationActivities:activityArray];
    activityVC.modalInPopover = YES;
    UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
        if (completed == YES) {
            NSLog(@"completed");
        }else{
            NSLog(@"cancel");
        }
    };
    activityVC.completionWithItemsHandler = itemsBlock;

    [self presentViewController:activityVC animated:YES completion:nil];
}

页面标题获取。

- (void)webViewDidFinishLoad:(id<YZWebView>)webView {
    [webView evaluateJavaScript:@"document.title"
              completionHandler:^(id  _Nullable response, NSError * _Nullable error) {
                self.title = response;
    }];
}

在自身APP用户退出登录后,需要同步退出有赞商城。

[YZSDK.shared logout];

注意:由于 iOS 编译的特殊性,为了方便开发者使用,有赞将 x86_64 armv7 arm64 几个架构都合并到了一起,所以使用动态库上传 App Store 时需要将x86_64 架构支持删除后,才能正常提交审核。需要下载有赞github中的 thin.sh文件,在有赞SDK所在目录执行 ./thin.sh 可删除 x86_64 平台的支持。

3、其它

其它未记录的内容大家可移步官网文档中心查看:

https://doc.youzanyun.com/doc#/content/35675/36986

https://github.com/youzan/YouzanMobileSDK-iOS/wiki/接入指南6.x#配置-app-scheme微信支付完成后跳转回app

尊重原创内容,转载请注明出处
本文链接地址: https://www.awnlab.com/archives/458

为您推荐

发表评论

邮箱地址不会被公开。 必填项已用*标注

2条评论

  1. 请问一下 APP开店的时候后, 会有二次登陆的情况, 您是怎么解决的呢? 可以联系我的邮箱332439409@qq.com

    1. 二次登录的情况,有赞会有登录回调,在登录回调中我们是通过调用自己后台接口提供的登录方法,然后再讲cookieKey和cookieValue同步给有赞。后台接口会跟有赞有相关登录校验逻辑,具体请查看有赞文档。