2017-02-14 31 views
0

我用下面的代码成功发送iOS设备之间的数据库文件:如何移动文件在对收件箱文件夹使用空投iOS设备之间。当

-(void) doSendDatabase { 
UIView *viewTemp = [[UIView alloc] init]; 
viewTemp.frame = CGRectMake(0.0f, 0.0f, 300, 300); 

NSString *currentDatabaseName; 

// This is the full path and file name with ext 
currentDatabaseName = [self.databases objectAtIndex:[[mainTableView indexPathForSelectedRow] row]]; 

NSURL *url = [[NSURL alloc] initFileURLWithPath:currentDatabaseName]; 

UIActivityViewController * airDrop = [[UIActivityViewController alloc] 
             initWithActivityItems:@[url] 
             applicationActivities:nil];  

airDrop.popoverPresentationController.sourceView = self.view; 

[self presentViewController:airDrop 
        animated:YES 
       completion:nil]; 

[url release]; 
[airDrop release]; 
[viewTemp release];} 

此代码和数据库成功会从发将iOS设备发送到接收设备。但是,数据库存储在Documents/Inbox文件夹中(按照设计我想)。我只是想将接收到的数据库文件从收件箱文件夹上移到一个文档文件夹中。从我正在阅读的内容中,我需要在App Delegate的openURL中处理这个问题 - 但我不知道如何解决这个问题。任何帮助将不胜感激。

谢谢。

回答

1

好的 - 这是我所做的解决问题。 (1)我在App Delegate中创建了一个handleInboxItems方法。

-(bool) handleInboxItems { 
bool success = YES; 
// Get the DBAccess object 
DBAccess *dbAccess = [[DBAccess alloc] init]; 
// Get the Func object 
Func *funcObject = [[Func alloc] init]; 

NSMutableArray *docDatabases; 
// get a list of all database files in the Documents/Inbox folder ans store them in the inboxDatabases array 
NSMutableArray *inboxDatabases = [[NSMutableArray alloc] init]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *inboxDirectory = [documentsDirectory stringByAppendingPathComponent:@"Inbox"]; 
NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:inboxDirectory]; 
for (NSString *inboxFileAndPath in directoryEnumerator) 
{ 
    //check to see if any of the files in the inbox folder end in the database extension - if so then save it in the inboxDatabases array 
    if ([[inboxFileAndPath pathExtension] isEqualToString:@“dbext”]) 
    { 
     [inboxDatabases addObject:[inboxDirectory stringByAppendingPathComponent:inboxFileAndPath]]; 
    } 
} 

// now go through the inboxDatabases array and copy them from the Documents/Inbox folder to the Documents folder 

// loop through all inbox database and see if any of the database names already exist in Documents - if so then we need to tack on a sequential number 
for (NSString *inboxDatabaseFileAndPath in inboxDatabases) 
{ 
    NSString *inboxDatabaseName = [[inboxDatabaseFileAndPath lastPathComponent] stringByDeletingPathExtension]; 

    // Get the databases array from the DBAccess class (from the Documents folder) - need to get each time since we are moving files in there 
    docDatabases = [dbAccess getAllDatabases]; 

    // does the inbox database already exist in the documents folder? 
    NSUInteger arrayIndex = [docDatabases indexOfObject:[funcObject databaseNameToFullPathName:allTrim(inboxDatabaseName)]]; 

    int i = 0; 
    while (arrayIndex != NSNotFound) 
    { 
     ++i; 
     NSString *tempDatabaseName = [NSString stringWithFormat:[inboxDatabaseName stringByAppendingString:@" %d"],i]; 

     // see if the database (with sequential number) already exists 
     arrayIndex = [docDatabases indexOfObject:[funcObject databaseNameToFullPathName:allTrim(tempDatabaseName)]]; 
     if (arrayIndex == NSNotFound) 
     { 
      // it does not exist, we can use this name 
      inboxDatabaseName = tempDatabaseName; 
     } 
    } 
    // give it full path and extension 
    NSString *docDatabaseFileAndPathToWrite = [funcObject databaseNameToFullPathName:allTrim(inboxDatabaseName)]; 
    NSError *error; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    success = [fileManager copyItemAtPath:inboxDatabaseFileAndPath toPath:docDatabaseFileAndPathToWrite error:&error]; 
    if (success) 
    { 
     // delete the inbox database file 
     success = [fileManager removeItemAtPath:inboxDatabaseFileAndPath error:&error]; 
     if (!success) 
     { 
      NSAssert1(0,@"Failed to delete inbox database:'%@'.",[error localizedDescription]); 
     }  
    } 
    else 
    { 
     NSAssert1(0,@"Failed to copy inbox database to documents folder:'%@'.",[error localizedDescription]); 
    } 
} 

[dbAccess release]; 
[funcObject release]; 
[inboxDatabases release]; 

return success;} 

(2)增加了一个调用这个新方法在App代表的didFinishLaunchingWithOptions以防万一有什么事情卡在启动时的收件箱。

(3)我将openURL方法添加到App Delegate以调用handleInboxItems。完成后,我发送通知,以便我可以刷新我的数据库列表。

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ 
bool success = [self handleInboxItems]; 

if (success) 
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_DATABASE_AIRDROPPED object:self]; 

return success;} 

就是这样 - 按我需要的那样工作。

相关问题