iOS development-Do not enter standby (screen to stay awake)---uiapplication learning

Source: Internet
Author: User

If you do not want the IPhone to enter the lock screen standby when the app is running, add the following line of code


[[UIApplication sharedapplication] setidletimerdisabled:yes];
By the way, learning the next uiapplication.
The iphone application is initiated by main function main, which is responsible for invoking Uiapplicationmainfunction, the form of the function is as follows:
Align Uiapplicationmain, option+ mouse click. can be viewed.


So what exactly does the Uiapplicationmain function do? This function is mainly responsible for three things:

1) Initializes the application object from the given class name, which is an instance of initializing the UIApplication or subclass object, and if you are given nil here, the system defaults to the UIApplication class, It is primarily this class that controls and coordinates the operation of the application. In subsequent work, you can use the static method sharedapplication to get the handle of the application.

2) Initializes an application delegate from the given application delegate class. and set the delegate to the application's delegate, here is if the passed parameter is nil, the function is called to access the Info.plist file to find the main nib file, get the application delegate.

3) Start the main event loop and start receiving events.
The above is the work of the Uiapplicationmain function, the next question is the application view display, the control of the message? Here is uiapplication(or subclass) the responsibility of the object, which mainly does the following:

1) is responsible for handling incoming user events and distributing event messages to the target object (sender, action) that should process the message.
2) Manage and control views, including rendering, control behavior, current display view, and more.
3) The object has an application delegate object that the application notifies when important events (which can include system events or life cycle control events) occur during some life cycles. For example, application startup, insufficient memory, or application completion, and so on, when these events occur, the application delegates to respond.

From the above analysis, you can know that uiapplication is a black box for developers, it can also be. Because all the operations can be done by its delegate, it only needs to maintain some immutable things, such as event message distribution and delivery, send an event processing request to the delegate, and so on, for example, the application load processing is complete, it sends a message to the delegate, and then the delegate can be in the Applicationdidfinishlanching in the delegate function to implement the action the developer wants. When you create an application with Xcode, an application delegate class is implemented by default. For loaded views, there is a view-related delegate class to handle the life events of the view loading process. Here are the main things that a delegate can do:
controlling the behavior of an application1,-(void) Applicationwillresignactive: (uiapplication *) ApplicationDescription: When the application is going to be inactive, during which time the application does not receive messages or events, such as coming to the phone2,-(void) Applicationdidbecomeactive: (uiapplication *) ApplicationDescription: When the application executes in the active state, this is exactly the opposite of the method above3,-(void) Applicationdidenterbackground: (uiapplication *) ApplicationDescription: Called when the program is pushed to the background. So to set the background to continue running, you can set it in this function4,-(void) Applicationwillenterforeground: (uiapplication *) ApplicationDescription: Called when the program is going back to the foreground from the background, which is exactly the opposite of the method above. 5,-(void) Applicationwillterminate: (uiapplication *) ApplicationDescription: When the program is about to exit is called, it is usually used to save the data and some cleanup work before exiting. This needs to set the key value of the uiapplicationexitsonsuspend. 6,-(void) applicationdidreceivememorywarning: (uiapplication *) ApplicationDescription: The iphone device has only limited memory, and if the application is assigned too much memory the operating system will terminate the application's operation, this method will be executed before termination, usually can be done in the memory cleanup work to prevent the program from being terminated7,-(void) Applicationsignificanttimechange: (uiapplication*) ApplicationDescription: Executes when the system time has changed8,-(void) applicationdidfinishlaunching: (uiapplication*) ApplicationDescription: Executes when the program is loaded9,-(void) application: (uiapplication) Application Willchangestatusbarframe: (cgrect) NewstatusbarframeDescription: Executes when the StatusBar box is going to change10,-(void) application: (uiapplication*) application willchangestatusbarorientation:(uiinterfaceorientation) newstatusbarorientationDuration: (nstimeinterval) DurationDescription: Executed when the StatusBar box direction is going to change11,-(BOOL) Application: (uiapplication*) Application Handleopenurl: (nsurl*) URLDescription: When executing via URL12,-(void) application: (uiapplication*) Application didchangestatusbarorientation: (uiinterfaceorientation) OldstatusbarorientationDescription: Executes when the StatusBar box direction changes13,-(void) application: (uiapplication*) Application Didchangesetstatusbarframe: (cgrect) OldstatusbarframeDescription: Executes when the StatusBar box changes
applications in the iphone are vulnerable to interruptions, such as an incoming call that may cause the application to lose focus, and if the phone is answered at this time, the application will go to the background. There are many other similar events that cause the iphone application to lose focus and call the Applicationwillresignactive () method of the delegate class before the application loses focus. The Applicationdidbecomeactive () method is called when the application gets to the focus again. For example, when running the application, the lock screen invokes the Applicationwillresignactive () method of the delegate class, and when the screen is unlocked, the Applicationdidbecomeactive () method is called.
Another very important approach is applicationdidreceivememorywarning (), because the iphone device has only limited memory, and if the application is assigned too much memory the operating system terminates the application's operation. But before terminating, the operating system warns the application by calling the Applicationdidreceivememorywarning () method of the delegate class first, and after UIApplication receives the event it passes it to The Applicationdidreceivememorywarning () method of the delegate class, in which the delegate class can release memory operations to prevent the operating system from forcing the application to terminate.
Here are some of the features of this class:1. Set the number icon on the icon//Set the digital icon on the main interface icon, introduced in 2.0, default is 0[UIApplication sharedapplication].applicationiconbadgenumber = 4;2. Whether the Redo,undo action is supported when setting the shake gesture//Shake gesture, whether the redo undo operation is supported. //3.0 introduced later, default Yes[uiapplication sharedapplication].applicationsupportsshaketoedit =yes;3. Judging the state of the program Operation//Judging the running state of the program, introduced after 2.0    /*uiapplicationstateactive,uiapplicationstateinactive,Uiapplicationstatebackground     */if ([uiapplication sharedapplication].applicationstate ==uiapplicationstateinactive) {NSLog (@ "program in Operation State");    }4. Prevent the screen from darkening into hibernation//Prevent screen from darkening, use cautiously, default to No 2.0[uiapplication sharedapplication].idletimerdisabled =yes;Use this feature sparingly, because it consumes very much power. 5. Show Networking Status//Display Networking Tag 2.0[uiapplication sharedapplication].networkactivityindicatorvisible =yes;6. Show an address on the mapnsstring* addresstext [email protected] "1 Infinite Loop, Cupertino, CA 95014";//URL encode the spacesAddresstext = [Addresstext stringbyaddingpercentescapesusingencoding:nsasciistringencoding];nsstring* urltext = [NSString stringwithformat:@ "http://maps.google.com/maps?q=%@", Addresstext];[[UIApplication Sharedapplication]openurl:[nsurlurlwithstring:urltext]];7. Send an emailnsstring *recipients [email protected] "Mailto:[email protected][email protected],[email protected]& Subject=hello from california! ";nsstring *body [email protected] "&body=it is raining in sunny california!";nsstring *email = [NSString stringwithformat:@ "%@%@", recipients, body];email = [email stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];[[UIApplication Sharedapplication]openurl:[nsurlurlwithstring:email]];8. Call a number//Call Google 411[[UIApplication sharedapplication]openurl:[nsurlurlwithstring:@ "tel://8004664411"];9. Send SMS//Text to Google SMS[[UIApplication sharedapplication]openurl:[nsurlurlwithstring:@ "sms://466453"];10. Open a URL//Lanuch any IPhone developers fav site[[UIApplication sharedapplication]openurl:[nsurlurlwithstring:@ "http://itunesconnect.apple.com"];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.