最近寫了手機衛士,有個小需求是將系統任何地方復制得到的手機號碼 粘貼到自己的手機衛士App中查詢該號碼信息,那么就有個去特殊符號的問題,只留數字,另外需要自動粘貼到自己App輸入框,所以呢在 下面的方法 獲取粘貼板內容並處理號碼為純數字結果:(PS :最近流行的微信復制淘寶鏈接內容,打開淘寶即可跳轉到相應的 頁面,使用的也是此技術要點,當下搬運工,參考:http://www.jianshu.com/p/10a6900cc904)
- (void)applicationDidBecomeActive:(UIApplication *)application { UIPasteboard *paste = [UIPasteboard generalPasteboard]; if (paste.string.length>=7) { NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:0 error:NULL]; //方法一去掉不符合規則的字符 @"a-zA-Z.-*#"; // NSString *resultString = [regular stringByReplacingMatchesInString:paste.string options:0 range:NSMakeRange(0, paste.string.length) withTemplate:@""]; // NSLog(@"pasteBoardString = %@ result = %@",paste.string,resultString); NSArray *arr =[regular matchesInString:paste.string options:0 range:NSMakeRange(0, paste.string.length)]; NSString *string = @""; for (NSTextCheckingResult *res in arr) { string = [string stringByAppendingFormat:@"%@",[paste.string substringWithRange:res.range]]; } NSLog(@"phone number = %@",string);
if (string.length>=7) {
BaseNavigationController * nav = (BaseNavigationController *)self.window.rootViewController;
if ([nav.topViewController isKindOfClass:[ViewController class]]) {
ViewController *rootVC = (ViewController *)nav.topViewController;
rootVC.pastenNmber = string;
paste.string = @"";
}
}
} // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. }
