如何从iPhone中的URL加载pdf?

时间:2012-04-27 09:09:01

标签: iphone

我有一个应用程序,我使用

从捆绑中加载pdf
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("hhhh.pdf"), NULL, NULL);.

现在我想从我的网络服务器加载它。我是用字符串url使用

完成的
NSString *strUlr = [NSString stringWithFormat:@"https://s3.amazonaws.com/hgjgj.pdf"];

CFStringRef aCFString = (CFStringRef)strUlr;

//CFURLRef pdfURL = aCFString;
// NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),aCFString, NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
   NSLog(@"%@",pdf);
//CFRelease(pdfURL);

但没有成功..任何人都知道如何使这个代码工作。我不需要因为某些原因将其加载到pdf。可以有人帮助我吗?

3 个答案:

答案 0 :(得分:1)

您仍然尝试获取应该在应用程序包中的资源的URL,而不是远程服务器上的资源。创建url的正确方法是CFURLCreateWithString:

CFURLRef pdfURL = CFURLCreateWithString(NULL, aCFString, NULL);

请记住,在显示之前下载pdf并在本地缓存它可能会更好 - 这种方法还可以为您的网址提供更多的错误处理灵活性和可能的​​身份验证挑战

答案 1 :(得分:0)

仅供阅读使用,您可以通过网址在Uiwebview中加载pdf文件。

答案 2 :(得分:0)

如果我没有弄错,那么您可能想要从iPhone中的Web服务器读取pdf,如果是这样,那么您可以根据需要使用UIWebView:以下是如何在本地阅读和存储它:

首先创建UIWebView并包含<< UIWebViewDelegate >>

  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  NSString *documentsPath = [paths objectAtIndex:0];
  NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"]; 
 if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
           // download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
         NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://s3.amazonaws.com/hgjgj.pdf"]];
            //Store the downloaded file  in documents directory as a NSData format
         [pdfData writeToFile:filePath atomically:YES];
    }

     NSURL *url = [NSURL fileURLWithPath:filePath];
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
     [yourWebView setUserInteractionEnabled:YES];
     [yourWebView setDelegate:self];
     yourWebView.scalesPageToFit = YES;
     [yourWebView loadRequest:requestObj];