繁体   English   中英

如何运行在UIWebView上运行的HTML + javascript文件。 。?

[英]How to run HTML+javascript file run on UIWebView . .?

每个人,

我有一个HTML页面,其中包含一个Javascript函数,显示一个动物动画..我已将其本地添加到xcode项目中。

现在当我在UIWebView中加载这个文件时,它看起来很完美。这是将HTMl文件加载到UIWebView的代码

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"elephant-animation" ofType:@"html"] isDirectory:NO];
        NSURLRequest *req = [NSURLRequest requestWithURL:url];
        [self performSelectorOnMainThread:@selector(startWebLoad3:) withObject:req waitUntilDone:YES];

-(void)startWebLoad3:(NSURLRequest *)request
{
    [wbView3 loadRequest:request];
    [wbView3 setBackgroundColor:[UIColor clearColor]];
    wbView3.scrollView.scrollEnabled = NO;
    [wbView3.scrollView setBackgroundColor:[UIColor clearColor]];
    [wbView3 setOpaque:NO];
}

但我有6页。 当开始用单独的UIWebView加载每个页面时,它转到内存警告..并给我这样的错误。

void SendDelegateMessage(NSInvocation*): delegate (webView:didFinishLoadForFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode

如果我只运行一个页面而不是它完美运行,但是当我开始一起加载或逐个加载时,它会因内存警告而崩溃。

如果有人得到任何解决方案,请告诉我..

我一直坚持这个问题..

谢谢,

最后我得到了解决方案..经过长期的研发,得到了一些有效的解决方案......

"JavaScript execution time is limited to 10 seconds for each top-level entry point. If your script executes for more than 10 seconds, the web view stops executing the script"

这就是为什么我无法在UIWebView中加载html页面的原因..而iPad只能在App启动时处理3 MB的数据加载..而且我使用的不仅仅是它..

所以,我根据要求更改了javascript,现在已经工作了..

谢谢你的一部分..

static NSInteger currentIndex = 0;
if (currentIndex < 99) {
    currentIndex++;
}
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"file%d",currentIndex] ofType:@"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData: 
                        [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.myWebView loadHTMLString:htmlString baseURL:nil];

我希望它能奏效

在下图中,一个用于添加HTML,Javascript和CSS等文件,第二个用于一般XCode文件。

用于HTML,Javascript和CSS文件

在将文件添加到XCode时:

在此输入图像描述

这对我有用..! 希望它能为所有人服务。 谢谢!!

我想,你想在webview中做html文件,在我的应用程序中我在该视图中完成了一个scrollview,在该视图中我最后在循环中添加webview我在webview中添加html文件。 试试这段代码:

ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,768,[[UIScreen mainScreen] bounds].size.height)];
    ScrollView.contentSize = CGSizeMake(768*21,[[UIScreen mainScreen] bounds].size.width);
    ScrollView.pagingEnabled=YES;
    ScrollView.delegate = self;
    ScrollView.userInteractionEnabled = YES;
    ScrollView.showsVerticalScrollIndicator=NO;
    ScrollView.showsHorizontalScrollIndicator=NO;

    int x=0,y=125;
    for ( i=1; i<22; i++)
    {

        view3=[[UIView alloc]initWithFrame:CGRectMake(x,0,768, 1000)];
        view3.backgroundColor=[UIColor whiteColor];

        webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0,y,768,755)];
        webview2.scalesPageToFit=YES;
        webview2.autoresizingMask=(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
        webview2.multipleTouchEnabled=YES;
        webview2.backgroundColor=[UIColor whiteColor];

        NSString *st = [NSString stringWithFormat:@"1%d",i];
        NSString *str12=[NSString stringWithFormat:@"information_"];
        str12=[str12 stringByAppendingString:st];
        NSString *urlAddress        = [[NSBundle mainBundle] pathForResource:str12 ofType:@"html"];//you can also use PDF files
        NSURL *url                  = [NSURL fileURLWithPath:urlAddress];
        NSURLRequest *requestObj    = [NSURLRequest requestWithURL:url];
        [webview2 loadRequest:requestObj];

        webview2.tag= i + 10;
        [view3 addSubview:webview2];


        [ScrollView addSubview:view3];

        x=x+768;


}
    [self.view addSubview:ScrollView];



}

在我的应用程序22 html页面(information_%i,必须在资源中)添加webview,它工作得很好。

我希望它对你也有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM