GithubHelp home page GithubHelp logo

hsgoogledrivepicker's Introduction

HSGoogleDrivePicker

A sane and simple file picker for Google Drive.

Google makes it ridiculously painful to select a file from Google Drive.

For many use-cases, all you want is to present a picker, and get a notification when your user has selected a file.

This is the API that Google should have written.

Picker Screenshot

Example

import HSGoogleDrivePicker

let picker = HSDrivePicker()

picker.pick(from: self) {
    (manager, file) in

    print("picked file: \(file?.name ?? "-none-")")
}

Updating from 2.0 to 3.0

  • Use the picker initialisation above
  • Follow the ‘Configure the sign in process’ section below
  • Note that GTLDriveFile has changed to GTLRDrive_File in the callback

Installing HSGoogleDrivePicker

You can install HSGoogleDrivePicker in your project by using CocoaPods

pod 'HSGoogleDrivePicker', '~> 3.0

Getting your API keys

  • You need to create an app in the Google APIs and Services dashboard
  • The easiest way if you're using other Google APIs (like firebase, admob) is to add the permission to one of those projects using Google's Wizard
  • Otehrwise you can follow Google's instructions
  • Enable the Drive API permission. (click on ‘APIs and Auth’, ‘APIs’, then search for ‘Drive’)

Note - Google's instructions are confusing, and change frequently. Don't give up!

Configure the sign in process

  • Download a the configuration file for your app

  • Add the configuration file to your project

  • Or manually configure GoogleSignIn by calling GIDSignIn.sharedInstance().clientID = "YOUR_CLIENT_ID" in your appDelegate

  • Add a URL scheme to your project

  1. Open your project configuration: double-click the project name in the left tree view. Select your app from the TARGETS section, then select the Info tab, and expand the URL Types section.
  2. Click the + button, and add a URL scheme for your reversed client ID. To find this value, open the GoogleService-Info.plist configuration file, and look for the REVERSED_CLIENT_ID key. Copy the value of that key, and paste it into the URL Schemes box on the configuration page. Leave the other fields blank.

When completed, your config should look something similar to the following (but with your application-specific values)

Picker Screenshot

  • Handle the url callback in your app delegate

In YourAppDelegate.m

import HSGoogleDrivePicker

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

    if HSDrivePicker.handle(url) {
        return true
    }

    //Your code for other callbacks

    return true
}

Usage

Create and show the picker

import HSGoogleDrivePicker

let picker = HSDrivePicker()

picker.pick(from: self) {
    (manager, file) in

    print("picked file: \(file?.name ?? "-none-")")
}

The completion handler returns with a GTLRDrive_File which has all the info you need.

To download the file, use

manager?.downloadFile(file, toPath: destinationPath, withCompletionHandler: {
    error in

    if error != nil {
        print("Error downloading : \(error?.localizedDescription ?? "")")
    } else {
        print("Success downloading to : \(destinationPath)")
    }
})

Status

I welcome pull requests.

License

HSGoogleDrivePicker is available under the MIT license. See the LICENSE file for more info.

hsgoogledrivepicker's People

Contributors

azakush avatar confusedvorlon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hsgoogledrivepicker's Issues

File not downloading

I used below code for download file, file downloading work well when file name have extension (like abc.png, xyz.docx, aaa.pdf).
if any file has no extension in name (like abc, xyz, aaa), that file is not downloading,
it gives error : The operation couldn’t be completed. (com.google.HTTPStatus error 403.)

HSDrivePicker *picker = [[HSDrivePicker alloc] initWithSecret:googleDriveClientSecret];

[picker pickFromViewController:self withCompletion:^(HSDriveManager *manager, GTLDriveFile *file) {
     [manager downloadFile:file toPath:tmpstrmahesh withCompletionHandler:^(NSError *error) {
           
                if (error)
                {
                    NSLog(@"Error downloading : %@", error.localizedDescription);
                }
                else
                {
                    //success code
                }
            }];
}];

sign-in shows twice in iOS8

open picker
sign in
click allow
signin view disappears

expected:
everything is splended

actual:
signin view shows again

API change

Hello Google Drive Developer,

We have identified you as a Developer who has used the Drive API in the last 30 days. We are writing to let you know that on September 13, 2021, Drive will apply a security update that will change the links used to share some files, and may lead to some new file access requests. Access to files won’t change for people who have already viewed or modified these files.

Please update your code as detailed below before September 13, 2021, to avoid failing requests.

What do I need to know?
Items that have a Drive API permission with type=domain or type=anyone, where withLink=true (v2) or allowFileDiscovery=false (v3), will be affected by this security update.

In addition to the item ID, your application may now also need a resource key to access these items. Without a resource key, requests for these items may result in a 404 Not Found error (See below for details). Note that access to items that are directly shared with the user or group are not affected.

Will this change affect me?
If your application uses the Drive API to access files which have been shared with a user through link sharing, your application may be affected by this change.

What do I need to do?
To avoid errors accessing files, you must update your code for accessing files to include the appropriate resource keys. Details on how to do this for each of the affected Drive APIs is included below:

Changes to the Drive API
The resource key of an item is returned on the resourceKey field of the file metadata in the Drive API response.

If the file is a shortcut file, then the resource key for the target of the shortcut can be read from the shortcutDetails.targetResourceKey field of the same resource.
URL type fields such as exportLinks, webContentLink, and webViewLink will include the resourceKey.
Requests to the Drive API can specify one or more resource keys with the X-Goog-Drive-Resource-Keys HTTP request header.
Learn more about this change from the Drive API guide.

Picker is loaded empty

When I downloaded and run your code after all the instructions , its working but when I implement in my actual project I get an empty picker .I don't get any crashes and I don't get a way to solve this problem

Sign out not available

Hi,

Is there any way to sign out from google drive, so another user can sign in ?

Regards,
Morgan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.