0

我正在使用以下代码在 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 中处理这个 - 但我不确定如何去做。任何帮助将不胜感激。

谢谢你。

4

1 回答 1

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 Delegate 的 didFinishLaunchingWithOptions 中添加了对这个新方法的调用,以防启动时收件箱有任何卡住。

(3) 我在 App Delegate 中添加了 openURL 方法,以便调用 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;}

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

于 2017-02-16T17:56:41.773 回答