2

I'm trying to preview my documents/pictures using QuickLook library. Everything was fine when I wanted to open the content in both ios7 & ios8. I want to change the name of the item found in the preview. Everything is fine on iOS7 but when I run the application on iOS8 there are problems.

This is my code :

-(void)startDownload{
// Download code here - after I set the  QLPreviewController

QLPreviewController* lcontroller= [QLPreviewController new];
self.quickLookUrl      = item.openUrl;
self.quickLookName     = item.name;
lcontroller.dataSource = self;
[self presentViewController:lcontroller animated:NO completion:nil];
}

#pragma mark - Quicklook Delegate
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
      return 1;
}

- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    QLPreviewItemCustom *obj = [[QLPreviewItemCustom alloc] initWithTitle:self.quickLookName url:[NSURL fileURLWithPath:self.quickLookUrl]];
    return obj;
}

QLPreviewItemCustom - QLPreviewItem details :

QLPreviewItemCustom.h

#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>

@interface QLPreviewItemCustom : NSObject <QLPreviewItem>

- (id) initWithTitle:(NSString*)title url:(NSURL*)url;
@end

QLPreviewItemCustom.m - details :

   #import "QLPreviewItemCustom.h"

    @implementation QLPreviewItemCustom
    @synthesize previewItemTitle = _previewItemTitle;
    @synthesize previewItemURL   = _previewItemURL;

    - (id) initWithTitle:(NSString*)title url:(NSURL*)url
    {
        self = [super init];
        if (self != nil) {
            _previewItemTitle = title;
            _previewItemURL   = url;
        }
        return self;
    }
    @end

When I run the project on iOS8 this is the result :

enter image description here

I have read the release notes about iOS8 GM (I was able to open PDF files in the application without any problem) : iOS8 GM Release Notes

At this point I do not know if it is still a problem with the new SDK or am I wrong in the development process. Can you help me with advice? I have not found other libraries to help me to preview the content of my items (pdf, jpg, excel, doc..).

Thanks in advance for your help !

Tyrone Prude
  • 362
  • 7
  • 19

2 Answers2

1

I Think that you forgot to indicate the delegate of your QLPreviewController in code. I run this code on iOS8 and it's work fine :

// MARK: - QLPreviewControllerDelegate

func numberOfPreviewItemsInPreviewController(controller: QLPreviewController! ) -> Int
{
    return 1
}


func previewController(controller: QLPreviewController!, previewItemAtIndex index: Int) -> QLPreviewItem!
{
    // Break the path into it's components (filename and extension)
    // Use the filename (index 0) and the extension (index 1) to get path
    let path = self.filenameToShow
    return NSURL.fileURLWithPath(path!)!
}


func showPrescription(sender: UIButton)
{

    self.previewController = IGQLPreviewController()
    let directory  = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    var path: String = directory[0] as! String
    let fileName = String(format: "%@_%@.png", arguments: [self.numberTextField.text, self.name.text])
    let imagePath = path.stringByAppendingPathComponent(fileName)

    self.filenameToShow = imagePath

    self.previewController!.dataSource = self
    self.previewController!.delegate = self
    self.previewController!.currentPreviewItemIndex = 0

    self.navigationController?.pushViewController(self.previewController!, animated: true)

}
MK-rou
  • 686
  • 2
  • 9
  • 30
-1
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {

        //NSURL(fileURLWithPath: self.imageUrl) this will append 'file' string at the end, if you try to load file directly from url.
        // so use below code to load file directly from url.
        let doc = URL(string: fileUrl)
        return doc! as QLPreviewItem

    }
Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28