How to get iOS user-set device name with Cordova?

2019-07-07 03:06发布

I'm using the org.apache.cordova.device plugin. I see that device.name has been deprecated a while back in favor of device.model. When using device plugin 0.2.12 and iOS 8.1 on iPhone 5S, device.name is undefined.

I still need to get the user's device name, like "Jerry's iPhone", so that the user can see which of their devices are using my app. How to do that with Cordova?

2条回答
Summer. ? 凉城
2楼-- · 2019-07-07 03:57

Your best bet might be modifying the Device plugin (with all the undesired consequences). I was able to get the device name by adding the following line to deviceProperties in ./cordova/platforms/ios/[project name]/Plugins/org.apache.cordova.device/CDVDevice.m:

- (NSDictionary*)deviceProperties
{
    UIDevice* device = [UIDevice currentDevice];
    NSMutableDictionary* devProps = [NSMutableDictionary dictionaryWithCapacity:4];
    [devProps setObject:[device name] forKey:@"name"];    // <------------- new line

You also need to adjust the corresponding Javascript object in ./cordova/plugins/org.apache.cordova.device/www/device.js:

 channel.onCordovaReady.subscribe(function() {
    me.getInfo(function(info) {
        me.name = info.name;             // <---------- new line

device.name may have been deprecated and removed from the plugin, but the assigned name is still a supported attribute as of the iOS SDK 8.1 documentation.

查看更多
虎瘦雄心在
3楼-- · 2019-07-07 03:58

The device plugin does not support the name, however there is another plugin that does:

https://github.com/becvert/cordova-plugin-device-name

查看更多
登录 后发表回答