分享 在 Ionic 或者 Phonegap(cordova)里如何在页面里获取设备是 iPhone 还是 iPad

i5ting · 2014年10月13日 · 最后由 i5ting 回复于 2014年10月14日 · 13326 次阅读

在 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)');
});

#1 楼 @darkbaby123 不是的,在 app.config() 这个地方是最先执行的,在 device ready 状态之前,所以是没法渠道 ionic.Platform 的,不信你试试

#1 楼 @darkbaby123

var app = angular.module('starter', ['ionic', 'starter.controllers','clock.no320.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.hide();
    }
  });
})

.service('magicNumberService', MyService)

.config(function($stateProvider, $urlRouterProvider) {
    // 此处执行的早,无法渠道$ionicPlatform

})

哈哈

需要 登录 后方可回复, 如果你还没有账号请 注册新账号