草庐IT

ios - 如何为 iOS 手动设置设备的锁定时间

coder 2024-01-25 原文

我知道在 iOS 中,如果您让设备闲置 45 秒,屏幕会变暗,如果再闲置 15 秒,设备将自动锁定。

我们可以禁用自动锁定 [UIApplication sharedApplication].idleTimerDisabled = YES

但是,我确实想要这个功能,只是想让它更长一些,有什么方法(没有越狱)可以让我手动设置这个定时器吗?

谢谢

最佳答案

您可以通过监视用户与您的应用程序的交互(触摸事件)并在自定义空闲计时器到期时设置 [UIApplication sharedApplication].idleTimerDisabled = NO; 来在一定程度上实现这一点。

您可以了解有关监控事件的更多信息on this blog .不过,我在下面概述了 ARC 代码更新的步骤。

一些关于测试后可能发生的事情的背景知识。无论是否设置了 idleTimerDisabled = YES;,Apple 的内部空闲计时器都会运行。这意味着如果用户在设置 idleTimerDisabled = NO; 时没有与手机互动超过自动锁定设置(即 1 分钟),设备将立即变暗并在之后完全关闭15 秒。所以我们可以做的是禁用 idleTimer,并手动创建一个新计时器,它会等待 x 分钟,然后再次启用 idleTimer。

这将有效地允许您增加自动锁定时间。我不认为你可以减少它(即用户有自动锁定从不,你想在一分钟后锁定设备)。

使用以下代码(假设您将自动锁定设置为 1 分钟)应用程序将保持唤醒状态 2 分钟,之后我们设置 idleTimerDisabled = NO; 将应用程序调暗 15 秒它会关闭。

  1. 将以下两个文件添加到您的项目中(original source here) :

    ELCUIApplication.h

    //
    //  ELCUIApplication.h
    //
    //  Created by Brandon Trebitowski on 9/19/11.
    //  Copyright 2011 ELC Technologies. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    // # of minutes before application times out
    #define kApplicationTimeoutInMinutes 2
    
    // Notification that gets sent when the timeout occurs
    #define kApplicationDidTimeoutNotification @"ApplicationDidTimeout"
    
    /**
     * This is a subclass of UIApplication with the sendEvent: method 
     * overridden in order to catch all touch events.
     */
    
    @interface ELCUIApplication : UIApplication {
        NSTimer *_idleTimer;
    }
    
    /**
     * Resets the idle timer to its initial state. This method gets called
     * every time there is a touch on the screen.  It should also be called
     * when the user correctly enters their pin to access the application.
     */
    - (void)resetIdleTimer;
    
    @end
    

    ELCUIApplication.m

    //
    //  ELCUIApplication.m
    //
    //  Created by Brandon Trebitowski on 9/19/11.
    //  Copyright 2011 ELC Technologies. All rights reserved.
    //
    
    #import "ELCUIApplication.h"
    
    @implementation ELCUIApplication
    
    - (void)sendEvent:(UIEvent *)event {
        [super sendEvent:event];
    
        // Fire up the timer upon first event
        if(!_idleTimer) {
            [self resetIdleTimer];
        }
    
        // Check to see if there was a touch event
        NSSet *allTouches = [event allTouches];
        if ([allTouches count] > 0) {
            UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
            if (phase == UITouchPhaseBegan) {
                [self resetIdleTimer];         
            }
        }
    }
    
    - (void)resetIdleTimer {
        if (_idleTimer) {
            [_idleTimer invalidate];
    //        [_idleTimer release];
        }
    
        // Schedule a timer to fire in kApplicationTimeoutInMinutes * 60
        float timeout = kApplicationTimeoutInMinutes * 60;
        _idleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout
                                                      target:self 
                                                    selector:@selector(idleTimerExceeded) 
                                                    userInfo:nil 
                                                     repeats:NO];
    }
    
    - (void)idleTimerExceeded {
        /* Post a notification so anyone who subscribes to it can be notified when
         * the application times out */ 
        [[NSNotificationCenter defaultCenter]
         postNotificationName:kApplicationDidTimeoutNotification object:nil];
    }
    
    //- (void) dealloc {
    //  [_idleTimer release];
    //  [super dealloc];
    //}
    
    @end
    
  2. 在您的 Supporting Files 文件夹中打开 main.m,更新以下内容:

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    
    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv,  @"ELCUIApplication", NSStringFromClass([AppDelegate class]));
        }
    }
    
  3. AppDelegate.m 中编辑 didFinishLaunchingWithOptions: 方法并添加 applicationDidTimeout: 方法。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        [UIApplication sharedApplication].idleTimerDisabled = YES;
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:)
                                                 name:kApplicationDidTimeoutNotification object:nil];
        return YES;
    }
    
    - (void) applicationDidTimeout:(NSNotification *) notif {
        NSLog(@"applicationDidTimeout");
        [UIApplication sharedApplication].idleTimerDisabled = NO;
    }
    

关于ios - 如何为 iOS 手动设置设备的锁定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324060/

有关ios - 如何为 iOS 手动设置设备的锁定时间的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  3. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  4. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  5. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  6. ruby-on-rails - Rails - 乐观锁定总是触发 StaleObjectError 异常 - 2

    我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd

  7. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  8. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  9. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  10. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

随机推荐