2012-11-19 12 views
5

मैंने अपने आईओएस ऐप के साथ Google drivs sdk एकीकृत किया। वर्तमान में डाउनलोड यूआरएल लिंक के आधार पर मेरे Google ड्राइव ए/सी से फ़ाइलों को डाउनलोड करने के लिए निम्न कोड का उपयोग कर रहा हूं। लेकिन जब मैं Google डॉक्स फ़ाइलों को डाउनलोड करने का प्रयास करता हूं (जिसमें माइम प्रकार एप्लिकेशन/vnd.google-apps.document है) Google ड्राइव लाइब्रेरी से कोई डाउनलोड यूआरएल लिंक नहीं है। उस स्थिति में मैं Google डॉक्स डेटा कैसे डाउनलोड कर सकता हूं? क्या मैं यूआरएल लिंक डाउनलोड करने के बजाय alternateLink का उपयोग कर सकता हूं? किसी भी मदद की सराहना की जानी चाहिए।आईओएस: आईओएस गूगल ड्राइव एसडीके एपीआई का उपयोग कर Google डॉक्स फाइलें कैसे डाउनलोड करें?

मेरे कोड:

- (void)loadFileContent { 

GTMHTTPFetcher *fetcher = 
[self.driveService.fetcherService fetcherWithURLString:[[self.driveFiles objectAtIndex:selectedFileIdx] downloadUrl]]; 

[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) { 
    if (error == nil) { 
     NSLog(@"\nfile %@ downloaded successfully from google drive", [[self.driveFiles objectAtIndex:selectedFileIdx] originalFilename]); 

     //saving the downloaded data into temporary location 

    } else { 
     NSLog(@"An error occurred: %@", error);    

    } 
}]; 

} Google डॉक्स देशी स्वरूपों में

+0

क्या आपने फ़ाइलों को डाउनलोड करने का प्रबंधन किया था? मैं जानना चाहता हूं कि इसे कैसे किया गया। मैं फ़ाइलों को डाउनलोड करने का भी प्रयास कर रहा हूं, लेकिन अब तक मैं कोई संसाधन ढूंढने में असमर्थ हूं जो इससे मेरी मदद करेगा। – Shailesh

+0

@ शैलेश, हां, मैं Google ड्राइव से फ़ाइलों को डाउनलोड करने में सक्षम हूं। मेरे मामले में जब मैंने फ़ाइल नाम पर उपयोगकर्ता टैप किया तो मैंने डाउनलोड ऑपरेशन शुरू किया। आप किस समस्या का सामना कर रहे हैं? – loganathan

+0

मैं कोड का उपयोग कर रहा हूं Google ने अपने नमूने में से एक प्रदान किया है। प्रोजेक्ट का नाम "ड्राइवसमूह" है, मुझे निर्यात/डाउनलोड यूआरएल नल मिल रहा है। यह पूरी तरह से डाउनलोड करने में विफल रहता है। – Shailesh

उत्तर

3

दस्तावेज अन्य फ़ाइलों के रूप में डाउनलोड नहीं किया जा सकता है, लेकिन केवल exportLinks URL का उपयोग विभिन्न समर्थित स्वरूपों को निर्यात किया।

https://developers.google.com/drive/manage-downloads#downloading_google_documents

+0

मैंने प्रलेखन की जांच की। लेकिन वहां मुझे ऑब्जेक्टिस-सी से संबंधित कोई उदाहरण नहीं मिल रहा है। तो क्या आप कृपया मुझे बता सकते हैं कि मैं GTLDriveFileExportLinks वर्ग से निर्यात प्रकार एप्लिकेशन/पीडीएफ कैसे प्राप्त कर सकता हूं। – loganathan

+2

ऑब्जेक्टिव-सी नमूना http://code.google.com/p/google-api-objectivec-client/source/browse/trunk/Examples/DriveSample/DriveSampleWindowController.m?#691 फ़ाइलों को डाउनलोड या निर्यात करने का तरीका दिखाता है –

+0

धन्यवाद :-) उपरोक्त लिंक ने मेरे problerm हल किया :-) – loganathan

1

पहले भी ऐसी ही समस्या थी:

अधिक जानकारी और समर्थित स्वरूपों की सूची के लिए Google डिस्क का SDK दस्तावेज़ की जाँच करें। विशेष रूप से, मुझे Google दस्तावेज़ में बनाए गए मूल दस्तावेज़ों के लिए downloadurl नहीं मिला। वे शून्य हैं। केवल डॉईडिट के माध्यम से बनाए गए (ड्राइव एसडीके में उदाहरण समाधान) डाउनलोड यूआरएल से जुड़े हुए हैं।

समाधान वास्तव में विशेषता में अंतर्निहित है: JSONमें GTLDriveFileExportLinks उदाहरण है, जो NSMutableDictionary * देता है। आप JSON ऑब्जेक्ट की सामग्री को JSONString विशेषता के माध्यम से देखना चुन सकते हैं। JSON mutatable शब्दकोश क्वेरी लिंक्सGTLDriveFile उदाहरण में पूछताछ के माध्यम से प्राप्त किया जा सकता है। उदाहरण नीचे दिया गया है:

GTLDriveFile *file = files.items[0]; // assume file is assigned to a valid instance. 
NSMutableDictionary *jsonDict = file.exportLinks.JSON; 
NSLog(@"URL:%@.", [jsonDict objectForKey:@"text/plain"]); 
5

यहां Google ड्राइव से फ़ाइल डाउनलोड करने के लिए चरण हैं। यह दोनों फाइलों और Google डॉक्स के लिए भी काम करेगा।

चरण 1:

फ़ाइल सूची लाएं और यह एक सरणी या संबंधित फाइल डाउनलोड लिंक यूआरएल साथ dict में स्टोर:

- (void)loadDriveFiles { 
fileFetchStatusFailure = NO; 

//for more info about fetching the files check this link 
//https://developers.google.com/drive/v2/reference/children/list  
GTLQueryDrive *query2 = [GTLQueryDrive queryForChildrenListWithFolderId:[parentIdList lastObject]]; 
query2.maxResults = 1000; 

// queryTicket can be used to track the status of the request. 
[self.driveService executeQuery:query2 
       completionHandler:^(GTLServiceTicket *ticket, 
            GTLDriveChildList *children, NSError *error) { 
        GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery];      
        //incase there is no files under this folder then we can avoid the fetching process 
        if (!children.items.count) {       
         [self.driveFiles removeAllObjects]; 
         [fileNames removeAllObjects];       
         [self performSelectorOnMainThread:@selector(reloadTableDataFromMainThread) withObject:nil waitUntilDone:NO];       
         return ; 
        } 

        if (error == nil) { 
         int totalChildren = children.items.count; 
         count = 0; 

         [self.driveFiles removeAllObjects]; 
         [fileNames removeAllObjects];             //http://stackoverflow.com/questions/14603432/listing-all-files-from-specified-folders-in-google-drive-through-ios-google-driv/14610713#14610713 
         for (GTLDriveChildReference *child in children) { 
          GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];        
          query.completionBlock = ^(GTLServiceTicket *ticket, GTLDriveFile *file, NSError *error) { 

           //increment count inside this call is very important. Becasue the execute query call is asynchronous 
           count ++; 
           NSLog(@"Google Drive: retrieving children info: %d", count);         
           if (error == nil) { 
            if (file != nil) { //checking the file resource is available or not 
             //only add the file info if that file was not in trash 
             if (file.labels.trashed.intValue != 1) 
              [self addFileMetaDataInfo:file numberOfChilderns:totalChildren]; 
            } 

            //the process passed all the files then we need to sort the retrieved files 
            if (count == totalChildren) { 
             NSLog(@"Google Drive: processed all children, now stopping HUDView - 1"); 
             [self performSelectorOnMainThread:@selector(reloadTableDataFromMainThread) withObject:nil waitUntilDone:NO]; 
            } 
           } else { 
            //the file resource was not found 
            NSLog(@"Google Drive: error occurred while retrieving file info: %@", error); 

            if (count == totalChildren) { 
             NSLog(@"Google Drive: processed all children, now stopping HUDView - 2"); 
             [self performSelectorOnMainThread:@selector(reloadTableDataFromMainThread) 
                  withObject:nil waitUntilDone:NO]; 
            }          
           }         
          };        
          //add the query into batch query. Since we no need to iterate the google server for each child. 
          [batchQuery addQuery:query]; 
         }       
         //finally execute the batch query. Since the file retrieve process is much faster because it will get all file metadata info at once 
         [self.driveService executeQuery:batchQuery 
            completionHandler:^(GTLServiceTicket *ticket, 
                 GTLDriveFile *file, 
                 NSError *error) { 
            }]; 

         NSLog(@"\nGoogle Drive: file count in the folder: %d", children.items.count); 
        } else { 
         NSLog(@"Google Drive: error occurred while retrieving children list from parent folder: %@", error); 
        } 
       }]; 

}

कदम-2: फ़ाइल जोड़ने मेटाडाटा जानकारी

-(void)addFileMetaDataInfo:(GTLDriveFile*)file numberOfChilderns:(int)totalChildren 
{ 
    NSString *fileName = @""; 
    NSString *downloadURL = @""; 

    BOOL isFolder = NO; 

    if (file.originalFilename.length) 
     fileName = file.originalFilename; 
    else 
     fileName = file.title; 

    if ([file.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) { 
     isFolder = YES; 
    } else { 
     //the file download url not exists for native google docs. Sicne we can set the import file mime type 
     //here we set the mime as pdf. Since we can download the file content in the form of pdf 
     if (!file.downloadUrl) { 
      GTLDriveFileExportLinks *fileExportLinks; 

      NSString *exportFormat = @"application/pdf"; 

      fileExportLinks = [file exportLinks]; 
      downloadURL = [fileExportLinks JSONValueForKey:exportFormat]; 
     } else { 
      downloadURL = file.downloadUrl; 
     } 
    } 

    if (![fileNames containsObject:fileName]) { 
     [fileNames addObject:fileName]; 

     NSArray *fileInfoArray = [NSArray arrayWithObjects:file.identifier, file.mimeType, downloadURL, 
            [NSNumber numberWithBool:isFolder], nil]; 
     NSDictionary *dict = [NSDictionary dictionaryWithObject:fileInfoArray forKey:fileName]; 

     [self.driveFiles addObject:dict]; 
    } 
} 

कदम-3: फ़ाइल पंक्ति के आधार पर फ़ाइल डाउनलोड करें तालिका

NSString *downloadUrl = [[[[self.driveFiles objectAtIndex:selectedFileIdx] allValues] objectAtIndex:0] 
        objectAtIndex:download_url_link]; 
NSLog(@"\n\ngoogle drive file download url link = %@", downloadUrl);  
GTMHTTPFetcher *fetcher = 
[self.driveService.fetcherService fetcherWithURLString:downloadUrl];  
//async call to download the file data 
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) { 
    if (error == nil) { 
     NSLog(@"\nfile %@ downloaded successfully from google drive", self.selectedFileName); 

     //saving the downloaded data into temporary location 
     [data writeToFile:<path> atomically:YES];    
    } else { 
     NSLog(@"An error occurred: %@", error); 
    } 
}]; 
+2

धन्यवाद! इसने काम कर दिया। :) हालांकि मुझे लगता है, Google को उनके एपीआई दस्तावेज पर काम करने की जरूरत है। हमारी परियोजनाओं में लागू करने के लिए जटिल तरीका है। ड्रॉपबॉक्स, दूसरी ओर SkyDrive सरल और सुंदर सीधे आगे की प्रक्रिया है। – Shailesh

+0

@ शैलेश, बहुत बढ़िया !!! :-) हाँ आप सही हैं :-) जब मैंने Google ड्राइव को एकीकृत करना शुरू किया तो मुझे बहुत सामना करना पड़ा। – loganathan

+0

@loganathan क्या यह ब्लॉक ऐप क्रैश हो जाएगा यदि डेटा 1 जीबी अधिक होगा? –

संबंधित मुद्दे