From 2015, access to the project, there will be the demand: The app needs to be like Android, in the background state, to perform normal functions. It has been more than a year now, has been studying this aspect, write down some ideas, hope to share with you to discuss.
First of all, we need to know that Apple is very strict about the use of hardware resources in the app, not to mention the use of resources in the background. Under normal circumstances, when using the app, the app loads from the hard drive into memory, starts working, and when the user presses the home key, the app is suspended and still resides in memory, and in this state, the program does not run if several back-end methods that Apple has opened are not invoked, and if the program continues to run at this time, the background state If the current memory is not enough, the system will automatically put out the memory in the previous suspended state of the app. So we see that sometimes when we open the app, it's the data from the last page when we left it, and occasionally it comes back from the splash screen.
In this way, you know that the background runs the maximum pre-condition--app in-memory pending state.
Then, take a look at the above-mentioned Apple's open background running method. Look at this picture first.
Obviously, the mechanism we can use in our project is so much, Background audio, this is the backstage sound, this very early has, can realize the background of the voice playback. Last year's project used it to play in the background without sound files, the results of audit failed.
Here's a look at the needs of the project that was done last year, user type A can view the location of user type B at any time. This function is a bit like "find a friend" on the iphone, do not know the friend please understand (want to know where your friend, want to know where your other half is, right, use it); a want to see B, b need to upload their current location to the server, regardless of the app in the suspended state how to do, First say the app in the active state, the server wants to communicate with the client, tell the client to upload their own location, this server active communication, commonly used is the socket and push notifications. I decided to use a push to locate and upload the app when it received a push from APNs.
However, when you press the home key to enter a pending state, the program does not execute, and you also get the location of B. Boss is very annoyed, Android minutes to do things, you can not make it (brain tonic: Can not get out on the fuck off).
At that time, the first contact with Apple these back-end mechanisms, the path of exploration is tortuous, there is a statement. Finally, the problem was solved with a silent push: Remote notification! The principle is very simple, but Apple's original intention is not to let me use the ..., say the application scenario of this mechanism: the previous Chat class application to accept push after the point in need to collect information, this situation in QQ, and other applications most obvious. However, with this interface, this situation will no longer exist, the push will be able to directly start the background task, in the background has received information, click the app does not need to pull.
So, when a view B position, give B a silent push, b in the background to locate and upload information. This recall time is relatively short, in about 3-5 seconds, sometimes B network is not good, no upload success is suspended, it needs to be repeated. This mechanism adds methods and pushes, with only a little difference, that is, the delegate method is different. The normal push executes this callback:
1 -(void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary * ) UserInfo {23 ddlogdebug (@ "[normal push]%@", userInfo); 4 }
And by ticking the above push-wake, this method will be recalled:
1 -(void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfo Fetchcompletionhandler: (void (^) (Uibackgroundfetchresult)) Completionhandler {2 Ddlogdebug (@ "[background push]%@", userInfo); 3 Completionhandler (uibackgroundfetchresultnewdata); 4 }
You can call the location request in this callback.
This thought solved the problem, boss tried, also feel can. On the exasperate waiting for the promotion of salary to the pinnacle of life, cough, and dream.
"What's the matter, ah, now is not in place, hurriedly do well" has not yet two hours, boss angrily ran over to spray a meal. After 0.01 seconds, I knew what was going on, "Listen to me explain Ah, the boss" did not say, he broke the door, leaving me to cry without tears. There are only two reasons, one, the app was cut off artificially; second, the app was collected by the system, kill.
On the sauce, knowing, undeterred. In order not to allow the system to recycle the app, I was very tough, plus background Audio. The results can be imagined, was rejected at the same time, received an English e-mail, asked why I do so, if there are objections, may be raised. Well, finally relieved, can leave (personal reasons).
Change the company, the demand is not the same, the app needs every five seconds and the server to Exchange data, the following is the use of VoIP.
At first, the push-wake mechanism is also possible, but the woods big what birds have, the same model settings, there is an iphone is not wake up, can only try new methods. Think of QQ voice, cut to the background can still call, I think, is it. VoIP: Background voice service, similar to Skype call application needs to call, can make voice calls in the background. Since it is a voice call, then it must be often connected, so, with the following code.
1 @implementationNsstream (streamstohost)2 3+ (void) getstreamstohostnamed: (NSString *) HostName4 Port: (UInt32) Port5InputStream: ( out__strong Nsinputstream * *) Inputstreamptr6OutputStream: ( out__strong Nsoutputstream * *) Outputstreamptr7 {8 cfreadstreamref Readstream;9 cfwritestreamref Writestream;Ten OneASSERT (HostName! =nil); AASSERT (Port >0) && (Port <65536) ); -ASSERT ((inputstreamptr! = NULL) | | (Outputstreamptr! =NULL)); - theReadstream =NULL; -Writestream =NULL; - - Cfstreamcreatepairwithsockettohost ( + NULL, - (__bridge cfstringref) HostName, + Port, A((inputstreamptr! = NULL)? &readstream:null), at((outputstreamptr! = NULL)? &writestream:null) - ); - - if(Inputstreamptr! =NULL) { -*inputstreamptr =cfbridgingrelease (readstream); - } in - if(Outputstreamptr! =NULL) { to*outputstreamptr =cfbridgingrelease (writestream); + } - } the * @end
added a category to the Nsstream. Then I need a server, I will not write; Initiate a connection request to keep the client communicating with the server, which is too much to post. When the Voice over IP is checked, the app suspends the socket session handle and calls the app into the background code when it receives a stream of data from the server. This evoke time is longer, can be more than 10 seconds. Has been tested successfully, but has not submitted the audit, but also need to give it a coat, or as the last time was rejected.
has been exploring, because the above method is not perfect, and the requirements of the background of the project is more stringent, in fact, the user in the use of the app, there will be many scenes, the most common is the weak network, in this scenario, whether it is pushed back to the socket can not receive content, so like this need to rely on external calls, The malpractice is still quite obvious.
Have felt the back of the relatively hasty writing, VoIP related content is still more, there is no one by one thoroughly understand, or impatient. Personal knowledge is limited, if there are errors, please correct me.
iOS background operating mechanism-practice summary