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

2019-07-07 03:08发布

问题:

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?

回答1:

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.



回答2:

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

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