Knowledge Base - How to build your first PDF reader for iOS - v3.7x

Download the PDF version of this guide.

How to include RadaeePDF SDK in your project

Copy PDFViewer folder into your project’s folder.

Remove these folders from PDFViewer inside your project: 

  • BookMarkViewController.h
  • BookMarkViewController.m
  • BookMarkViewController.xib
  • main.m
  • PDFLib/PDFObjc.1.h (if present)
  • PDFLib/PDFObjc.1.m (if present)
  • RDTestMainViewController.m

Drag PDFViewer folder with “Create groups” option selected except cmaps and fdat folders.

Drag cmaps and fdat folders into your project with the “Create folder references” option.

Add these lines in your prefix.pch file:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "UINavigationController+autoRotate.h"
#endif
#import "RDUtils.h"

If you don’t have any prefix.pch file in your project you could copy PDFViewer-Prefix.pch from Radaee demo project in your project folder and set its path as prefix header in Build Setting/Prefix Header.

 

In Build Phases/Link Binary With Libraries:

  • Add QuartzCore framework 
  • Add CoreGraphics framework 
  • Add libc++ lib 
  • Add libRDPDFLib.a (if it isn’t already added)

Add -lz and -lstdc++ in Other Linker Flags of Build Settings tab 

Add -fno-objc-arc to the following files in Build Phases/Compile Sources:

  • ASIInputStream.m
  • ASIDownloadCache.m
  • ASIDataDecompressor.m
  • ASIFormDataRequest.m
  • ASIHTTPRequest.m
  • ASIAuthenticationDialog.m
  • ASINetworkQueue.m
  • ASIDataCompressor.m
  • Reachability.m

The building process may throw an error while building the project for iOS Simulator and linking in object file built for ARM64 architecture iOS, for architecture arm64:

  • Add arm64 in Build Settings/excluded Architectures debug

How to open your first PDF file

Import RDVGlobal.h in your AppDelegate.h 

Add :

- [RDVGlobal Init];

in - (BOOL)application:(UIApplication  *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Add a Navigation controller in Main.storyboard, delete the Root View Controller, put a root view controller segue from your Navigation Controller to your View Controller and apply “Is Initial View Controller” to the Navigation Controller.

Import RDLoPDFViewController.h in your ViewController.h and create NSMutableArray *m_files and PDFReaderCtrl *m_pdf:

#import <UIKit/UIKit.h>
#import "RDLoPDFViewController.h"
@interface ViewController : UIViewController
{
    RDLoPDFViewController *m_pdf;
    NSMutableArray *m_files;
}
@end

@interface RDFileItem : NSObject
-(id)init:(NSString *)help :(NSString *)path :(int)level;
@property NSString *help;
@property NSString *path;
@property int level;
@property RDVLocker *locker;
@end

Add these functions to your ViewController.m:

- (void)refreshDocuments
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dpath = [paths objectAtIndex:0];
    NSFileManager *fm = [NSFileManager defaultManager];
    m_files = [[NSMutableArray alloc] init];
    [self addPDFs:dpath :@"" :fm :0];
    [self copyDocumentsFromAssets:dpath];
}

- (void)addPDFs:(NSString *)dpath :(NSString *)subdir :(NSFileManager *)fm :(int)level
{
    NSString *path = [dpath stringByAppendingFormat:@"/%@", subdir];
    NSDirectoryEnumerator *fenum = [fm enumeratorAtPath:path];
    NSString *fName;
    while(fName = [fenum nextObject])
    {
        BOOL dir;
        NSString *dst = [path stringByAppendingFormat:@"%@",fName];
        if( [fm fileExistsAtPath:dst isDirectory:&dir] )
        {
            if( dir )
            {
                [self addPDFs:dpath :dst :fm :level+1];
            }
            else if( [dst hasSuffix:@".pdf"] )
            {
                NSString *dis = [subdir stringByAppendingFormat:@"%@",fName];//display name
                RDFileItem *item = [[RDFileItem alloc] init:dis :dst :level];
                [m_files addObject:item];
            }
        }
    }
}

- (void)copyDocumentsFromAssets :(NSString *)dpath
{
    NSString *hf = [[NSBundle mainBundle] pathForResource:@"fdat" ofType:nil];
    for (NSString *fpath in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hf error:nil]) {
        if([fpath.pathExtension isEqualToString:@"pdf"] || [fpath.pathExtension isEqualToString:@"PDF"]) {
            NSString *documentPath = [hf stringByAppendingPathComponent:fpath];
            NSString *destPath = [dpath stringByAppendingPathComponent:fpath];           
if(![[NSFileManager defaultManager] fileExistsAtPath:destPath]) {
                [[NSFileManager defaultManager] copyItemAtPath:documentPath toPath:destPath error:nil];
                RDFileItem *item = [[RDFileItem alloc] init:[fpath stringByDeletingPathExtension] :destPath :0];
                [m_files addObject:item];
            }
        }
    }
}

- (void)presentPwdAlertControllerWithTitle:(NSString *)title message:(NSString *)message
{
}

- (void)pdf_open_path:(RDFileItem *)item :(NSString *)pswd
{
   if( m_pdf == nil )
       {
           m_pdf = [[RDLoPDFViewController alloc] initWithNibName:@"RDLoPDFViewController" bundle:nil];
           m_pdf.view.frame = self.view.frame;
       }
       NSLock *theLock = [[NSLock alloc] init];
       if ([theLock tryLock])
       {
           //Open PDF file
           int result = [m_pdf PDFOpen:item.path :pswd];
           if(result == 1)
           {
               m_pdf.hidesBottomBarWhenPushed = YES;
               [self.navigationController pushViewController:m_pdf animated:YES];
           }
           //return value is encryption document
           else if(result == 2)
           {
               NSString *title = NSLocalizedString(@"Please Enter PassWord", @"Localizable");
               UIAlertController *pwdAlert = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
               [pwdAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                   textField.placeholder = NSLocalizedString(@"PassWord", @"Localizable");
                   textField.secureTextEntry = YES;
               }];
               UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"Localizable") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                   UITextField *password = pwdAlert.textFields.firstObject;
                   if (![password.text isEqualToString:@""]) {
                       int result = [self->m_pdf PDFOpen:item.path :password.text];
                           if(result == 1)
                           {
                               UINavigationController *nav = self.navigationController;
                               self->m_pdf.hidesBottomBarWhenPushed = YES;
                               nav.hidesBottomBarWhenPushed =NO;
                               [nav pushViewController:self->m_pdf animated:YES];
                           }
                           else if(result == 2)
                           {
                               NSString *str1=NSLocalizedString(@"Alert", @"Localizable");
                               NSString *str2=NSLocalizedString(@"Error PassWord", @"Localizable");
                               [self presentPwdAlertControllerWithTitle:str1 message:str2];
                           }
                   }
                   else
                   {
                       [self presentViewController:pwdAlert animated:YES completion:nil];
                   }
               }];
               UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Localizable"style:UIAlertActionStyleCancel handler:nil];
               [pwdAlert addAction:okAction];
               [pwdAlert addAction:cancel];
               [self presentViewController:pwdAlert animated:YES completion:nil];
           }
           else if (result == 0)
           {
               NSString *str1=NSLocalizedString(@"Alert", @"Localizable");
               NSString *str2=NSLocalizedString(@"Error Document,Can't open", @"Localizable");
               NSString *str3=NSLocalizedString(@"OK", @"Localizable");
               UIAlertController* alert = [UIAlertController alertControllerWithTitle:str1
                                          message:str2
                                          preferredStyle:UIAlertControllerStyleAlert];
               UIAlertAction *okAction = [UIAlertAction actionWithTitle:str3 style:UIAlertActionStyleDefault handler:nil];
               [alert addAction:okAction];
               [self presentViewController:alert animated:YES completion:nil];
           }
           [theLock unlock];
       }
}

add: 

[self refreshDocuments];

in  - (void)viewDidLoad

Create a method to open a pdf:

-(void)openPDF
{
    for(int i = 0; i <[m_files count]; i++)
    {
        RDFileItem *item = [m_files objectAtIndex:i];
        NSString *fName = item.help;
        if([fName isEqualToString:@"help.pdf"])
        {
            [self pdf_open_path:item :nil];
        }
    }
}

 

Applies To

RadaeePDF SDK for iOS

Related Articles

How to create your own APP with Xamarin (iOS)

How to create your own APP with Swift

Details

Created : 2021-04-13 17:56:07, Last Modified : 2021-04-13 18:20:22