Proficient in iOS development-gcd and background processing

Source: Internet
Author: User
<span id="Label3"></p><p><p>Call time for some life cycle functions</p></p><p><p>When you open an app, call</p></p><p><p><strong>Applicationwillenterforeground:</strong></p></p><p><p><strong>Applicationdidbecomeactive:</strong></p></p><p><p></p></p><p><p>Press the home key to invoke</p></p><p><p><strong>Applicationwillresignactive:</strong></p></p><p><p><strong>Applicationdidenterbackground:</strong></p></p><p><p></p></p><p><p>Double-click the home key, swipe up to close the program, call</p></p><p><p><strong>Applicationwillterminate:</strong></p></p><p><p></p></p><p><p>Status Save and reply mechanism reference</p></p><p><p>http://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ Strategiesfor-implementingyourapp/strategiesforimplementingyourapp.html App Programming Guide for IOS</p></p><p><p></p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">in appdelegate.m, Each method adds the following statement, which is used to observe the invocation of the associated method when running the program switching state</span> . NSLog (<span style="color: #800000;">@ "</span><span style="color: #800000;">%@</span><span style="color: #800000;">"</span>, Nsstringfromselector (_cmd));</pre></pre><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">VIEWCONTROLLER.M</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">state <span style="color: #008000;">Lab</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Created by jierism on 16/7/31.</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">copyright©2016 Year Jierism. All Rights Reserved.</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">ViewController.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">viewcontroller () @property (strong,nonatomic) UILabel</span></span>*<span style="color: #000000;"><span style="color: #000000;">label; @property (strong,nonatomic) UIImage</span></span>*<span style="color: #000000;"><span style="color: #000000;">smiley, @property (strong,nonatomic) uiimageview</span></span>*<span style="color: #000000;"><span style="color: #000000;">smileyview, @property (assign,nonatomic) nsinteger index; @property (strong,nonatomic) Uisegmentedcontrol</span></span>*<span style="color: #000000;"><span style="color: #000000;">segmentedcontrol;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller{BOOL animate;}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {[super viewdidload]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">additional setup after loading the view, typically from a nib. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set Label</span></span>CGRect bounds =<span style="color: #000000;"><span style="color: #000000;">self.view.bounds; CGRect Labelframe</span></span>= CGRectMake (bounds.origin.x, Cgrectgetmidy (bounds)-<span style="color: #800080;"><span style="color: #800080;"></span> -</span>, bounds.size.width,<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">); Self.label</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[UILabel alloc] initwithframe:labelframe]; Self.label.font</span></span>= [uifont Fontwithname:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Helvetica</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Size<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">]; Self.label.text</span></span>=<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">bazinga!</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; Self.label.textAlignment</span></span>=<span style="color: #000000;"><span style="color: #000000;">nstextalignmentcenter; Self.label.backgroundColor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor clearcolor]; [self.view addSubview:self.label]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set smiley image, size is 84 pixels X84 pixels</span></span>CGRect smileyframe = CGRectMake (cgrectgetmidx (bounds)-<span style="color: #800080;"><span style="color: #800080;"></span> the</span>, Cgrectgetmidy (bounds)/<span style="color: #800080;"><span style="color: #800080;">2</span></span>-<span style="color: #800080;"><span style="color: #800080;"></span> the</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">); Self.smileyview</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[uiimageview alloc] initwithframe:smileyframe]; Self.smileyView.contentMode</span></span>=<span style="color: #000000;"><span style="color: #000000;">uiviewcontentmodecenter; NSString</span></span>*smileypath = [[nsbundle mainbundle] pathforresource:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Smiley</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>OfType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">PNG</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.smiley</span></span>=<span style="color: #000000;"><span style="color: #000000;">[UIImage imagewithcontentsoffile:smileypath]; Self.smileyView.image</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.smiley; [self.view addSubview:self.smileyView]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">to <span style="color: #008000;">Create a segmented control</span></span>Self.segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems:[nsarray arraywithobjects:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> one</span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> both</span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">three</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> four</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, nil]]; Self.segmentedControl.frame</span></span>= CGRectMake (bounds.origin.x+<span style="color: #800080;"><span style="color: #800080;"></span> -</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span>, bounds.size.width-<span style="color: #800080;"><span style="color: #800080;"></span> +</span>,<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">); [self.segmentedcontrol addtarget:self action: @selector (selectionchanged:) forcontrolevents:uicontroleventvaluechanged]; [self.view addSubview:self.segmentedControl]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">when returned from the background, displays the state of the original segmented Control's index value, and sets the default to select the first</span></span>Self.index = [[nsuserdefaults standarduserdefaults] integerforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Index</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.segmentedControl.selectedSegmentIndex</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.index; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">changing the application state notifies the application delegate, but because our class is not an application delegate, we cannot implement the delegate method and expect them to take Effect.</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">but we can register to accept notification that the reload changes are coming from the app</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">When the <span style="color: #008000;">program is in the process of text messages, by changing the animate marker, will stop the rotation, close the SMS window will continue to rotate</span></span>Nsnotificationcenter *center =<span style="color: #000000;"><span style="color: #000000;">[nsnotificationcenter defaultcenter]; [center addobserver:self selector: @selector (applicationwillresignactive) name: (uiapplicati Onwillresignactivenotification)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Object</span></span><span style="color: #000000;"><span style="color: #000000;">: nil]; [center addobserver:self selector: @selector (applicationdidbecomeactive) name:uiapplication Didbecomeactivenotification</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Object</span></span><span style="color: #000000;"><span style="color: #000000;">: nil]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">re-register two notifications, with the following two methods, when the program into the background to reclaim the smiling face of the resources, when the program returned from the background to re-create it, optimize system resources</span></span><span style="color: #000000;"><span style="color: #000000;">[center addobserver:self selector: @selector (applicationdidenterbackground) name: (U Iapplicationwillresignactivenotification)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Object</span></span><span style="color: #000000;"><span style="color: #000000;">: nil]; [center addobserver:self selector: @selector (applicationwillenterforeground) Name:uiapplica Tiondidbecomeactivenotification</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Object</span></span><span style="color: #000000;"><span style="color: #000000;">: nil];}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Rotatelabeldown{[UIView animatewithduration:</span></span><span style="color: #800080;"><span style="color: #800080;">0.5</span></span>animations:^<span style="color: #000000;"><span style="color: #000000;">{self.label.transform</span></span>=<span style="color: #000000;"><span style="color: #000000;">cgaffinetransformmakerotation (m_pi); } completion:</span></span>^<span style="color: #000000;"><span style="color: #000000;">(BOOL finished) {[self rotatelabelup]; }];}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Rotatelabelup{[UIView animatewithduration:</span></span><span style="color: #800080;"><span style="color: #800080;">0.5</span></span>animations:^<span style="color: #000000;"><span style="color: #000000;">{self.label.transform</span></span>= Cgaffinetransformmakerotation (<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); } completion:</span></span>^<span style="color: #000000;"><span style="color: #000000;">(BOOL Finished) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(animate) {[self rotatelabeldown]; } }];}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">program is going to become</span> inactive</span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Applicationwillresignactive{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">vc:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, Nsstringfromselector (_cmd)); Animate</span></span>=<span style="color: #000000;"><span style="color: #000000;">NO;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">program becomes active</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Applicationdidbecomeactive{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">vc:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, Nsstringfromselector (_cmd)); Animate</span></span>=<span style="color: #000000;"><span style="color: #000000;">YES; [self rotatelabeldown];}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">program into the background, release pictures</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Applicationdidenterbackground{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">vc:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, Nsstringfromselector (_cmd)); Self.smiley</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; Self.smileyview</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; [[nsuserdefaults standarduserdefaults] SetInteger:self.index forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Index</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">request more background time, when the program is in the background, to the system request time to continue to perform the task</span></span>UIApplication *app =<span style="color: #000000;"><span style="color: #000000;">[uiapplication sharedapplication]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">calling Beginbackgroundtaskwithexpirationhandler and Endbackgroundtask match calls is telling the system: we need more time to do something and promise to tell him when it's Done. If the system determines that we have been running for too long and decides to stop the background task, we can call the code block we provided as a parameter. </span></span>__block uibackgroundtaskidentifier taskId = [app beginbackgroundtaskwithexpirationhandler:^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Background Task ran</span> out of time and was Terminated.</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); [app endbackgroundtask:taskid]; }]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if a special value of Uibackgroundtaskinvalid is returned, the system does not give us any extra time</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(taskId = =<span style="color: #000000;"><span style="color: #000000;">Uibackgroundtaskinvalid) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Failed to start background task!</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default,</span></span><span style="color: #800080;"><span style="color: #800080;">0</span></span>), ^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">starting background task with%f seconds remaining</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, app.backgroundtimeremaining); Self.smiley</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; Self.smileyview</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">simulation of a 25s process</span></span>[nsthread Sleepfortimeinterval:<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Finishing background task with%f seconds remaining</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, app.backgroundtimeremaining); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">tell the system that we've done the work we requested for extra time</span> .</span><span style="color: #000000;"><span style="color: #000000;">[app endbackgroundtask:taskid]; });}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">loading pictures When the program returns from the background</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Applicationwillenterforeground{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">vc:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, Nsstringfromselector (_cmd)); NSString</span></span>*smileypath = [[nsbundle mainbundle] pathforresource:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Smiley</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>OfType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">PNG</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.smiley</span></span>=<span style="color: #000000;"><span style="color: #000000;">[UIImage imagewithcontentsoffile:smileypath]; Self.smileyView.image</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.smiley;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">whenever the user changes the selected segment, the value of the Index property is updated</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) selectionchanged: (uisegmentedcontrol *<span style="color: #000000;"><span style="color: #000000;">) Sender{self.index</span></span>=<span style="color: #000000;"><span style="color: #000000;">sender.selectedsegmentindex;}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) didreceivememorywarning {[super didreceivememorywarning]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Dispose of any resources the can be Recreated.</span></span><span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p></p></p><p><p>Proficient in iOS development-gcd and background processing</p></p></span>

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.