我想知道是否有人可以深入了解我在升级我的应用程序时希望进行的数据迁移。我正在使用 jquery mobile 将一个用 native Objective-C 编写的应用更新为基于 cordova 的 WebView 。
现有应用程序具有使用 sqllite3.h 实现的“收藏夹”。我想知道是否有可能当我们将更新发布到应用程序时,我们可以连接到这个现有的 sqlite 数据库,并将旧的“收藏夹”迁移到新数据库中,这是使用 localStorage 实现的。
非常感谢任何见解!
最佳答案
我们刚刚做了类似的事情。我们的旧 native 应用程序使用的是 CoreData。我们在 MainViewController 的 webViewDidFinishLoad 委托(delegate)方法中添加了一个检查,以查看是否存在现有数据库。如果是,那么我们加载数据库,转换内容以供通过 localStorage 使用,然后设置一个标志,这样它就不会尝试再次导入它。这是将其传输到 localStorage 的代码段。首先,我们将要传输的信息放入 NSMutableString,但将其格式化为 javascript 字典
NSMutableString *dict = [NSMutableString string];
[dict appendString:@"{"];
[dict appendFormat:@"'notes':'%@'", notes];
[dict appendFormat:@",'date':'%f'",seconds];
[dict appendFormat:@",'count':'%d'",[ss.count intValue]];
[dict appendFormat:@",'weather':'%@'",wx];
[dict appendFormat:@",'location':'%@'",ss.event.location.name];
[dict appendFormat:@",'latitude':'%@'",[ss.event.location.latitude stringValue]];
[dict appendFormat:@",'longitude':'%@'",[ss.event.location.longitude stringValue]];
[dict appendString:@"}"];
然后我们像这样将它传递到 javascript 中:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"importMyOldData(%@);",dict]];
这有效地调用了 javascript 方法,并将项目字典传递给它。然后您可以使用 Javascript 将其放入本地存储,例如:
function importMyOldData( oldData )
{
// if you want to inspect each item
var notes = oldData.notes;
localStorage.setItem( "NoteKey", notes );
// if you want to dump the whole thing
localStorage.setItem( "MyKey", JSON.stringify(oldData) );
}
关于iOS 应用程序迁移 - 将旧的 native 应用程序更新到 Cordova,迁移数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30356846/