سؤال

أحاول استدعاء جافا سكريبت في صفحة هتمل باستخدام الوظيفة -

View did load function
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"BasicGraph.html"];
    NSURL *urlStr = [NSURL fileURLWithPath:writablePath];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"BasicGraph" ofType:@"html"];
    [fileManager copyItemAtPath:myPathInfo toPath:writablePath error:NULL];

    [graphView loadRequest:[NSURLRequest requestWithURL:urlStr]];
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"];
}

هنا هو جافا سكريبت على صفحة هتمل -

<script>
    function methodName()
      {
         // code to draw graph
      }

ومع ذلك ، فإن الوظيفة methodName() لا يتم الاتصال به ولكن بعد النافذة.أونلواد = وظيفة () كل شيء يعمل بشكل جيد..

أحاول دمج رغرافس في طلبي و Basic.html هي صفحة هتمل التي تتم كتابة جافاسكريبتس.

سيكون أمرا رائعا إذا كان شخص ما يمكن أن يساعدني مع هذا.

هل كانت مفيدة؟

المحلول

بسيطة:محاولة تنفيذ وظيفة شبيبة من الهدف-ج قبل الصفحة حتى تم تحميلها.

تنفيذ طريقة مندوب إيويبفيو webViewDidFinishLoad: في إيفيوكونترولر الخاص بك وهناك استدعاء [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"]; للتأكد من الحصول على استدعاء الدالة بعد تم تحميل الصفحة.

نصائح أخرى

لتوضيح أكثر قليلا.

.ح-تنفيذ إيويبفيوديليغات

@interface YourViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end

.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = @"http://www.google.com";
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];
    _webView.delegate = self; //Set the webviews delegate to this
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    //Execute javascript method or pure javascript if needed
    [_webView stringByEvaluatingJavaScriptFromString:@"methodName();"];
}

يمكنك أيضا تعيين المندوب من القصة المصورة بدلا من القيام بذلك في التعليمات البرمجية.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top