0

I am trying to redirect using iOS's stringByEvaluatingJavavascriptFromString. The whole app is basically just our mobile site with an objectice C wrapper.

Here is my code

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

What I'm trying to do in these 3 lines of code, is this possible?

[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');"];

1 Answer 1

0

May be this will help you:

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

Just inject redirect function to your webview and then call it.

1
  • Yup, that'll do it. Thanks! Commented Jul 12, 2013 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.