简体   繁体   English

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

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

Everybody, 每个人,

I have a one HTML page which contain one Javascript function which shows one animals animation.. i have added it locally in to xcode project. 我有一个HTML页面,其中包含一个Javascript函数,显示一个动物动画..我已将其本地添加到xcode项目中。

Now when i load this file in UIWebView it will looks perfect.. Here is the code of loading HTMl file to UIWebView 现在当我在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];
}

But i have 6 pages. 但我有6页。 When start to load every page with separate UIWebView, It goes to memory warning.. And give me error like here. 当开始用单独的UIWebView加载每个页面时,它转到内存警告..并给我这样的错误。

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

if i run only one page than it run perfectly, but when i start to load all together or one by one it crashed with memory warning. 如果我只运行一个页面而不是它完美运行,但是当我开始一起加载或逐个加载时,它会因内存警告而崩溃。

Let me know if any one get any solution of it.. 如果有人得到任何解决方案,请告诉我..

i have stuck with this problem.. 我一直坚持这个问题..

Thanks, 谢谢,

finally i got solution.. After long R & D, got some effective solution... 最后我得到了解决方案..经过长期的研发,得到了一些有效的解决方案......

"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"

this is the reason why i fail to load html page in UIWebView.. And iPad handle up to 3 MB data loading on App launch only.. And i have used more than it.. 这就是为什么我无法在UIWebView中加载html页面的原因..而iPad只能在App启动时处理3 MB的数据加载..而且我使用的不仅仅是它..

So, i change javascript as per requirement and now worked.. 所以,我根据要求更改了javascript,现在已经工作了..

Thanks for be part of it.. 谢谢你的一部分..

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];

I hope it will work 我希望它能奏效

In the figure below, one is for adding HTML, Javascript and CSS like files and the second is for general XCode files. 在下图中,一个用于添加HTML,Javascript和CSS等文件,第二个用于一般XCode文件。

for HTML, Javascript and CSS files 用于HTML,Javascript和CSS文件

While adding files into XCode: 在将文件添加到XCode时:

在此输入图像描述

This works for me..! 这对我有用..! Hope it will work for all. 希望它能为所有人服务。 Thank you!! 谢谢!!

i think,you want to do html file in webview ,in my application i done one scrollview in that view ,in that view i add webview in loop at last i add the html file in webview. 我想,你想在webview中做html文件,在我的应用程序中我在该视图中完成了一个scrollview,在该视图中我最后在循环中添加webview我在webview中添加html文件。 try this code: 试试这段代码:

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];



}

in my application 22 html pages(information_%i,must in resource ) add in webview ,it is working very well. 在我的应用程序22 html页面(information_%i,必须在资源中)添加webview,它工作得很好。

i hope it's helpful to u also. 我希望它对你也有帮助。

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

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