8 Mar 2017

Implementation of Document Pickers or Pick an iCloud document...



Implementation of  Document Pickers...


  https://www.macstories.net/tutorials/implementing-ios-8-document-pickers/



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIDocumentInteractionControllerDelegate>

@property UIDocumentInteractionController *documentInteractionController;

@end




#import "ViewController.h"


@interface ViewController ()<UIDocumentPickerDelegate>
- (IBAction)clickAction:(id)sender;
@property (strong, nonatomic) IBOutlet UIImageView *image;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)clickAction:(id)sender {
    
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.composite-content"] inMode:UIDocumentPickerModeOpen];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:documentPicker animated:YES completion:nil];
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller;{
    return self;
}

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url{
    NSLog(@"-->%@<---->%@",url.absoluteString,url.lastPathComponent);
    
    
    NSString *path = [url path];
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
    
    NSString *base64String = [data base64EncodedStringWithOptions:0];

    base64String = [base64String stringByReplacingOccurrencesOfString:@"/"
                                                           withString:@"_"];
    
    base64String = [base64String stringByReplacingOccurrencesOfString:@"+"
                                                           withString:@"-"];
    
//    NSLog(@"---->>%@",base64String);
    
    UIImage *image = [UIImage imageWithData:data];
    [_image setImage:image];
    
    /*This is for preview*/
    
    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
    
    // Configure Document Interaction Controller
    [self.documentInteractionController setDelegate:self];
    
    // Preview PDF
    [self.documentInteractionController presentPreviewAnimated:YES];
}

@end




No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0