IOS с использованием javascript

Я пытаюсь перенаправить использование iOS stringByEvaluatingJavavascriptFromString. Все приложение в основном является нашим мобильным сайтом с оболочкой объектива C.

Вот мой код

-(********)sendToCart:(id)sender
{
 self.action = @"cart";
 self.addingToList = NO;
 self.selectedIndex = 3;

 if([self.barCodeArray count] > 0)
 {
 NSString *myUrl = @"http://mycompany'surl/barcodeAddTo.ep?action=cart&barcodes=";

 NSMutableArray *newQuantityArray = [NSMutableArray arrayWithCapacity:self.**********];

 for(NSInteger i = 0; i <= self.**********; i++)
 {
 if ([self.quantityArray objectAtIndex:i] != NULL)
 {
 [newQuantityArray addObject:[self.quantityArray objectAtIndex:i]];
 }
 }

 NSDictionary *barCodeAndQuantityData = [NSDictionary dictionaryWithObjectsAndKeys: self.barCodeArray, @"barcodes", newQuantityArray, @"qty", nil];

 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:barCodeAndQuantityData options:kNilOptions error:nil];

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

 myUrl = [myUrl stringByAppendingString:myDictionaryString];

 NSString *newUrl = [myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 [self.webView stringByEvaluatingJavaScriptFromString:@"jq('body').append('\');"];
 [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"jq('#submitToCart').on('click', function(){window.location.href = %@});", newUrl ]];
 [self.webView stringByEvaluatingJavaScriptFromString:@"jq('#submitToCart').trigger('click');"];

 NSLog(@"Your encoded url looks like %@", newUrl);

 /*
 NSURL *url = [[NSURL alloc] initWithString:[myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
 NSLog(@"Your url looks like %@", url);

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
 [request setURL:url];
 [request setHTTPMethod:@"POST"];
 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
 [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

 //NSLog(@"Your request looks like %@", request);

 NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
 [connection start];
 */
 [self hideScanViewElementsShowWebView];

 }
 else
 {
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Bar Codes Scanned" message:@"You haven't scanned any bar codes" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
 [alert show];
 alert = nil;
 }
 }

Что я пытаюсь сделать в этих трех строках кода, возможно ли это?

[self.webView stringByEvaluatingJavaScriptFromString:@"jq('body').append('\');"];
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"jq('#submitToCart').on('click', function(){window.location.href = %@});", newUrl ]];
[self.webView stringByEvaluatingJavaScriptFromString:@"jq('#submitToCart').trigger('click');"];
1 ответ

Может быть, это поможет вам:

http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview

Просто добавьте функцию перенаправления в свой веб-просмотр, а затем вызовите ее.

licensed under cc by-sa 3.0 with attribution.