ios - Saving PDF file to iBooks. -
i'm displaying list of pdf files in uiwebview along uibutton. when click button need save pdf file ibooks , later user can read ibooks.
here code below:
nsstring *path = [[nsbundle mainbundle] pathforresource:@"mypdf" oftype:@"pdf"]; nsurl *targeturl = [nsurl fileurlwithpath:path]; nsurlrequest *request = [nsurlrequest requestwithurl:targeturl]; uiwebview *webview=[[uiwebview alloc] initwithframe:cgrectmake(0, 0, 300, 300)]; [[webview scrollview] setcontentoffset:cgpointmake(0,500) animated:yes]; [webview stringbyevaluatingjavascriptfromstring:[nsstring stringwithformat:@"window.scrollto(0.0, 50.0)"]]; [webview loadrequest:request]; [self.view addsubview:webview]; [webview release];
uibutton download:
uibutton *button = [uibutton buttonwithtype:uibuttontyperoundedrect]; [button addtarget:self action:@selector(download:) forcontrolevents:uicontroleventtouchdown]; [button settitle:@"show view" forstate:uicontrolstatenormal]; button.frame = cgrectmake(80.0, 210.0, 160.0, 40.0); [webview addsubview:button]; -(void)download:(id)sender{ }
you have use uidocumentinteractioncontroller this:
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory , nsuserdomainmask, yes); nsstring *documentsdir = [paths objectatindex:0]; nsstring *pdffilepath = [documentsdir stringbyappendingpathcomponent:yourpdffile.pdf];// yourpdffile file here nsurl *url = [nsurl fileurlwithpath:pdfpath]; uidocumentinteractioncontroller *doccontroller = [uidocumentinteractioncontroller interactioncontrollerwithurl:url]; doccontroller.delegate = self; [doccontroller retain]; // doccontroller released when dismissed (autorelease in delegate method) bool isvalid = [doccontroller presentopeninmenufromrect:yourreadpdfbutton.frame inview:self.view animated:yes]; // provide u want read pdf yourreadpdfbutton if (!isvalid) { nsstring * messagestring = [nsstring stringwithformat:@"no pdf reader found on device. in order consult %@, please download pdf reader (eg. ibooks).", yourpdffiletitle]; uialertview * alertview = [[uialertview alloc] initwithtitle:@"error" message:messagestring delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show]; [alertview release]; } ... // uidocumentinteractioncontroller delegate method - (void)documentinteractioncontrollerdiddismissopeninmenu:(uidocumentinteractioncontroller *)controller { [controller autorelease]; }
hope you.
Comments
Post a Comment