在 ionicframework 或者 phonegap(cordova)里如何在页面里获取设备是 iPhone 还是 iPad
在 index.js 里
// Update DOM on a Received Event
receivedEvent: function(id) {
alert(platform);
修改 oc 代码
方式 1
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
// Black base color for background matches the native apps
theWebView.backgroundColor = [UIColor blackColor];
NSString *os = (IS_IPAD == YES) ? @"ipad":@"iPhone";
NSString *js = [NSString stringWithFormat:@"window.platform='%@'",os];
[theWebView stringByEvaluatingJavaScriptFromString:js];
return [super webViewDidFinishLoad:theWebView];
}
方式 2:变态精简版
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
// Black base color for background matches the native apps
theWebView.backgroundColor = [UIColor blackColor];
[theWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.platform='%@'",((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) == YES) ? @"ipad":@"iPhone"]];
return [super webViewDidFinishLoad:theWebView];
}
附赠一个 tip,使用 webSQL 来保存数据
//open database
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS last_word (id INTEGER PRIMARY KEY AUTOINCREMENT, content Text,date string)');
});