使用javascript的iOS注入重定向
我正在尝试使用iOS的stringByEvaluatingJavavascriptFromString重定向。整个应用程序基本上就是我们的移动站点,带有objectice C包装 这是我的密码使用javascript的iOS注入重定向,javascript,iphone,ios,objective-c,Javascript,Iphone,Ios,Objective C,我正在尝试使用iOS的stringByEvaluatingJavavascriptFromString重定向。整个应用程序基本上就是我们的移动站点,带有objectice C包装 这是我的密码 -(IBAction)sendToCart:(id)sender { self.action = @"cart"; self.addingToList = NO; self.selectedIndex = 3; if([self.barCodeArray count] &
-(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;
}
}
-(iAction)发送开始:(id)发送人
{
self.action=@“购物车”;
self.addingToList=否;
self.selectedIndex=3;
如果([self.barcoderarray count]>0)
{
NSString*myUrl=@”http://mycompany'surl/barcodeAddTo.ep?action=cart&barcodes=“;
NSMutableArray*newQuantityArray=[NSMutableArray阵列容量:self.arrayIndex];
对于(NSInteger i=0;i可能,这将帮助您:
只需将重定向功能插入您的webview,然后调用它。是的,就可以了。谢谢!
[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');"];