ios - Save PDF to Documents (PDF is corrupted) -


i'm saving pdfs json. when navigate file path in app documents directory verify have right file, , try open pdf, i can't open pdf, , error:

enter image description here

enter image description here

the file path good, document there, been corrupted or something. i'd assume related nsdata i'm not sure i'm doing wrong. thoughts? thanks!

viewcontroller:

dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{     nslog(@"downloading pdf started");      // selected pdf     nsstring *pdfselected = self.pdfarray[indexpath.row];     nslog(@"pdfsl: %@", pdfselected);     nsdata *pdfdata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:pdfselected]];     nslog(@"pdfdata: %@", pdfdata);     nsstring *pdfpath =[documentsdirectory stringbyappendingpathcomponent:[nsstring stringwithformat:@"%@%ld.pdf",@"savedpdf", (long)indexpath.row]];      dispatch_async(dispatch_get_main_queue(), ^{         [pdfdata writetofile:pdfpath atomically:yes];         nslog(@"pdf file saved!");         nslog(@"pdf file path: %@", pdfpath);         // save pdf path in defaults evaluation in details         [defaults setobject:pdfpath forkey:@"cachedpdfpath"];     });  }); 

json snippet:

  {         "fname": "nadene",         "lname": "feehan",         "email": "nadene.feehan@gmail.com",         "phone": "(152) 555-5321",         "image": "http://logok.org/wp-content/uploads/2014/04/apple-logo-grey-880x625.png",         "video": "https://github.com/versluis/movie-player/blob/master/movie%20player/video.mov?raw=true",         "pdf": "http://adcdownload.apple.com/developer_tools/xcode_8_beta_3/release_notes_for_xcode_8_beta_3.pdf"     }, 

console:

pdfsl:http://adcdownload.apple.com/developer_tools/xcode_8_beta_3/release_notes_for_xcode_8_beta_3.pdf

pdfdatalength: 2070

pdf file path: /users/user/library/developer/coresimulator/devices/1/data/containers/data/application/a/documents/savedpdf0.pdf

i tried:

nsdata *pdfdata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:pdfselected] options:nsdatareadinguncached error:&errorpdf]; when checked console see error there none pdfdataerror: (null)

looks url locked if you're not signed in developer account; when tried w/out having signed in first, redirected page: developer.apple.com/unauthorized. contents are, might expect, pure html, , i'm guessing many, if not most, pdf parsers crash if send them html.

you'll need sort out how handle users aren't signed developer accounts (and, make things more complicated, believe developer.apple.com auto-signs out after 20 minutes or hour of inactivity), , doesn't it's straightforward checking http status/error codes.


Comments