簡體   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