ios - 如何在一个应用程序中访问另一个应用程序中的sqlite数据库

标签 ios iphone ipad

我有一个应用程序,它创建了 sqlite 数据库并将其存储在文档目录中。现在我从现有应用程序打开其他应用程序,并且我想访问现有的数据库路径。我该怎么做,谁能帮帮我。我写了下面的代码来打开另一个应用程序并调用文档目录。但是它得到了错误的文档目录路径。

==============

UIApplication *ourApplication = [UIApplication sharedApplication];
    NSString *ourPath = @"POSRetail://";
    NSURL *ourURL = [NSURL URLWithString:ourPath];
    if ([ourApplication canOpenURL:ourURL])
    {

        databasePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSLog(@"databasePath : %@",databasePath);
        NSString *filePath = [databasePath stringByAppendingPathComponent:@"testapp.sqlite"];

        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];

        if(fileExists)
        {

            NSMutableString *strRecord =[[NSMutableString alloc]init];
            //Retrieve the values of database
            const char *dbpath = [filePath UTF8String];
            if (sqlite3_open(dbpath, &sdatabase) == SQLITE_OK)
            {
                // NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM employee"];
                // const char *query_stmt = [querySQL UTF8String];
                const char *query_stmt = "Select * from info";
                sqlite3_stmt *statement;
                //const char *query_stmt = "Select * from sqlite_master where type='table'";
                NSMutableArray *arrTemp = [[NSMutableArray alloc] init];
                if(sqlite3_prepare_v2(sdatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK)
                {
                    while (sqlite3_step(statement)==SQLITE_ROW) {

                        [arrTemp addObject:[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 0)]];
                        [strRecord appendFormat:@"%@,",[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 0)]];
                        [arrTemp addObject:[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 1)]];
                        [strRecord appendFormat:@" %@,",[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 1)]];
                        [arrTemp addObject:[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 2)]];
                        [strRecord appendFormat:@" %@,",[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 2)]];
                        [arrTemp addObject:[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 3)]];
                        [strRecord appendFormat:@" %@\n",[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 3)]];
                    }

                    NSLog(@"arrTemp : %@",arrTemp);
                    sqlite3_finalize(statement);
                }
                else
                {
                    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(sdatabase));
                }
                sqlite3_close(sdatabase);
            }
            else
            {
                sqlite3_close(sdatabase);
                NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(sdatabase));
            }
        }
        else
        {
            NSLog(@"SQLITE DATABASE NOT AVAILABLE");
        }
    }
    else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Receiver Not Found" message:@"The Receiver App is not installed. It must be installed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }

最佳答案

就好像你想访问其他 iOS App 数据然后来自 Apple 的两个词>

不可能

由于 iOS 应用程序在 SandBox 环境 中工作,因此在应用程序之间共享数据因此无法直接通过 文件系统 共享数据,但是有还提供了一些共享数据的方法,例如

Custom URL Scheme:

  • iOS 编程 指南现在有一节是关于在两者之间传递数据的 通过让一个应用程序声明某个 URL 前缀 然后 其他应用引用该 URL。对于这一点,你可以看看 这个Apple Documentation .和 Good Thread关于 SO 相关 这个问题。

UIDocumentInteractionController

  • 一个文档交互 Controller ,以及一个委托(delegate)对象, 提供应用内支持以管理用户与文件的交互 本地系统。例如,电子邮件程序可能会使用此类 允许用户预览附件并以其他方式打开它们 应用。使用此类为 预览、打开、复制或打印指定文件。

iCloud API:

  • iCloud 是一项免费服务,让用户可以访问他们的个人 全部内容 他们的设备——通过 Apple ID 自动无线连接。 iCloud 通过 将基于网络的存储与专用 API 相结合,由完整的 与操作系统的集成。苹果提供服务器基础设施, 备份和用户帐户,这样您就可以专注于构建出色的支持 iCloud 的功能 应用程序。

注意:不知道越狱 iOS 设备我得到了引用 This Great Document.

关于ios - 如何在一个应用程序中访问另一个应用程序中的sqlite数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22601802/

相关文章:

ios - 如何打开像照片 native 应用程序一样的相册列表?

ios - 打开带方向的 iOS map 时可以调用 Siri 吗?

iphone - TMX map 文件到底是如何进行 base_64 编码的?

iphone - iPhone 上的 double 与 float

ios - 如何为 iPhone 和 iPad 设置自动布局约束

javascript - window.close() 和 iOS

objective-c - 当我尝试将它设置为 NSString 时,NSURL 被取消

ios - 使用免费 Apple ID 将 Apple iOS Swift App 部署到 iPhone

javascript - 如何立即检测浏览器是否支持js onscroll..

iphone - itunesconnect iPhone 应用程序状态 - 无效的二进制文件