ios - 事件指示器未显示在 PDF uiwebview 加载中

标签 ios objective-c uiwebview uiactivityindicatorview

我正在开发一个带有 uiwebview 的应用程序,在该 webview 中,当 uiwebview 通过 java 调用 native 方法时,我需要加载本地 pdf 文件。我成功地做到了这一点,但是当我浏览 webisete 事件指示器时,它显示得很好,但是当 native 函数调用并且我在 uiwebview 中加载 pdf 时,它在从 url 加载 pdf 时不会显示。让我在这里发布我的代码。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize webPage;
- (BOOL) shouldAutorotate{
    return NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    webLink = @"http://url.com/m/";
    liNk = webLink;
    [webPage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:liNk]]];
    webPage.backgroundColor = [UIColor blackColor];
    //[[webPage.subviews objectAtIndex:0] setBounces:NO];
    [(UIScrollView*)[webPage.subviews objectAtIndex:0] setShowsVerticalScrollIndicator:NO];
    [webPage addSubview:activity];
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
    webPage.delegate = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[[request URL] absoluteString] hasPrefix:@"ios:"]) {
        // Call the given selector
        [self performSelector:@selector(webToNativeCall)];
        return NO;
    }
    return YES;

}
- (void)webToNativeCall
{
        NSString *pdfurl = @"http://url.com/mypdf.pdf";
        NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pdfurl]];
        NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"Documents"]];
        NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"mypdf.pdf"];
        [pdfData writeToFile:filePath atomically:YES];
        // Now create Request for the file that was saved in your documents folder
        NSURL *url = [NSURL fileURLWithPath:filePath];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webPage setUserInteractionEnabled:YES];
        [webPage setDelegate:self];
        [webPage loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最佳答案

尝试在 UIWebViewDelegate 方法中启动和停止事件指示器

  #pragma mark - UIWebViewDelegate -

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
    [activity startAnimating];

    if ([[[inRequest URL] absoluteString] hasPrefix:@"ios:"]) 
    {
       // Call the given selector
       [self performSelector:@selector(webToNativeCall)];
       return NO;
    }

    return YES;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [activity stopAnimating];
}


-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [activity stopAnimating];
}

关于ios - 事件指示器未显示在 PDF uiwebview 加载中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22862057/

相关文章:

iphone - 从 iOS 的 WebService(Json/XML) 同步数据库 (sqlite)

android - Titanium 是否以及如何将 css 转换为原生?

objective-c - 我的 UI 上的 NSTextField 需要通过标签访问它们

uiwebview - iOS 9 在 WebCore::FrameView::contentsSizeRespectingOverflow 中崩溃:

ios - 1x、2x 和 3x 的可用设备是什么

iphone - 模糊图像的一部分

ios - 此处 map 中未获得当前位置

php - 我可以用 C 语言包装我网站上的 PHP 代码来构建 iOS 应用程序吗?

objective-c - 我有两个错误 : No visible @interface for 'UIWebview'

iphone - 创建 iPhone map 应用程序