2 garaster garaster 于 2013.07.15 15:40 提问

javascript进行重定向

使用IOS的stringByEvaluatingJavavascriptFromString重定向,整个程序就是我们的移动网站。

代码:

-(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;
    }
 }

下面这三行代码是我需要实现的,能实现吗?

[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');"];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!