1. First Develop plugin: because I need to do before the project (according to the situation)
Create a new obj C file in the project's plugins file. Such as
Demo, this will produce two files of Demo.h and DEMO.M.
The. h file is primarily defined by a number of methods, similar to interfaces in Java. (to inherit Cdvplugin)
The. m file is an implementation of the H folder, which enters the appropriate function when the plug-in executes, keeping in mind that when the function is easy to CEO, Uithread is in a blocked state. No, we can start a thread in the function, the function of the start line is as follows:
Java code
- Nsthread *thread=[[nsthread alloc]initwithtarget:selft selector:@selector (doinbackground:) object:argumetns];
- Doinbackground is the method to be executed in the new thread
- [Thread start];
Nsthread *thread=[[nsthread alloc]initwithtarget:selft selector: @selector (doinbackground:) object:argumetns];// Doinbackground is the method to be executed in the new thread [thread start];
I have a few simple code here:
Java code
- #Import<foundation/foundation.h>
- #Import<cordova/cdvplugin.h>
- @Interface Displaynumber:cdvplugin
- -(void) Setnumber: (cdvinvokeurlcommand) command;
- @end;
#import <Foundation/Foundation.h> #import <Cordova/CDVPlugin.h> @Interface displaynumber:cdvplugin-( void) Setnumber: (cdvinvokeurlcommand) command; @end;
2. Enable plugins in CONFIG.
Add <feature name= "Demo" >
<param name= ' ios-package ' value= ' Demo '/>
</feature>
Here's a note: value is the class name we defined earlier, the name in the face feature refers to the name of the plug-in to be called before we write JS, if you do not understand, write a name written in the same line. (That's what I did)
3 Edit Write Plugin js
Java code
- var demo=function () {
- }
- demo.prototype={
- Method:function (fun1,fun2,params) {cordova.exec (fun1//Successful call, FUN2, ' plug-in name ', ' method name of plugin ', [params//parameter array]);
- }
- }
var demo=function () { } demo.prototype={ method:function (fun1,fun2,params) {cordova.exec (called when fun1//succeeds, fun2, ' plugin name ', ' method name of plugin ', [params//parameter array]);}}
If we want to use the demo plugin, we can simply write the new demo (). method (Fun1,fun2,params);//Very simple
Note: We can also in the plugin JS in the new Demo () to a variable, we will not call again when the new one.
About the solution of unlimited operation in the background (there are many solutions on the web)
1. info.plist file added: Required Background modes (is an array form of the built value), after the item0 of the value set to become the APP plays audio or streams audio/video using A Irplay.
2. Locate MainViewController.h under the Classes folder,
Java code
- #Import <Cordova/CDVViewController.h>
- #Import <Cordova/CDVCommandDelegateImpl.h>
- #Import <Cordova/CDVCommandQueue.h>
- #Import <AVFoundation/AVFoundation.h>
- @interface mainviewcontroller:cdvviewcontroller{
- Avaudioplayer *audioplayer;
- }
- @property (nonatomic) avaudioplayer * audioplayer;
- @end
- @interface Maincommanddelegate:cdvcommanddelegateimpl
- @end
- @interface Maincommandqueue:cdvcommandqueue
- @end
#import <Cordova/CDVViewController.h> #import <Cordova/CDVCommandDelegateImpl.h> #import <cordova/ cdvcommandqueue.h> #import <AVFoundation/AVFoundation.h> @interface mainviewcontroller:cdvviewcontroller{ Avaudioplayer *audioplayer;} @property (nonatomic) avaudioplayer * audioplayer; @end @interface maincommanddelegate: Cdvcommanddelegateimpl@end@interface Maincommandqueue:cdvcommandqueue@end
Then modify the mainviewcontroller.m file to find the Viewdidload method, modified to:
Java code
- -(void) Viewdidload
- {
- [Super Viewdidload];
- additional setup after loading the view from its nib.
- dispatch_queue_t dispatchqueue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
- Dispatch_async (Dispatchqueue, ^ (void) {
- Nserror *audiosessionerror = nil;
- Avaudiosession *audiosession = [Avaudiosession sharedinstance];
- if ([Audiosession setcategory:avaudiosessioncategoryplayback Error:&audiosessionerror]) {
- NSLog (@"successfully set the audio session.");
- } Else {
- NSLog (@"Could not set the audio session");
- }
- NSBundle *mainbundle = [NSBundle mainbundle];
- NSLog (@"%@", Mainbundle);
- NSString *filepath = [Mainbundle pathforresource:@"Love" oftype:@"wav"];
- NSData *filedata = [NSData Datawithcontentsoffile:filepath];
- Nserror *error = nil;
- NSLog (@"aa%@", FilePath);
- Self.audioplayer = [[Avaudioplayer alloc] Initwithdata:filedata error:&error];
- if (self.audioplayer! = nil) {
- Self.audioPlayer.delegate = self;
- [Self.audioplayer setnumberofloops:-1];
- if ([Self.audioplayer preparetoplay] && [Self.audioplayer Play]) {
- NSLog (@"successfully started playing ...");
- } Else {
- NSLog (@"Failed to play.");
- }
- } Else {
- NSLog (@"Failed to play.");
- }
- });
- }
-(void) viewdidload{[Super Viewdidload]; Do any additional setup after loading the view from its nib. dispatch_queue_t dispatchqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0); Dispatch_async (dispatchqueue, ^ (void) {nserror *audiosessionerror = nil; Avaudiosession *audiosession = [Avaudiosession sharedinstance]; if ([Audiosession setcategory:avaudiosessioncategoryplayback Error:&audiosessionerror]) {NSLog (@ "Successful Ly set the audio session. "); else {NSLog (@ "Could not set the audio session"); } nsbundle *mainbundle = [NSBundle mainbundle]; NSLog (@ "%@", Mainbundle); NSString *filepath = [Mainbundle pathforresource:@ "Love" oftype:@ "wav"]; NSData *filedata = [NSData Datawithcontentsoffile:filepath]; Nserror *error = nil; NSLog (@ "aa%@", FilePath); Self.audioplayer = [[Avaudioplayer alloc] Initwithdata:filedata error:&error]; if (self.audioplayer! = nil) {self.audioPlayer.delegate = self; [Self.audioplayer setnumberofloops:-1]; if ([Self.audioplayer preparetoplay] && [Self.audioplayer Play]) {NSLog (@ "successfully started play ing ... "); } else {NSLog (@ "Failed to play.");}} else {NSLog (@ "Failed to play.");});
Description: The Love.wav file is a file under other sources.
Then modify the APPDELEGATE.M file, new method:
Java code
- -(void) Applicationdidenterbackground: (uiapplication *) application{
- [Nsrunloop Currentrunloop];
- //
- UIApplication *app=[uiapplication Sharedapplication];
- __block Uibackgroundtaskidentifier Bgtask;
- Bgtask=[app beginbackgroundtaskwithexpirationhandler:^{
- Dispatch_async (Dispatch_get_main_queue (), ^{
- if (bgtask!=uibackgroundtaskinvalid) {
- Bgtask=uibackgroundtaskinvalid;
- // }
- // });
- // }];
- //
- Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
- Dispatch_async (Dispatch_get_main_queue (), ^{
- if (bgtask!=uibackgroundtaskinvalid) {
- Bgtask=uibackgroundtaskinvalid;
- // }
- // });
- // });
- //
- [[UIApplication sharedapplication] setkeepalivetimeout:600 handler:^{
- NSLog (@ "KeepAlive");
- // }];
- Mainviewcontroller *mvc=[[mainviewcontroller alloc] init];
- [MVC viewdidload];
- }
-(void) Applicationdidenterbackground: (uiapplication *) application{//[Nsrunloop currentrunloop];////uiapplication *app=[uiapplication sharedapplication];//__block UIBackgroundTaskIdentifier bgtask;//Bgtask=[app beginbackgroundtaskwithexpirationhandler:^{//Dispatch_async (Dispatch_get_main_queue (), ^{ if (bgtask!=uibackgroundtaskinvalid) {//bgtask=uibackgroundtaskinvalid;//}// });//}];////Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{//dispatch_ Async (Dispatch_get_main_queue (), ^{//if (bgtask!=uibackgroundtaskinvalid) {//BGTASK=UIBACKGROUNDTA skinvalid;//}//});///[[UIApplication sharedapplication] setkeepalivetimeout:600 Handler : ^{//NSLog (@ "KeepAlive");/}]; Mainviewcontroller *mvc=[[mainviewcontroller alloc] init]; [MVC viewdidload];}
There are also many online, found under the simulator can be long-running, but under the real-time machine does not run. Find it a bit better to play a silent audio file for a long time.
-------------------If there is any bad place, please advise.
PhoneGap iOS plugin development and unlimited background operation solution