0

I have an HTML file that calculates various cable sizes with my xcode project (all the rest is in objective c), if a cable is out of range it fires an alert alert ("No suitable cables"); upon this I would like to trigger an action. Can I tell my app that the event has happened when its in HTML? Ive heard of listners and think I may need to use in this circumstance maybe? Not sure how I would implement it thought

b1nary
  • 289
  • 3
  • 11
JSA986
  • 5,870
  • 9
  • 45
  • 91
  • see here: http://stackoverflow.com/questions/3742590/nsstring-in-webview-iphone-and-objective-c/3742635#3742635 – lupatus Dec 10 '12 at 14:23

1 Answers1

2

You can accomplish this by in the webview triggering a URL that would load a custom prefix that you can look for in the webview's shouldStartLoadWithRequest method in the javascript if a cable alert needs to be sent.

ex. sendCableAlert://scenario1.com

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *alertPrefix = @"sendCableAlert://";

    if ([[[request URL] absoluteString] hasPrefix:alertPrefix]) {
        //You've hit an alert do something..
        NSLog(@"CABLE ALERT!");

        return NO;
    }
    else {
        return YES;
    }
}
propstm
  • 3,461
  • 5
  • 28
  • 41
  • I think Im understanding the second part of the code and the principals behind this, but I still dont fully understand. The `sendCableAlert://scenario1.com`, does that go in my html file? – JSA986 Dec 10 '12 at 17:12
  • Yes you would have the javascript trigger that in something like a loadUrl(http://somesite.com) which i'm guessing will be conditionally sent based on the outcome of your calculations. – propstm Dec 10 '12 at 17:17
  • I think thats where im confused, the load url? I dont have that im my html code, its a calculator in a local html file (sorry should have stated that) ` ...` form.percentage_drop.value = Math.round(mycable.percentage_drop(I,l,V) * 10 )/10; form.current.value = Math.round(I*100)/100; } else { alert("No suitable cables"); } } – JSA986 Dec 10 '12 at 17:30
  • try this then form.percentage_drop.value = Math.round(mycable.percentage_drop(I,l,V) * 10 )/10; form.current.value = Math.round(I*100)/100; } else { loadURL(sendCableAlert://testingprefix); } } // I just got the js from quickly googling loadURL javascript. – propstm Dec 10 '12 at 17:34
  • That way where you are doing your js alert now, its simply using javascript to try to load a URL, which obj-c will see as not really a URL but just a way to pass a message back to the viewcontroller – propstm Dec 10 '12 at 17:35
  • Tried and tried with this but im just not getting it, thanks for your time. Will have to think of a workaround – JSA986 Dec 10 '12 at 18:12