objective c - Show downloaded PDF file with webView ios -
i'm making iphone app, download pdf file , display in webview. script not show downloaded pdf. download , save in documents, webview not show it.
here's script:
nsstring *path = [[nsbundle mainbundle] pathforresource:@"3" oftype:@"pdf"]; nsurl *urlen = [nsurl fileurlwithpath:path]; nsmutableurlrequest *urlrequest = [nsmutableurlrequest requestwithurl:urlen]; [webview loadrequest:urlrequest]; [webview setscalespagetofit:yes];
from official documentation on nsurl
official documentation on nsurl
.
sending nil
argument fileurlwithpath:
produces exception.
the problem [[nsbundle mainbundle] pathforresource:oftype:]
. returning nil
, rather actual path file.
the problem here [nsbundle mainbundle]
refers files bundle app. need in app's document directory, stores files has downloaded.
this method give path app's document directory:
nsstring* documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) firstobject];
now, append file name path:
nsstring *pdfpath = [documentspath stringbyappendingpathcomponent:@"3.pdf"];
and measure (because crashes bad), make sure file exists such:
bool fileexists = [[nsfilemanager defaultmanager] fileexistsatpath:pdfpath];
and finish such:
if (fileexists) { nsurl *urlen = [nsurl fileurlwithpath:pdfpath]; nsmutableurlrequest *urlrequest = [nsmutableurlrequest requestwithurl:urlen]; [webview loadrequest:urlrequest]; [webview setscalespagetofit:yes]; } else { // let user know there's sort of problem }
Comments
Post a Comment