uiwebview crash while executing javascript event on save button

In our app we have one jsp page which we are loading in uiwebview.in jsp page after selecting some files ,clicking on save button application crashes using exc_bad_access.When i tried to debug the issue with zoombieenabled, i got this

-[CALayerArray countByEnumeratingWithState:objects:count:]: message sent to deallocated instance 0x17564d9b0.


below is the code which i am using,

#import "FormCreateViewController.h"

#import "DCWiseURLManager.h"

#import "AsiteUserDataManager.h"

#import "NetworkAvailibilityManager.h"

#import "EventConstants.h"

extern AsiteUserDataManager* gpUserDataManager;

const int FCV_Offset = 15;

@interface FormCreateViewController ()

@property BOOL isLoaded;

@property (retain) UIWebView *detailWebview;

@property (retain) UIActivityIndicatorView *activityIndicatorView;

@end

@implementation FormCreateViewController

@synthesize isLoaded;

@synthesize createFormURL;

@synthesize detailWebview;

@synthesize titleLable;

@synthesize activityIndicatorView;

@synthesize detailDelegate;

@synthesize pstrProjectId;

@synthesize pstrFormTypeId;

@synthesize pPopoverImageController;

@synthesize pImagePickerController;

@synthesize pActionSheetForImageUpload;

@synthesize xPosition;

@synthesize yPosition;

@synthesize iAttachmentNumer;

@synthesize pAttachmentDictionary;

- (void)viewDidLoad {

[super viewDidLoad];

iAttachmentNumer =0;

pAttachmentDictionary = [[NSMutableDictionary alloc] init];

self.isLoaded = NO;

self.detailWebview = [[UIWebView alloc] init];

self.detailWebview.scrollView.minimumZoomScale = 0.2f;

self.detailWebview.scrollView.maximumZoomScale = 10.0f;

[self.detailWebview.scrollView setBouncesZoom:YES];

self.detailWebview.scrollView.scrollEnabled = YES;

self.detailWebview.scrollView.bounces = YES;

self.detailWebview.delegate = self;

[self.view addSubview:self.detailWebview];


UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTest:)];

[self.detailWebview setUserInteractionEnabled:YES];

tap.numberOfTapsRequired = 1;

[tap setDelegate:self];


/


[self.detailWebview addGestureRecognizer:tap];

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return YES;

}

-(void)tapTest:(UITapGestureRecognizer *)sender

{

self.xPosition = [sender locationInView:self.detailWebview].x;

self.yPosition = [sender locationInView:self.detailWebview].y;


}

-(void) SetParentItemTitle

{

/

}

-(void)viewDidDisappear:(BOOL)animated

{

[self StopActivityIndicator];

if([self isMovingFromParentViewController])

{

/

}

}

-(NSString*) GetActionName :(id) objDictionary

{

NSString* actionName = NULL;

id actionsJsonArray = [objDictionary objectForKey:@"actions"];

if(actionsJsonArray != nil)

{

if([actionsJsonArray isKindOfClass:[NSArray class]])

{

NSArray *actionInfoList = actionsJsonArray;

NSDictionary *actionInfoMap = [actionInfoList objectAtIndex:0];

if(actionInfoMap != nil)

{

actionName = [actionInfoMap objectForKey:@"actionName"];

if(actionName == nil)

{

actionName = @"";

}

}

}

}

return actionName;

}

-(NSURLRequest*) getFormDetailViewReqeustForObject:(id)dataObject

{


NSString *projId = [[[dataObject objectForKey:@"projectId"] componentsSeparatedByString:@"$$"] objectAtIndex:0];

DCWiseURLManager *dcwiseURLManager = [DCWiseURLManager getInstance];

DCWiseURLsInfo *urlInfo = [dcwiseURLManager getDcURLSInfoForProjectId:projId];

NSString *adoddleURL = gpUserDataManager.adoddleURL;

NSString *strURL = [NSString stringWithFormat:@"%@/%@",adoddleURL, @"adoddle/viewer/mobileapp/view.jsp?"];


/

strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSURL* requestURL = [NSURL URLWithString:strURL];


NSString *projectIdList = [gpUserDataManager GetFilteredProjectIdListString];

/

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

[request setHTTPShouldHandleCookies:NO];

[request setTimeoutInterval:gpUserDataManager.timeOutInterval];

[request setHTTPMethod:@"POST"];


[request addValue:[@"ASessionID=" stringByAppendingString:gpUserDataManager.pstrSessionID] forHTTPHeaderField:@"Cookie"];


NSMutableString* request_body = [[NSMutableString alloc] init];

[request setURL:requestURL];


NSString* pstrWidth = [NSString stringWithFormat:@"%f", self.detailWebview.frame.size.width];


NSString* pstrHeight = [NSString stringWithFormat:@"%f", self.detailWebview.frame.size.height - 35];

NSMutableDictionary *docDictionary = dataObject;


[request_body appendFormat:@"&projectId=%@",[docDictionary objectForKey:@"projectId"]];



[request_body appendFormat:@"&formID=%@",[docDictionary objectForKey:@"formId"]];

[request_body appendFormat:@"&formTypeId=%@",[docDictionary objectForKey:@"formTypeId"]];


[request_body appendFormat:@"&folderId=%@",[docDictionary objectForKey:@"folderId"]];


[request_body appendFormat:@"&dcId=%@",[docDictionary objectForKey:@"dcId"]];


[request_body appendFormat:@"&statusId=%@",[docDictionary objectForKey:@"statusId"]];


[request_body appendFormat:@"&actions=%@",@""];/


[request_body appendFormat:@"&parentmsgId=0"];


[request_body appendFormat:@"&msgTypeCode=%@",[docDictionary objectForKey:@"msgTypeCode"]];


[request_body appendFormat:@"&msgCode=%@",[docDictionary objectForKey:@"msgCode"]];


[request_body appendFormat:@"&isFromApps=true"];


[request_body appendFormat:@"&originatorId=%@",[docDictionary objectForKey:@"originatorId"]];


[request_body appendFormat:@"&msgId=%@",[docDictionary objectForKey:@"msgId"]];


if([docDictionary objectForKey:@"isDraft"] == 0)

[request_body appendFormat:@"&isDraft=true"];

else

[request_body appendFormat:@"&isDraft=false"];


[request_body appendFormat:@"&commId=%@",[docDictionary objectForKey:@"commId"]];


[request_body appendFormat:@"&ios=%@",@"true"];

[request_body appendFormat:@"&isNewUI=%@",@"true"];

[request_body appendFormat:@"&isAndroid=%@",@"true"];

[request_body appendFormat:@"&callFor=%@",@"viewForm"];


/

[request_body appendFormat:@"&applicationId=%@",@"3"];

[request_body appendFormat:@"&projectids=%@",projectIdList];

@try

{

[AsiteUserDataManager StoreSharedCookieWithName:@"ASessionID" withValue:gpUserDataManager.pstrSessionID:adoddleURL];

}

@catch (NSException *exception) {

}


NSString *postLength = [NSString stringWithFormat:@"%d", [request_body length]];


[request addValue:postLength forHTTPHeaderField:@"Content-Length"];

[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request addValue:@"*/" forHTTPHeaderField:@"Accept"];


/

[request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]];


return request;

}

-(void)viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

[[NSNotificationCenter defaultCenter] postNotificationName:DETAIL_VIEW_OPENED_EVENT_NAME object:nil];


if(self.isLoaded == NO)

{

iAttachmentNumer =0;

[self UpdateView];

[self StartActivityIndicator];

self.isLoaded = YES;

BOOL isOnline = [[NetworkAvailabilityManager getNetworkAvailManagerInstance] hasNetworkConnection];

/

if(isOnline)

{

NSURLRequest *urlRequest = self.createFormURL;

[self.detailWebview loadRequest:urlRequest];

[self SetParentItemTitle];

}

else

{

[self SendGetCreateFormViewSubmissionData:self.pstrFormTypeId];

/

}

}



}

/

-(void) ShowOfflineCreateForm:(NSData*) pData

{


NSString *pstr = [[NSString alloc] initWithData:pData encoding:NSUTF8StringEncoding];

NSString* pstrOfflineJSDir = [gpUserDataManager.pstrAppRootFolderPath stringByAppendingPathComponent:@"OfflineJSFolder"];

[self.detailWebview loadHTMLString:pstr baseURL:[NSURL fileURLWithPath:pstrOfflineJSDir]];

[self SetParentItemTitle];


}

/

-(void) SendGetCreateFormViewSubmissionData :(id) object

{

NSString * jsonString = [self GetCreateFormViewSubmissionData:@"GetOfflineFormCreateData" :object];

TaskHelper::getInstance()->SubmitTask(@"GetOfflineFormCreateData",self,jsonString);

}

-(NSString*) GetCreateFormViewSubmissionData:(NSString*)pstrTaskId : (id) object

{

BOOL isOnline = NO;/

NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc] init];

[jsonDictionary setObject:@"2" forKey:@"BUSINESS_CATEGORY"];

[jsonDictionary setObject:@"2" forKey:@"BUSINESS_TASK_TYPE"];

[jsonDictionary setObject:pstrTaskId forKey:@"TASK_ID"];


NSMutableDictionary *pTaskData = [[NSMutableDictionary alloc] init];

NSMutableDictionary *pSessionData = [[NSMutableDictionary alloc] init];

[ pSessionData setObject:gpUserDataManager.pstrSessionID forKey:@"ASessionID"];


NSMutableArray *pCookieArray = [[NSMutableArray alloc] init];

[pCookieArray addObject:pSessionData];

[pTaskData setObject:pCookieArray forKey:@"COOKIES"];

[pTaskData setObject:@"9" forKey:@"OPERATION_TYPE"];

[pTaskData setObject: [NSNumber numberWithBool:isOnline] forKey:@"isOnline"];


NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] init];



[dataDictionary setObject: gpUserDataManager.pstrSessionID forKey:@"ASessionID"];

[dataDictionary setObject: object forKey:@"formTypeId"];


[pTaskData setObject: dataDictionary forKey:@"DATA"];

[jsonDictionary setObject:pTaskData forKey:@"TASK_DATA"];


NSError *error = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary

options:0

error:&error];

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

return jsonString;


}

#pragma mark - TaskHelperDelegate methods

-(void) TaskCompletedWithData:(NSString*)responseString

{


NSData* data = [responseString dataUsingEncoding:NSUTF8StringEncoding];


if(NSClassFromString(@"NSJSONSerialization"))

{

NSError *error = nil;

id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

if(error)

{

/ JSON was malformed, act appropriately here */

}

if([object isKindOfClass:[NSDictionary class]])

{

NSDictionary *results = object;

NSString *taskType = [results objectForKey:@"TASK_ID"];

if([taskType isEqualToString:@"SaveCreateFormData"])

{

[self performSelectorOnMainThread:@selector(reloadTable) withObject:nil waitUntilDone:YES];

}

else

{

id obj = [results objectForKey:@"RESPONSE_DATA"];

if([obj isKindOfClass:[NSDictionary class]])

{

NSDictionary *res = obj;

NSString* pstrAppsList = [res objectForKey:@"DATA"];

NSData* dataNew = [pstrAppsList dataUsingEncoding:NSUTF8StringEncoding];

[self ShowOfflineCreateForm:dataNew];

}

}

}

}


}

/

-(void)reloadTable

{

[self.detailDelegate formSaved];

}

-(void) HandleJSPCommands:(NSString*)command :(NSString*)pstrInfoDetails

{

/

if([command caseInsensitiveCompare:@"fireLogoutMethod"] == NSOrderedSame)

{

[Helper DoLogout:self.view];

}

else if([command caseInsensitiveCompare:@"formSaved"] == NSOrderedSame)

{

self.detailWebview.delegate = nil;

[self.detailDelegate formSaved];

[gpUserDataManager refreshNotificationCount];

}

if([command caseInsensitiveCompare:@"autoDraftCreated"] == NSOrderedSame)

{

}


/

if([command caseInsensitiveCompare:@"getData"] == NSOrderedSame)

{

NSLog(@"json data apps crete form is %@",gpUserDataManager.pstrJSONDataFromDB);

[self.detailWebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setData(%@)",@""]];

}

/


if([command caseInsensitiveCompare:@"FormSubmitClicked"] == NSOrderedSame)

{

}


if([command caseInsensitiveCompare:@"loadingCompleted"] == NSOrderedSame)

{

}


if([command caseInsensitiveCompare:@"cancel"] == NSOrderedSame )

{

[self.detailDelegate backButtonIsClicked];

}


if([command caseInsensitiveCompare:@"offlineFormSubmitClicked"] == NSOrderedSame)

{

[self OfflineFormSavedUsingJavaScriptMethod];

}


if([command caseInsensitiveCompare:@"offLineFormAttachmentClicked"] == NSOrderedSame)

{

[self ShowActionSheetForImageUpload];

}

if([command caseInsensitiveCompare:@"removeAttachment"] == NSOrderedSame)

{

[self RemoveAttachment:pstrInfoDetails];

}

}

-(void) RemoveAttachment:(NSString*)pstrInfoDetails

{

NSArray *stringTokens = [pstrInfoDetails componentsSeparatedByString:@"_"];

if(stringTokens.count > 1)

{

NSString* pstrIndex = [stringTokens objectAtIndex:1];

NSString* pstrAttachmentKey = [NSString stringWithFormat:@"txtAttachedPath%@",pstrIndex];

NSString* pstrImageFilePath = [pAttachmentDictionary objectForKey:pstrAttachmentKey];

[pAttachmentDictionary removeObjectForKey:pstrAttachmentKey];

NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

NSUserDomainMask, YES) objectAtIndex:0];

NSString *oldPath = [documentDir stringByAppendingPathComponent:pstrImageFilePath];

NSFileManager *fileMan = [NSFileManager defaultManager];

NSError *er = nil;

[fileMan removeItemAtPath:oldPath error:&er];

}

}

-(void) HandleJSPEventsForURLString:(NSString*)urlString

{


if([urlString containsString:@"#"])

{

NSArray *stringTokens = [urlString componentsSeparatedByString:@":"];

if(stringTokens.count > 1)

{

NSString *command = [stringTokens objectAtIndex:1];

command = [[command componentsSeparatedByString:@"#"] objectAtIndex:0];

NSArray *hashstringTokens = [urlString componentsSeparatedByString:@"#"];

NSString *pstrInfoDetails =@"NULL";

pstrInfoDetails = [hashstringTokens objectAtIndex:1];

if([command caseInsensitiveCompare:@"error"] != NSOrderedSame)

{

[self HandleJSPCommands:command: pstrInfoDetails];

}

}

}

else

{

NSArray *stringTokens = [urlString componentsSeparatedByString:@":"];

if(stringTokens.count > 1)

{

NSString *command = [stringTokens objectAtIndex:1];

NSString *pstrInfoDetails =@"NULL";

if(stringTokens.count ==3)

{

pstrInfoDetails = [stringTokens objectAtIndex:2];

}

if([command caseInsensitiveCompare:@"error"] != NSOrderedSame)

{

[self HandleJSPCommands:command: pstrInfoDetails];

}

}

}

}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

BOOL result = YES;

NSString *urlString = [request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

if([request.URL.absoluteString hasPrefix:@"js-frame:"])

{

[self HandleJSPEventsForURLString:urlString];

result = NO;

}

/

if ([request.URL.absoluteString rangeOfString:@"session_timed_out"].location != NSNotFound || [request.URL.absoluteString rangeOfString:@"session-overriden"].location != NSNotFound) {

[[NSNotificationCenter defaultCenter] postNotificationName:SESSION_EXPIRED_EVENT_NAME object:nil];

return NO;

}

return result;

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{


[self StopActivityIndicator];


}

/

-(void)webViewDidFinishLoad:(UIWebView *)webView

{

/

[self StopActivityIndicator];


}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

/

}

-(void) SetData:(id)dataArray

{


}

-(void) UpdateView

{

CGRect webViewFrame = self.view.bounds;

[self.detailWebview setFrame:webViewFrame];

}

- (IBAction)backButtonClicked:(id)sender {


[self.detailDelegate backButtonIsClicked];

}

-(void) StopActivityIndicator

{

[NSThread detachNewThreadSelector:@selector(stopAnimating) toTarget:self.activityIndicatorView withObject:nil];

}

-(void) StartActivityIndicator

{

if(self.activityIndicatorView == nil)

{

self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

CGRect indicatorFrame = self.activityIndicatorView.frame;

CGRect navigationViewFrame = self.navigationController.view.bounds;

indicatorFrame.origin.x = (navigationViewFrame.size.width - indicatorFrame.size.width)/2;

indicatorFrame.origin.y = (navigationViewFrame.size.height - indicatorFrame.size.height)/2;

[self.activityIndicatorView setFrame:indicatorFrame];

self.activityIndicatorView.color = [UIColor darkGrayColor];

[self.navigationController.view addSubview:self.activityIndicatorView];

}

[self.navigationController.view bringSubviewToFront:self.activityIndicatorView];

[NSThread detachNewThreadSelector:@selector(startAnimating) toTarget:self.activityIndicatorView withObject:nil];

}

-(void) OfflineFormSavedUsingJavaScriptMethod

{

NSString* pstrSavedXMLPath = [gpUserDataManager.pstrAppRootFolderPath stringByAppendingPathComponent:@"HML_DataCreatedForm.xml"];

NSError *error = nil;



NSString * jsCallBack = [NSString stringWithFormat:@"passDataToAndroid()"];


NSString* pstr = [self.detailWebview stringByEvaluatingJavaScriptFromString:jsCallBack];


NSData *data = [pstr dataUsingEncoding:NSUTF8StringEncoding];


id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

if (!json)

{

}


else

{

if([json isKindOfClass:[NSDictionary class]])

{

NSMutableDictionary* OfflineFormFieldJSONDictionary = [json mutableCopy];

NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];

[DateFormatter setDateFormat:@"dd-MMM-yyyy#hh:mm:ss"];

NSString* pstrCreationDate = [DateFormatter stringFromDate:[NSDate date]];

/

NSDateFormatter *DateFormatter1=[[NSDateFormatter alloc] init];

[DateFormatter1 setDateFormat:@"dd-MM-yyyy#hh:mm:ss"];

NSString* pstrCreationDate1 = [DateFormatter1 stringFromDate:[NSDate date]];

NSString* pstrTempFormId = [pstrCreationDate1 stringByReplacingOccurrencesOfString:@"#" withString:@"-"];

pstrTempFormId = [ pstrTempFormId stringByReplacingOccurrencesOfString:@":" withString:@"-"];

pstrTempFormId = [ pstrTempFormId stringByReplacingOccurrencesOfString:@":" withString:@"-"];

NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];

NSString* pstrFormId = [NSString stringWithFormat:@"%f",timeInterval];

[ OfflineFormFieldJSONDictionary setObject:pstrCreationDate forKey:@"formUpdationDate"];

NSString* pstrOfflineFormTitleTemp = @"OfflineForm";

[OfflineFormFieldJSONDictionary setObject:pstrOfflineFormTitleTemp forKey:@"OfflineFormTitle"];

[OfflineFormFieldJSONDictionary setObject:pstrCreationDate forKey:@"formCreationDate"];

[OfflineFormFieldJSONDictionary setObject:pstrFormId forKey:@"offlineFormId"];

[OfflineFormFieldJSONDictionary setObject:gpUserDataManager.pstrFormTypeId forKey:@"offlineFormTypeId"];

int iCount = (int)[self.pAttachmentDictionary count];

NSArray* pAllKeys = [self.pAttachmentDictionary allKeys];

for( int i=0; i<iCount; i++)

{

NSString* pstrKey = [pAllKeys objectAtIndex:i];

[OfflineFormFieldJSONDictionary setObject:[self.pAttachmentDictionary objectForKey:pstrKey] forKey:pstrKey];

}

NSString* pstrFormUpdatedDate = [OfflineFormFieldJSONDictionary objectForKey:@"formUpdationDate"];

NSString* pstrFormCreationDate = [OfflineFormFieldJSONDictionary objectForKey:@"formCreationDate"];

NSString* pstrOfflineFormTitle = [OfflineFormFieldJSONDictionary objectForKey:@"OfflineFormTitle"];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:OfflineFormFieldJSONDictionary

options:0

error:&error];

NSTimeInterval timeInMiliseconds = [[NSDate date] timeIntervalSince1970];

float creationDate = timeInMiliseconds*1000;

if(jsonData)

{

[self SendFormSubmissionData:gpUserDataManager.pstrAppTypeId :pstrFormId :gpUserDataManager.pstrInstanceGroupId :jsonData :nil :self.pAttachmentDictionary];

[self.detailWebview stringByEvaluatingJavaScriptFromString:@"document.body.style.background = '# FFFFFF';"];

[self.detailWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

}

}

}



}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

if(buttonIndex == (NSInteger)(-1))

{

[pActionSheetForImageUpload dismissWithClickedButtonIndex:0 animated:YES];

pActionSheetForImageUpload = nil;

return;

}


if(pActionSheetForImageUpload != nil)

{

NSString *pButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];

if([pButtonTitle isEqualToString:@"Take Photo or Video"] )

{

[self ShowImagePickerControllerForImageUpload:@"0"];

[pActionSheetForImageUpload dismissWithClickedButtonIndex:0 animated:YES];

}

if([pButtonTitle isEqualToString:@"Choose Existing"] )

{

[self ShowImagePickerControllerForImageUpload: @"1"];

[pActionSheetForImageUpload dismissWithClickedButtonIndex:1 animated:YES];

}

pActionSheetForImageUpload = nil;

}

return;

}

-(void)ButtonCancelClicked

{

[self dismissViewControllerAnimated:YES completion:nil];

}

-(void)ButtonOkCliked

{

[self dismissViewControllerAnimated:YES completion:nil];


NSString* pstrImageFileName = [self.detailWebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('%@').value;",@"txtAttachedPath"]];

NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

NSUserDomainMask, YES) objectAtIndex:0];

NSString *oldPath = [documentDir stringByAppendingPathComponent:pstrImageFileName];


NSFileManager *fileMan = [NSFileManager defaultManager];

NSError *er = nil;

[fileMan removeItemAtPath:oldPath error:&er];

/



[self.detailWebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('txtAttachedPath').innerHTML = '%@';", @""]];


[self.detailWebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('txtAttachedPath').value= '%@';", @""]];



if(pActionSheetForImageUpload != nil)

{

[pActionSheetForImageUpload dismissWithClickedButtonIndex:0 animated:YES];

pActionSheetForImageUpload = nil;

}



pActionSheetForImageUpload = [[UIActionSheet alloc] initWithTitle:nil

delegate:self

cancelButtonTitle:nil

destructiveButtonTitle:nil

otherButtonTitles:LocalizedText(@"Take Photo or Video", @""), LocalizedText(@"Choose Existing", @""), nil];



[pActionSheetForImageUpload showFromRect:CGRectMake(self.xPosition,self.yPosition,1,1) inView:[self view] animated:YES];

}


-(void) ShowFileUploadForPhone

{



UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *selectPhotoAction = [UIAlertAction actionWithTitle:@"Choose Existing"

style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {

[self ShowImagePickerControllerForImageUpload: @"1"];

}];

UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"Take Photo or Video"

style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {

[self ShowImagePickerControllerForImageUpload:@"0"];

}];

UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action)

{

}];

[alert addAction:takePhotoAction];

[alert addAction:selectPhotoAction];

[alert addAction:cancelAction];

[self presentViewController:alert animated:YES completion:nil];

}

- (void) ShowActionSheetForImageUpload

{


if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)

{

[self ShowFileUploadForPhone];

}

else

{

if(pActionSheetForImageUpload != nil)

{

[pActionSheetForImageUpload dismissWithClickedButtonIndex:0 animated:YES];

pActionSheetForImageUpload = nil;

}

pActionSheetForImageUpload = [[UIActionSheet alloc] initWithTitle:nil

delegate:self

cancelButtonTitle:nil

destructiveButtonTitle:nil

otherButtonTitles:LocalizedText(@"Take Photo or Video", @""), LocalizedText(@"Choose Existing", @""), nil];

[pActionSheetForImageUpload showFromRect:CGRectMake(self.xPosition,self.yPosition,1,1) inView:[self view] animated:YES];

}

/


}

- (void) ShowImagePickerControllerForImageUpload:(NSString*) strIndex

{


if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)

{

if(self.pImagePickerController == nil)

{

pImagePickerController = [[UIImagePickerController alloc] init];

pImagePickerController.delegate = self;

}

if([strIndex isEqualToString:@"0"])

{

[pImagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];

}

else

{

[pImagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

}

[self presentViewController:pImagePickerController animated:YES completion:nil];

return;

}

else{

if(self.pPopoverImageController == nil)

{

if(self.pImagePickerController == nil)

{

pImagePickerController = [[UIImagePickerController alloc] init];

pImagePickerController.delegate = self;

}

self.pPopoverImageController = [[UIPopoverController alloc] initWithContentViewController:pImagePickerController];

self.pPopoverImageController.delegate = self;

}

if(!self.pPopoverImageController.popoverVisible)

{

if([strIndex isEqualToString:@"0"])

{

[pImagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];

}

else

{

[pImagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

}

if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)

{

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

[self.pPopoverImageController presentPopoverFromRect:CGRectMake(self.xPosition,self.yPosition,1,1) inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}];

}

else{

[self.pPopoverImageController presentPopoverFromRect:CGRectMake(self.xPosition,self.yPosition,1,1) inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

return;

}

else

{

[self.pPopoverImageController dismissPopoverAnimated:YES];

}

}


}

#pragma mark -

#pragma mark UIImagePickerControllerDelegate

- (UIImage *)normalizedImage :(UIImage *)image{

if (image.imageOrientation == UIImageOrientationUp) return image;


UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);

[image drawInRect:(CGRect){0, 0, image.size}];

UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return normalizedImage;

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{


UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];




/

originalImage = [self normalizedImage:originalImage];

NSData *imageData = UIImageJPEGRepresentation(originalImage, 1.0);



NSDateFormatter *DateFormatter1=[[NSDateFormatter alloc] init];

[DateFormatter1 setDateFormat:@"dd-MM-yyyy#hh:mm:ss"];

NSString* pstrCreationDate1 = [DateFormatter1 stringFromDate:[NSDate date]];


NSString* pstrTempFormId = [pstrCreationDate1 stringByReplacingOccurrencesOfString:@"#" withString:@"-"];

pstrTempFormId = [ pstrTempFormId stringByReplacingOccurrencesOfString:@":" withString:@"-"];


NSString* pstrTemp =[pstrTempFormId stringByAppendingString:@"#"];

NSString *pImageName =[pstrTemp stringByAppendingString:@"image.png"/pstrValueImageName*/];



NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString* path = [documentsDirectory stringByAppendingPathComponent:

pImageName];


[imageData writeToFile:path atomically:YES];



NSString * jsCallBack = [NSString stringWithFormat:@"addAttachElement()"];

NSString* pstr = [self.detailWebview stringByEvaluatingJavaScriptFromString:jsCallBack];

++iAttachmentNumer;


[self.detailWebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('txtAttachedPath%d').innerHTML = '%@';", (int)iAttachmentNumer, pImageName]];


[self.detailWebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('txtAttachedPath%d').value= '%@';", (int)iAttachmentNumer, pImageName]];


NSString* pstrAttachmentKey = [NSString stringWithFormat:@"txtAttachedPath%d",(int)iAttachmentNumer];

[pAttachmentDictionary setObject:pImageName forKey:pstrAttachmentKey];


[picker dismissViewControllerAnimated:YES completion:NULL];

[self.pPopoverImageController dismissPopoverAnimated:YES];


}

-(void) SendFormSubmissionData:(NSString*)pstrAppId :(NSString*)pstrFormId :(NSString*)pstrInstanceGroupId :(NSData*)JsonData :(NSString*)pstrFileName :(NSMutableDictionary*)pDictionary

{

[self StartActivityIndicator];

NSString * jsonString = [self SaveCreateFormJson:@"SaveCreateFormData" :pstrAppId :pstrFormId :pstrInstanceGroupId :JsonData :pstrFileName : pDictionary];

TaskHelper::getInstance()->SubmitTask(@"SaveCreateFormData",self,jsonString);


}

-(NSString*) SaveCreateFormJson:(NSString*)pstrTaskId :(NSString*)pstrAppId :(NSString*)pstrFormId :(NSString*)pstrInstanceGroupId :(NSData*)JsonData :(NSString*)pstrAttachedFileName :(NSMutableDictionary*)pDictionary

{

BOOL isOnline = NO;/

NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc] init];

[jsonDictionary setObject:@"2" forKey:@"BUSINESS_CATEGORY"];

[jsonDictionary setObject:@"2" forKey:@"BUSINESS_TASK_TYPE"];

[jsonDictionary setObject:pstrTaskId forKey:@"TASK_ID"];


NSMutableDictionary *pTaskData = [[NSMutableDictionary alloc] init];

NSMutableDictionary *pSessionData = [[NSMutableDictionary alloc] init];

[ pSessionData setObject:gpUserDataManager.pstrSessionID forKey:@"ASessionID"];


NSMutableArray *pCookieArray = [[NSMutableArray alloc] init];

[pCookieArray addObject:pSessionData];

[pTaskData setObject:pCookieArray forKey:@"COOKIES"];

[pTaskData setObject:@"10" forKey:@"OPERATION_TYPE"];

[pTaskData setObject: [NSNumber numberWithBool:isOnline] forKey:@"isOnline"];


NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] init];

[dataDictionary setObject: self.pstrFormTypeId forKey:@"formTypeId"];

[dataDictionary setObject: self.pstrProjectId forKey:@"projectId"];

[dataDictionary setObject: pstrAppId forKey:@"appId"];

[dataDictionary setObject: pstrFormId forKey:@"formId"];

[dataDictionary setObject: gpUserDataManager.pstrSessionID forKey:@"ASessionID"];

[dataDictionary setObject: pstrInstanceGroupId forKey:@"instanceGroupId"];


NSString* jString = [[NSString alloc] initWithData:JsonData encoding:NSUTF8StringEncoding];


[dataDictionary setObject:jString forKey:@"offlineCreatedformJsonData"];


NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];


if([pDictionary count]>0)

{

NSArray* pArray = [pDictionary allValues];

[dataDictionary setObject:@"true" forKey:@"isUploadAttachmentInTemp"];

for(int i=0; i<[pDictionary count]; i++)

{

/

[dataDictionary setObject:[pArray objectAtIndex:i] forKey:@"attachedDocs_"];

/

NSString* pstrUpFile = [NSString stringWithFormat:@"upFile%d", i];

NSString *pstrAttachedFilePath = [documentDir stringByAppendingPathComponent:[pArray objectAtIndex:i]];

[dataDictionary setObject:pstrAttachedFilePath forKey:pstrUpFile];

}

}


[pTaskData setObject: dataDictionary forKey:@"DATA"];

[jsonDictionary setObject:pTaskData forKey:@"TASK_DATA"];


NSError *error = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary

options:0

error:&error];

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

return jsonString;


}

@end

In this code, while clicking on save button, HandleJSPCommands method exectues successfully till end,after that application crashes with exc_bad_access,

I tried to symobicate crash report but did not got which object is creating this issue.



-Note:As my uses static library which is not built for simulatore, i can not do profile through xcode in simulator.It would be helpful if anybody identifies which code is creating this issue

uiwebview crash while executing javascript event on save button
 
 
Q