Je fais n'importe quel webView Et je veux appeler de javascript toute méthode c objective qui renvoie tout paramètre. J'ai essayé de nombreuses façons mais pas autorisé. Méthode de l'objectif c ici:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *URL = [request URL]; if ([[URL scheme] isEqualToString:@"donordialog"]) { // now we need to figure out the function part NSString *functionString = [URL resourceSpecifier]; if ([functionString hasPrefix:@"bloodTypeChanged"]) { // the blood type has changed, now do something about it. NSString *parameter = [functionString stringByReplacingOccurrencesOfString:@"bloodTypeChanged" withString:@""]; // remove the '(' and then the ')' parameter = [parameter stringByReplacingOccurrencesOfString:@"(" withString:@""]; parameter = [parameter stringByReplacingOccurrencesOfString:@")" withString:@""]; // log the paramter, as I don't know what to do with it right now UIAlertView *alert=[[ UIAlertView alloc] initWithTitle:@"iosdan javascripti" message:@"ddddddddd" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; //NSLog(@"%@", parameter); } return NO; } return YES; }
Javascript:
function myFunction() { url="http://example.com"; window.location="donordialog:blooTypeChanged("+url+")"; }
Html:
<button onclick="myFunction()">click me</button>
Commentaire: J'ai besoin de ça: c'est une méthode d'obj c par exemple. Aaa {}. Ma méthode aaa doit renvoyer tout paramètre. Et j'ai wevView. J'ai chargé toute url sur cette webView: par exemple www.example.com/forios. J'ai besoin d'appeler cette méthode (aaa) à partir de javascript qu'elle est en html sur www.example.com/forios et alerter le résultat de la fonction aaa. comprendre?. Si vous comprenez, ne regardez pas mon code. Aidez-vous de toute façon. Version Android de ma question: Appelez la fonction Java à partir de JavaScript sur Android WebView Je souhaite alerter certains paramètres de retour de la méthode. Aidez-moi, s'il vous plaît. Merci.
L'approche la plus courante que j'avais l'habitude d'interagir de javascript dans obj-c est de modifier hash. Lors de l'événement requis dans votre js write window.location.hash = '#cmd_alertMessage';
Après ceci votre - (BOOL)webView: shouldStartLoadWithRequest: navigationType:
sera appelé:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSArray * compenents = [request.URL.absoluteString componentsSeparatedByString:@"#"]; if (compenents.count > 1) { NSString * cmd = compenents[1]; if ([cmd rangeOfString:@"cmd_alertMessage"].location != NSNotFound) { // Call your obj-c method and get appropriated param NSString * jsFunction = [NSString stringWithFormat:@"setParameterFromAAAMethod('%@')", [self aaa]]; // Return this param back to js NSString * alertMessage = [webView stringByEvaluatingJavaScriptFromString:jsFunction]; } } return YES; } - (NSString *)aaa { return @"This is special parameter that you need"; }
Ainsi, il fonctionnera en 3 étapes:
Voici une solution rapide et simple sans JavaScriptCore pour une compatibilité ascendante (pourrait envisager de passer à Javascript). Voici une mise en œuvre facile.
Remarque: cette solution est uniquement pour UIwebview (pas UIscrollview)
//viewcontroller.h
@interface ViewController : UIViewController<UIWebViewDelegate> @property (strong, nonatomic) IBOutlet UIWebView *viewWeb; @end
//viewcontroller.m
- (void)viewDidLoad { [super viewDidLoad]; NSString *fullURL = @"http:player.fusion.fm"; NSURL *url = [NSURL URLWithString:fullURL]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [_viewWeb loadRequest:requestObj]; _viewWeb.delegate = self; } -(BOOL)webView:(UIWebView *)_viewWeb shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *url = request.URL; if ([[url scheme] isEqualToString:@"ios"]) { [self mute]; // <-- YOUR OBJ-C FUNCTION HERE NSLog(@"test"); return YES; }else { return YES; } }
// Le Javascript (dans la visualisation web)
try { window.location="ios://null" } catch(err) { }
Vous pouvez définir l'emplacement d'un iframe caché à quelque chose de spécifique à votre application, puis intercepter cette requête dans Obj-C.
Voici un aperçu et quelques exemples: http://blog.techno-barje.fr//post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/
Eh bien, je ne sais pas si j'ai bien compris votre question, mais si vous souhaitez appeler une méthode javascript, vous pouvez procéder de la manière suivante:
[yourWebView stringByEvaluatingJavaScriptFromString:@"yourMethodName()"];
Vous pouvez également passer les paramètres dans la méthode ci-dessus.