刚过完双十一,支付宝盖楼,助力得猫币,铺天盖地出现在朋友圈,多年不联系的朋友发来各种口令。
整一下类似淘口令。
主要用到的就是UIPasteboard。
UIPasteboard
UIPasteboard主要用来粘贴和复制的。
//获取系统级别的剪切板系并且在整个设备中共享,程序退出后台,删除都可以存在
+(UIPasteboard*)generalPasteboard;
// 自定义的剪切板 , name为剪切板的名称, create用于设置剪切板不存在时 是否进行创建
+(nullable UIPasteboard*)pasteboardWithName:(NSString*)pasteboardName create:(BOOL)create;
//获取一个应用内可用的剪切板
+(UIPasteboard*)pasteboardWithUniqueName;
淘口令是微信和淘宝之间粘贴复制的功能,所以需要用系统的剪切板。
上代码
// 【New Balance/NB 男鞋女鞋情侣复古鞋运动鞋跑步鞋ML565SG/BG/BLN】,椱ァ製这句话₴TzdTYv5eVOQ₴后咑閞淘灬寳
// 【一起蓋の樓,瓜分20亿红I包】,椱ァ製这句话¥GNQ7YGRlHde¥后咑閞淘宀┡ē
// 【拜托拜托,帮我助力得喵币,你也有喵币哦,一起瓜分20亿】,椱ァ製这段描述¥3n1CYJFcAwe¥后到手机天猫
// 把需要复制的内容复制到剪切板
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
pboard.string = [NSString stringWithFormat:@"【New Balance/NB 男鞋女鞋情侣复古鞋运动鞋跑步鞋ML565SG/BG/BLN】,椱ァ製这句话₴%@₴后咑閞淘灬寳",@"TzdTYv5eVOQ"];
NSLog(@"复制成功");
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"复制成功" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:cancelAction];
[self presentViewController:alertVC animated:YES completion:nil];
// 粘贴
// APP将要进入前台
- (void)sceneWillEnterForeground:(UIScene *)scene {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
// 识别APP
if ([pasteboard.string containsString:@"淘灬寳"]) {
// 提取商品ID
NSRange firstRange = [pasteboard.string rangeOfString:@"₴"];
NSString * tempStr = [pasteboard.string substringFromIndex:firstRange.location+1];
NSRange secondRange = [tempStr rangeOfString:@"₴"];
NSString *goodsID = [tempStr substringToIndex:secondRange.location];
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:goodsID message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"跳转商品详情页");
}];
[alertVC addAction:cancelAction];
[self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];
// 清空剪切板
pasteboard.string = @"";
}
}
GitHub代码地址