8 Mar 2017

Dropbox Integration for Objective-C Developers...



Dropbox Integration for Objective-C Developers...


*** First see the link once and follow steps add url Schemes in info.plist file...

Link:

https://dropbox.github.io/dropbox-sdk-obj-c/api-docs/latest/

In AppDelegate.m add this...


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    
        [DBClientsManager setupWithAppKey:@"Your App key"];
    
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    
    DBOAuthResult *authResult = [DBClientsManager handleRedirectURL:url];
    if (authResult != nil) {
        if ([authResult isSuccess]) {
            NSLog(@"Success! User is logged into Dropbox.");
            
        } else if ([authResult isCancel]) {
            NSLog(@"Authorization flow was manually canceled by user!");
        } else if ([authResult isError]) {
            NSLog(@"Error: %@", authResult);
        }
    }
    
//    [DBClientsManager unlinkAndResetClients];
    return NO;

}





#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)dropBoxbtnAction:(id)sender;

@end



#import "ViewController.h"
#import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>
#import "PathListViewController.h"
#import "DropBoxFileObject.h"



#define kDropboxToken @"Your DropBox token"

@interface ViewController ()

@property (nonatomic, strong) NSArray *pathArrays;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.pathArrays = [[NSMutableArray alloc]init];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveDropBoxAccess:)
                                                 name:@"DropBoxAccess"
                                               object:nil];
    
}

-(void)receiveDropBoxAccess:(NSNotification *)notification{
    NSLog(@"Access");
    [self showFileAccess];
}

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


- (IBAction)dropBoxbtnAction:(id)sender {

    
    if ([DBClientsManager authorizedClient]) {
        [self showFileAccess];
    }else{
    
        [DBClientsManager authorizeFromController:[UIApplication sharedApplication]
                                       controller:self
                                          openURL:^(NSURL *url) {
                                              [[UIApplication sharedApplication] openURL:url];
                                          }
                                      browserAuth:YES];
//        [self showFileAccess];
    }
}

-(void)showFileAccess{
    
    DBUserClient *client = [DBClientsManager authorizedClient];
    
//    DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:kDropboxToken];
    
    [[client.filesRoutes listFolder:@""] setResponseBlock:^(DBFILESListFolderResult * _Nullable response, DBFILESListFolderError * _Nullable routeError, DBRequestError * _Nullable error) {
        if (response) {
            
            NSLog(@"ACCESS");
            
            self.pathArrays = response.entries;
            
            [self showView:response.entries];
            
        }
        
    }];
}

-(void)parsePathData:(NSArray<DBFILESMetadata*>*)folderPaths{
    
}

-(void)showView:(NSArray*)array{
    
    PathListViewController *objPath = [self.storyboard instantiateViewControllerWithIdentifier:@"PathListViewController"];
    objPath.pathArrays = array;
    //    UINavigationController *objNav = [[UINavigationController alloc]initWithRootViewController:objPath];
    //    [self presentViewController:objNav animated:YES completion:nil];
    [self presentViewController:objPath animated:YES completion:nil];
    
}

@end


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

@interface DropBoxFileObject : NSObject

@property (nonatomic, strong) NSString *pathName;

@property (nonatomic, strong) NSString *pathDisplay;

@property (nonatomic, strong) NSString *pathLower;

@property (nonatomic, strong) NSString *pathId;

@property (nonatomic, strong) NSString *pathType;

- (id)initWithInfo:(DBFILESMetadata *)info;

@end



#import "DropBoxFileObject.h"

@implementation DropBoxFileObject

- (id)initWithInfo:(DBFILESMetadata *)info{
    self = [super init];
    
    if (self) {
        
        _pathName = info.name;
        
        _pathId = info.parentSharedFolderId;
        
        _pathLower = info.pathLower;
        
        _pathDisplay = info.pathDisplay;
        
    }
    
    return self;
}

@end



DropBoxTableViewCell.h
DropBoxTableViewCell.m
DropBoxTableViewCell.xib



#import <UIKit/UIKit.h>

@interface PathListViewController : UIViewController

@property (nonatomic, strong) NSArray *pathArrays;

- (IBAction)doneButtonPathListVCAction:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end


#import "PathListViewController.h"
#import "DropBoxTableViewCell.h"
#import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>
#import "ViewController.h"

#define kDropboxToken @"Your DropBox token"

@interface PathListViewController ()

@end

@implementation PathListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    static NSString *CellIdentifier = @"DropBoxTableViewCell";
    UINib *nib = [UINib nibWithNibName:@"DropBoxTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pathArrays.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"DropBoxTableViewCell";
    DropBoxTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    
    DBFILESMetadata *fileData = self.pathArrays [indexPath.row];
    
    cell.textLabel.text=[NSString stringWithFormat:@"Path:%@", fileData.name];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DBUserClient *client = [DBClientsManager authorizedClient];
    
//    DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:kDropboxToken];
    
    DBFILESMetadata *fileData = self.pathArrays [indexPath.row];
    
    
    
    
    
    if([self isFileType:fileData.pathLower ])
    {
        // Download to NSData
        
        [[[client.filesRoutes downloadData:fileData.pathLower] setResponseBlock:^(DBFILESFileMetadata * _Nullable response, DBFILESDownloadError * _Nullable routeError, DBRequestError * _Nullable error, NSData * _Nullable fileData) {
            if (response) {
                NSLog(@"%@\n", response);
                NSString *dataStr = [[NSString alloc]initWithData:fileData encoding:NSUTF8StringEncoding];
                NSLog(@"%@\n", dataStr);
            }else {
                
                NSLog(@"%@\n%@\n", routeError, error);
                
            }
        }] setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
            NSLog(@"%lld\n%lld\n%lld\n", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
        }];
        
        
//        [DBClientsManager unlinkAndResetClients];
}else  {
        
        [[client.filesRoutes listFolder:fileData.pathLower] setResponseBlock:^(DBFILESListFolderResult * _Nullable response, DBFILESListFolderError * _Nullable routeError, DBRequestError * _Nullable error) {
            if (response) {
                if(response.entries.count>0){
                    self.pathArrays=response.entries;
                    [self.tableView reloadData];
                }
                NSLog(@"ACCESS....");

            } queue:[NSOperationQueue new];
        
        
        }];
        
    }
}

- (BOOL)isFileType:(NSString *)itemName {
    NSRange range = [itemName rangeOfString:@"\\.pdf|\\.doc|\\.txt" options:NSRegularExpressionSearch];
    return range.location != NSNotFound;
}

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                   inDomains:NSUserDomainMask] lastObject];
}

- (IBAction)doneButtonPathListVCAction:(id)sender {
    
    [DBClientsManager unlinkAndResetClients];
    ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [self presentViewController:VC animated:YES completion:nil];
    
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end














No comments:

Post a Comment

Recent Posts

Codable demo

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