【iOS】识别剪切板 淘口令

作者: Always_on | 来源:发表于2017-01-11 12:21 被阅读2430次

淘宝用了好多年,对于淘宝的淘口令第一次使用的时候感觉好神奇,分享一件商品复制粘贴,再打开淘宝就能提示,本想研究一下,当时工作一忙就忘了,最近刚好没什么事,过年这段时间买年货,给亲朋好友分享一些商品是经常的事,也就想起研究一下这个功能了,看了几篇文章,结果很惊讶,原来这么简单,word天啊~~~

下面的例子只是在程序入口添加了获取剪切板的代码,后台切前台的大家自行添加一下

获取设备的剪贴板很简单,一行代码搞定​:

NSString *shareStr = [UIPasteboard generalPasteboard].string;

弹出提示框,提示框我用的是系统的,根据自身实际需求定制;

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"口令" message:shareStr preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    secondViewController *secv = [[secondViewController alloc]init];

    secv.urlString = alert.message;

    UINavigationController *nav = (UINavigationController *)[UIApplication   sharedApplication].keyWindow.rootViewController;

    [nav pushViewController:secv animated:YES];

}]];

[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

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

工程本身是个demo,根控制器为firstViewController,如果根控制器为tabbarController的话请用下面方法获取nav;

UITabBarController *tabBarVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;

UINavigationController *nav = (UINavigationController *)tabBarVC.selectedViewController;

接下来在模拟器Safari中打开淘宝,复制淘宝链接;


Snip20170110_19.png

运行程序:


Snip20170110_22.png
点击后跳转:
Snip20170110_23.png

分享的为url地址,打开的页面使用WKWebView加载该页面:

[self.contentWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];

以上是我的测试的代码,如有问题请大家指出,你们的建议是我进步的动力!

相关文章

网友评论

    本文标题:【iOS】识别剪切板 淘口令

    本文链接:https://www.haomeiwen.com/subject/rwfrbttx.html