[英]iOS objective-c How can I catch window.open event in UIWebView
[英]how to open file.xlsx in UIWebView IOS using objective-c
我的IOS应用程序文档文件夹中有file.xlsx。 我想在UIWebview中显示这个excel文件。 但我得到低于错误,
Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted"
但pdf和CSV文件正在打开,我是IOS的新手并尝试了所有可能的工作,我想从最近2天开始工作。 什么都没有解决..请帮助我
更新:即使我将其重命名为file.xls,下面未打开的是我的代码,
NSURL* nsUrl = [NSURL URLWithString:url];
_urlReq = [NSURL URLWithString:url];
[self performSelector:@selector(urlRequestForFile) withObject:nil afterDelay:0];
_webView.delegate = self;
NSURLRequest* request = [NSURLRequest requestWithURL: nsUrl];
[_webView loadRequest: request];
-(void)urlRequestForFile{
self.connection = nil;
NSURLRequest *requestForFile = [NSURLRequest requestWithURL:_urlReq cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:300];
_connection = [[NSURLConnection alloc]initWithRequest:requestForFile delegate:self startImmediately:YES];
_ongingServiceFlag = YES;
}
需要帮助在我的IOS应用程序中使用UIWebView显示xlsx文件,还是有其他方法在应用程序内显示xlsx文件而不使用第三方应用程序?
更新(解决方案):
我很惊讶地发现即使在UIWebView的苹果网站中也没有提到XLSX的支持,但实际上UIWebView完全支持XLSX格式。 您需要确保的一件事是指定正确的'textEncodingName'值。 如果您的文件以base64二进制编码存储,则必须将其作为textEncodingName提及:@“base64”否则您必须提及“utf-8”
以下线为我工作:
[webView loadData:urlData MIMEType:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" textEncodingName:@"base64" baseURL:nil];
无法使用UIWebView
打开.xlsx
文件。 虽然您可以使用UIWebView
加载.xls
文件。 以下是可以使用UIWebView
加载的文件列表
https://developer.apple.com/library/content/qa/qa1630/_index.html
如果要使用.xlsx file
,则必须使用包含QLPreviewController
QuickLook FrameWork
。 你的代码应该是这样的 -
- (void) initQlController{
QLPreviewController *prev = [[QLPreviewController alloc]init];
prev.delegate = self;
prev.dataSource = self;
[self presentModalViewController:prev animated:YES];
[prev.navigationItem setRightBarButtonItem:nil]; }
然后你必须使用dataSource方法: -
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
.XLSX文件类型基于openXML,这是个好消息! 这意味着它易于阅读,我们只需要让webview知道我们正在加载/显示的文件的类型,或者更确切地说是mimeTYPE。 根据microsoft,用于XLSX(OpenXML)文件的mimetype是:
应用/ vnd.openxmlformats-officedocument.spreadsheetml.sheet
来源: https : //blogs.msdn.microsoft.com/dmahugh/2006/08/08/content-types-for-open-xml-documents/
为此,我们通过调用webView方法加载数据( dataWithContentsOfFile:
或dataWithContentsOfURL:
或您喜欢的方法):
[_webView loadData:<#(nonnull NSData *)#> MIMEType:<#(nonnull NSString *)#> textEncodingName:<#(nonnull NSString *)#> baseURL:<#(nonnull NSURL *)#>]
我的工作代码示例:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://mycoolserver.com/file.xlsx"]];
[_webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" textEncodingName:@"utf-8" baseURL:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.