raman raman - 11 days ago 4
iOS Question

I can't access native iOS from javascript in phonegap 2.7.0 using CDVPlugin?

I'm doing phonegap 2.7.0 web application in xcode 4.5.2 for iOS 6. In my web application contains video recording option with six seconds duration. So I done this process from "https://github.com/piemonte/PBJVision".

Now I'm trying to hybrid native iOS with Phonegap by using CDVPlugin. Because I have an image(camera icon) in homescreen.html, when I click the image, the image tag onclick() fire and call the -(void) _setup() method in PBJViewController.m file. So the mainscreen.html was hide and show the camera option with some custom design's.

So, I put PBJViewController.h & PBJViewController.m, PBJStrobeView.h & PBJStrobeView.m, PBJVisionUtilities.h & PBJVisionUtilities.m, and last PBJVision.h & PBJVison.m files in plugins folder.

Then I create a JavaScript files and named as PBJViewController.js and add into the www folder.

PBJViewController.js

function PBJViewController()
{

}


loadingalert.prototype._setup = function()
{
//this.resultCallback(res);
cordova.exec("PBJViewController._setup");
}


cordova.addConstructor(function() {

if(!window.plugins)
{
window.plugins = {};
}
//window.plugins.loadingalert = new PBJViewController();
window.loadingalert = new PBJViewController();
});


And then I wrote calling code in function video() at homescreen.html file.

homescreen.html

function video()
{
alert("video function");

$('#loader').hide();
$('#divcontroller').hide();
$('#menuid').hide();
$('#home_feeds').hide();

loadingalert._setup();
//window.plugins.loadingalert._setup();



//objloading._setup();
//window.location = "js-call:myObjectiveCFunction";
//window.location.href='video.html?user_id='+userid+'&fb_token='+getValue("fb_token");
}


And I made some changes in PBJViewController.h file

PBJViewController.h

#import <UIKit/UIKit.h>
#import <Cordova/CDVPlugin.h>

@interface PBJViewController : CDVPlugin <UIWebViewDelegate>
{
IBOutlet UIWebView *webView;

}

-(void)_setup:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options;

@end


And In PBJViewcontroller.m file

#import "PBJViewController.h"
#import "PBJVision.h"
#import "PBJStrobeView.h"
#import <Cordova/CDVPlugin.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MediaPlayer/MediaPlayer.h>

@interface UIButton (ExtendedHit)

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;

@end

@implementation UIButton (ExtendedHit)

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect relativeFrame = self.bounds;
UIEdgeInsets hitTestEdgeInsets = UIEdgeInsetsMake(-35, -35, -35, -35);
CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, hitTestEdgeInsets);
return CGRectContainsPoint(hitFrame, point);
}

@end

@interface PBJViewController () <
UIGestureRecognizerDelegate,
PBJVisionDelegate,
UIAlertViewDelegate>
{
UIView *view;
PBJStrobeView *_strobeView;
UIButton *_doneButton;
UIButton *_flipButton;

UIView *_previewView;
AVCaptureVideoPreviewLayer *_previewLayer;
MPMoviePlayerViewController *playerController;
MPMoviePlayerController *moviePlayer;
UILabel *_instructionLabel;
UILabel *ticker;
NSTimer *timer;
//NSDate *startDate;
NSTimeInterval startDate;
int hours, minutes, seconds;
int secondsLeft;
int counter;
int timeRemaining;
int i;
NSTimeInterval elapsedTime;


UILongPressGestureRecognizer *_longPressGestureRecognizer;
BOOL _recording;
BOOL running;

ALAssetsLibrary *_assetLibrary;
__block NSDictionary *_currentVideo;


//NSTimeInterval coding..

int currMinute;
int currSecond;
int currHour;
int mins;
NSTimeInterval secondsAlreadyRun;
NSTimeInterval startTime;
NSDate *startDate1;


}
//@property (nonatomic, retain) IBOutlet UIView *containerView;
@property (strong, nonatomic) MPMoviePlayerController *moviePlayerController;
@property (nonatomic, strong) NSString *videoPath;

@end

@implementation PBJViewController

//@synthesize containerView;
@synthesize moviePlayerController;
@synthesize videoPath;

#pragma mark - init


/*- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//counter = 0;
//ticker.text = [NSString stringWithFormat:@"%d", counter];

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_assetLibrary = [[ALAssetsLibrary alloc] init];
//[self _setup];
}
return self;
}*/

- (CDVPlugin *)initWithWebView:(UIWebView *)theWebView
{
self = (PBJViewController *)[super initWithWebView:theWebView];

if (self) {
_assetLibrary = [[ALAssetsLibrary alloc] init];
}

return self;
}


/*-(void) viewDidLoad
{
webView = [[UIWebView alloc] init];
// Register the UIWebViewDelegate in order to shouldStartLoadWithRequest to be called (next function)
webView.delegate = self;

}*/


- (void)dealloc
{
[UIApplication sharedApplication].idleTimerDisabled = NO;
_longPressGestureRecognizer.delegate = nil;

[super dealloc];
}

//- (void)_setup
-(void)_setup:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options;
{
NSLog(@"Yes inheriting from homescreen btn click");
running = false;
secondsLeft = 67;
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
view.backgroundColor = [UIColor blackColor];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

CGFloat viewWidth = CGRectGetWidth(view.frame);

// done button
_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
_doneButton.frame = CGRectMake(viewWidth - 20.0f - 20.0f, 20.0f, 20.0f, 20.0f);

UIImage *buttonImage = [UIImage imageNamed:@"capture_yep"];
[_doneButton setImage:buttonImage forState:UIControlStateNormal];

[_doneButton addTarget:self action:@selector(_handleDoneButton:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:_doneButton];

// elapsed time and red dot
_strobeView = [[PBJStrobeView alloc] initWithFrame:CGRectZero];
CGRect strobeFrame = _strobeView.frame;
strobeFrame.origin = CGPointMake(15.0f, 15.0f);
_strobeView.frame = strobeFrame;
[view addSubview:_strobeView];

// preview
_previewView = [[UIView alloc] initWithFrame:CGRectZero];
_previewView.backgroundColor = [UIColor blackColor];
CGRect previewFrame = CGRectZero;
previewFrame.origin = CGPointMake(0, 60.0f);
CGFloat previewWidth = view.frame.size.width;
previewFrame.size = CGSizeMake(previewWidth, previewWidth);
_previewView.frame = previewFrame;

// add AV layer
_previewLayer = [[PBJVision sharedInstance] previewLayer];
CGRect previewBounds = _previewView.layer.bounds;
_previewLayer.bounds = previewBounds;
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_previewLayer.position = CGPointMake(CGRectGetMidX(previewBounds), CGRectGetMidY(previewBounds));
[_previewView.layer addSublayer:_previewLayer];
[view addSubview:_previewView];

// instruction label
_instructionLabel = [[UILabel alloc] initWithFrame:view.bounds];
_instructionLabel.textAlignment = NSTextAlignmentCenter;
_instructionLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:15.0f];
_instructionLabel.textColor = [UIColor whiteColor];
_instructionLabel.backgroundColor = [UIColor blackColor];
_instructionLabel.text = NSLocalizedString(@"Touch and hold to record", @"Instruction message for capturing video.");
[_instructionLabel sizeToFit];
CGPoint labelCenter = _previewView.center;
labelCenter.y += ((CGRectGetHeight(_previewView.frame) * 0.5f) + 35.0f);
_instructionLabel.center = labelCenter;
[view addSubview:_instructionLabel];

ticker = [[UILabel alloc] initWithFrame:CGRectMake(230, 100, 80, 20)];
ticker.textAlignment = NSTextAlignmentLeft;
ticker.font = [UIFont fontWithName:@"HelveticaNeue" size:15.0f];
ticker.textColor = [UIColor whiteColor];
ticker.backgroundColor = [UIColor blackColor];
ticker.text = @"00:00:00";
[_previewView addSubview:ticker];


// press to record gesture
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
_longPressGestureRecognizer.delegate = self;
_longPressGestureRecognizer.minimumPressDuration = 0.0f;
_longPressGestureRecognizer.allowableMovement = 10.0f;
[_longPressGestureRecognizer addTarget:self action:@selector(_handleLongPressGestureRecognizer:)];

// gesture view to record
UIView *gestureView = [[UIView alloc] initWithFrame:CGRectZero];
CGRect gestureFrame = view.bounds;
gestureFrame.origin = CGPointMake(0, 60.0f);
gestureFrame.size.height -= 10.0f;
gestureView.frame = gestureFrame;
[view addSubview:gestureView];
[gestureView addGestureRecognizer:_longPressGestureRecognizer];

// flip button
_flipButton = [UIButton buttonWithType:UIButtonTypeCustom];

UIImage *flipImage = [UIImage imageNamed:@"capture_flip"];
[_flipButton setImage:flipImage forState:UIControlStateNormal];

CGRect flipFrame = _flipButton.frame;
flipFrame.size = CGSizeMake(25.0f, 20.0f);
flipFrame.origin = CGPointMake(10.0f, CGRectGetHeight(view.bounds) - 10.0f);
_flipButton.frame = flipFrame;

[_flipButton addTarget:self action:@selector(_handleFlipButton:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:_flipButton];
}

#pragma mark - view lifecycle



- (void)viewWillAppear:(BOOL)animated
{

[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

[self _resetCapture];
[[PBJVision sharedInstance] startPreview];
}


Here I face the warning message 'CDVPlugin' may not respond to 'viewWillAppear:' because I replace the CDVPlugin for UIViewController "@interface PBJViewController : CDVPlugin "

And I include the plugins name and value in config.xml file, there is,

config.xml



These are one of my sample code. My problem is till not get the desired output.

Kindly give some suggestion or result.

Answer

You can try to call plugin method like this cordova.exec(null, null, "PBJViewController", "_setup", [])