Ale*_* R. 3 safari cocoa-touch objective-c ios
我有一个HTML文件保存在一个临时目录中,如下所示:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentDirectory = NSTemporaryDirectory();
NSString *documentPath = [documentDirectory stringByAppendingPathComponent:@"mydocument.html"];
[fileManager createFileAtPath:documentPath contents:myHTMLDocumentData attributes:nil];
Run Code Online (Sandbox Code Playgroud)
该文档在我的临时文件中创建.在它之后,我想在Safari中打开这个文档,但它不起作用:
NSURL *url = [NSURL fileURLWithPath:documentPath];
[[UIApplication sharedApplication] openURL:url];
Run Code Online (Sandbox Code Playgroud)
屏幕上没有任何事情发生,没有错误......但是,如果我用@"http://google.fr"替换"url",则会使用google.fr启动Safari,我可以通过输入网址来访问我的临时文件" Safari中的文件://localhost..../myHtmlDocument.html".
希望你能帮我
您无法通过Safari打开存储在应用程序包中的文档(请参阅iOS安全模型).
您需要的是使用a UIWebView
来显示您的应用程序内的文档内容– loadHTMLString:baseURL:
; 例如:
UIWebView* webView = [[[UIWebView alloc] initWithFrame:rect] autorelease];
[webView loadHTMLString:myHTMLSource baseURL:nil];
[self.view addSubview:webView];
Run Code Online (Sandbox Code Playgroud)