我正在尝试使用 iOS 的 stringByEvaluatingJavavascriptFromString 进行重定向。整个应用程序基本上只是我们的移动网站,带有 objectice C 包装器。
这是我的代码
-(IBAction)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.arrayIndex];
for(NSInteger i = 0; i <= self.arrayIndex; 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('<a id=\"submitToCart\"></a>\');"];
[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;
}
}
我在这 3 行代码中试图做什么,这可能吗?
[self.webView stringByEvaluatingJavaScriptFromString:@"jq('body').append('<a id=\"submitToCart\"></a>\');"];
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"jq('#submitToCart').on('click', function(){window.location.href = %@});", newUrl ]];
[self.webView stringByEvaluatingJavaScriptFromString:@"jq('#submitToCart').trigger('click');"];