- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一种方法可以在应用程序激活时检查剪贴板:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:@"reload" object:nil];
dispatch_queue_t backgroundQueue = dispatch_queue_create("clipboardQueue", 0);
dispatch_async(backgroundQueue, ^{
[self checkClipboard];
});
}
但应用程序挂起的时间过长(似乎是剪贴板数据的正则表达式检查)- 我如何强制此进程在另一个线程上运行?看来我的常规 GCD 方法似乎不起作用。
这是它挂起的函数:
- (BOOL)validateUrl:(NSString *)candidate {
NSString *urlRegEx = @"\\b(?:(?:https?|ftp):\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!10(?:\\.\\d{1,3}){3})(?!127(?:\\.\\d{1,3}){3})(?!169\\.254(?:\\.\\d{1,3}){2})(?!192\\.168(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\x{00a1}-\\x{ffff}0-9]+-?)*[a-z\\x{00a1}-\\x{ffff}0-9]+)(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]+-?)*[a-z\\x{00a1}-\\x{ffff}0-9]+)*(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}]{2,})))(?::\\d{2,5})?(?:\\/[^\\s]*)?\\b(\\/)?";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
这是我的checkClipboard
方法:
- (void)checkClipboard {
[[ProfileManager sharedInstance] getUserProfile:^(BOOL done, Profile *profile, NSInteger status) {
if (done) {
NSURL *lastUrl = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] stringForKey:@"clipboard"]];
UIPasteboard *pb = [UIPasteboard generalPasteboard];
NSMutableString *urlString = pb.string.mutableCopy;
if (urlString.length) {
if (![urlString containsString:@"http"]) {
urlString = [NSString stringWithFormat:@"http://%@",urlString].mutableCopy;
}
BOOL urlValidate = [self validateUrl:urlString];
if (urlValidate) {
if (!pb.string.length || [urlString isEqualToString:lastUrl.absoluteString] || !urlValidate) {
return;
} else {
dispatch_async(dispatch_get_main_queue(), ^{
//popup toast code
});
}
}
}
} else {
NSLog(@"Not Logged In");
}
}];
}
最佳答案
似乎 checkClipboard 方法在主线程中调用了 valudateURL,像这样:
dispatch_async(dispatch_get_main_queue(), ^{
[self valudateURL];
});
因此,如果您希望 valudateURL 在这种情况下在非主线程中工作,您应该直接调用它而不使用 dispatch_async。
-- 已编辑--
是的,所以 getUserProfile: 阻止在主线程中运行,如您在屏幕截图中所见,这就是它阻止应用程序的原因。所以如果你想让 validateUrl:complete: 在后台运行,你应该用 dispatch_async 调用它,像这样:
- (void)checkClipboard {
[[ProfileManager sharedInstance] getUserProfile:^(BOOL done, Profile *profile, NSInteger status) {
...
dispatch_queue_t backgroundQueue = dispatch_queue_create("clipboardQueue", 0);
dispatch_async(backgroundQueue, ^{
[self validateUrl:urlString complete:^(BOOL urlValidate) {
...
dispatch_async(dispatch_get_main_queue(), ^{
//popup toast code
});
}];
});
}];
}
关于ios - applicationDidBecomeActive 方法阻塞了我的 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33831796/
我们遇到了一次奇怪的崩溃 - 现在我知道是什么原因造成的,但我注意到的一件奇怪的事情是所有崩溃日志在两个单独的背景上都有 -applicationDidBecomeActive线程 Thread 5:
我如何确定当前在 applicationDidBecomeActive 中的 Controller /窗口?例如在登录屏幕(这是一个 LoginViewController)上,如果 applicat
使用 Storyboard和 UINavigationController 我的应用程序有很多 UIViewControllers。最初的 UIViewController 被命名为 viewCont
我正在使用 NSLog 语句来了解何时调用 -applicationDidBecomeActive:,但它似乎从未被调用过。这是代码: - (void)applicationDidBecomeActi
applicationDidBecomeActive 方法何时被调用?这个方法的目的是什么? 最佳答案 了解 iOS 的状态和转换 州 未运行 - 应用未运行。 Inactive - 应用程序在前台运
我的应用程序委托(delegate)方法 applicationDidBecomeActive: 在应用程序首次启动时被调用两次。我有一些代码我只想执行一次并且我已经放入 applicationDid
在带有 RevenueCat 的 tvOS 13.4 上,当我在 4K 电视设备上运行我的应用程序,然后关闭电视(让应用程序保持运行)时,我得到以下信息: 2020-05-10 12:21:59-07
当我的手机长时间闲置并收到来自应用程序的推送时,我希望能够打开应用程序并执行网络功能。这通常有效。我在主应用程序 View 中发布来自 applicationDidBecomeActive 的通知,因
要求 - 当用户从后台 tabBar 打开应用程序时,所选索引应为 2 我尝试过的 - //For getting current visible controller - public ex
我有一个使用地理围栏的应用程序。当您越过地理围栏时,该应用程序会启动 - 但它是否会进入“事件”状态,即使它是从后台启动的?我们在 applicationDidBecomeActive 中有相关代码。
我有一种方法可以在应用程序激活时检查剪贴板: - (void)applicationDidBecomeActive:(UIApplication *)application { [[NSNot
这是我的应用委托(delegate)中 applicationDidBecomeActive 的主体 if (myViewController != nil) { [myViewControl
我正在使用 UILocalNotification 对象开发闹钟应用程序。在我的代码中,当报警并且用户点击通知面板时,我的应用程序将显示另一个 View Controller 。为了满足这种情况,我在
我正在使用重复本地通知向用户显示警报。为此,我使用了下面的代码 UILocalNotification* localNotification = [[UILocalNotification alloc
这是我的第一个问题,因为我在开发我的第一个 iOS 应用程序时遇到了问题。它是数以千计的手电筒应用程序之一,但我正在尝试为其添加尽可能多的功能。其中之一是在应用程序进入后台或终止时保存应用程序的状态。
当我的应用进入后台时,出于数据保护的原因,它必须模糊当前屏幕。屏幕内容不能在任务管理器窗口中看到,因此必须在应用程序退出之前完成模糊例程。随着每次 UI 更新,模糊处理必须在主循环中进行。它只是在普通
当用户通过将应用程序切换到前台来激活应用程序时,我需要在屏幕上隐藏一些东西。 我已经尝试在 applicationDidBecomeActive 或 applicationWillEnterForeg
我在尝试实现 iOS-8 的新交互式通知时遇到了这个奇怪的问题。在处理本地通知时,一切都按预期工作,但在处理 APNS 时,一切都不太顺利。 详细信息:我通过横幅获得自定义推送(我只注册到最小上下文)
applicationDidBecomeActive 方法在应用程序激活时被调用,有没有办法可以为某个 UIViewController 执行此方法?我知道有用于 View Controller 的
当应用在后台收到远程推送通知时,应用进入applicationDidBecomeActive。从那里,如何从通知中访问数据的 NSDictionary? 最佳答案 通知数据在 application:
我是一名优秀的程序员,十分优秀!