开发环境
Xocde 12.4
Swift 5.3
Mac OS 10.15.6
生成商品优惠券
接口
https://api.sunofbeach.net/shop/tpwd
post
body:
{
"title":"商品的title",
"url" :"https: + couponClickUrl"
}
//返回数据
{
"success": true,
"code": 10000,
"message": "淘宝口令构建成功!",
"data": {
"tbk_tpwd_create_response": {
"data": {
"model": "4¥rm11X0tDlMV¥ https://m.tb.cn/h.4Mybft3 亲亲纯蛋糕400g零食早餐速食整箱懒人面包手工鸡蛋糕點营养食品"
},
"request_id": "nx55f5sw7i9b"
}
}
}
编写返回数据的模型
把上面返回的json复制,然后打开软件JSONExport。选择swift,object mapping一件生成。
请求接口部分
func getCoupons() {
//获取优惠券
//let paraData = CouponsPara(title: goodsData?.title ?? "", url: goodsData?.couponClickUrl ?? "")
//获取优惠券的url
let clickUrl = goodsData?.couponClickUrl ?? ""
//拼接https
let targetUrl = "https:\(clickUrl)"
let paraData: NSDictionary = ["title":goodsData?.title ?? "", "url":targetUrl]
let headers = [
"Content-Type": "application/json"
]
Alamofire.request(UnionApi.getCouponsUrl(), method: .post, parameters: paraData as! Parameters,encoding: JSONEncoding.default, headers: headers).responseObject{ (response: DataResponse<CouponsResult>) in
let data = response.result.value
//print(data?.data?.tbkTpwdCreateResponse?.data?.model)
//显示优惠券数据
if let model = data?.data?.tbkTpwdCreateResponse?.data?.model{
self.labCouponContent.text = model
}
}
}
获取成功之后,显示到页面上面就行了。设置text。
把优惠券复制到粘贴板和打开淘宝
复制文本到粘贴板很简单
UIPasteboard.general.string = "需要放到粘贴板的文本"
打开一个窗口,让我们选择是否直接打开淘宝
// 复制到粘贴板中
@objc func copyToPasteboard(sender: UIButton){
//UIPasteboard.general.string = self.labCouponContent.text
let pboard = UIPasteboard.general
pboard.string = self.labCouponContent.text
let titleStr = "淘口令复制成功,是否跳转到手机淘宝APP?"
let jumpStr = "taobao://item.taobao.com/item.htm"
let alertCtr = UIAlertController.init(title: titleStr, message: nil, preferredStyle: UIAlertController.Style.alert)
alertCtr.addAction(UIAlertAction.init(title: "取消", style: UIAlertAction.Style.cancel, handler: nil))
alertCtr.addAction(UIAlertAction.init(title: "确定", style: UIAlertAction.Style.default, handler: { (action) in
if let url = URL.init(string: jumpStr) {
if UIApplication.shared.canOpenURL(url) == true {
UIApplication.shared.openURL(url)
} else {
let alertC = UIAlertController.init(title: "您未安装手机淘宝APP,是否前往AppStore下载安装?", message: nil, preferredStyle: UIAlertController.Style.alert)
alertC.addAction(UIAlertAction.init(title: "取消", style: UIAlertAction.Style.cancel, handler: nil))
alertC.addAction(UIAlertAction.init(title: "确定", style: UIAlertAction.Style.default, handler: { (action: UIAlertAction) in
let AppStoreVC = UIViewController.init()
AppStoreVC.modalTransitionStyle = UIModalTransitionStyle.coverVertical
let webView = UIWebView.init(frame: AppStoreVC.view.bounds)
let request = NSURLRequest.init(url: URL.init(string: "itms-apps://itunes.apple.com/cn/app/tao-bao-sui-shi-sui-xiang/id387682726?mt=8")!) //AppStore手机淘宝地址
webView.loadRequest(request as URLRequest)
AppStoreVC.view.addSubview(webView)
self.present(AppStoreVC, animated: true, completion: {
AppStoreVC.dismiss(animated: true, completion: nil)
})
}))
self.present(alertC, animated: true, completion: nil)
}
}
}))
self.present(alertCtr, animated: true, completion: nil)
}
这样打开,是会失败的,我们还需要在info.plist
中配置一个值,支持打开淘宝和天猫
LSApplicationQueriesSchemes
Array类型
保存2个值,taobao,tmall
代码地址:
https://github.com/cat13954/IOSUiTableViewSample
最近比较忙,一个星期没更新了,今天就先到这里先啦
下一篇文章的内容,应该是显示多个不同类型的商品列表