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

*爱你&永不变心* 提交于 2019-12-10 20:33:42

问题


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



来源:https://stackoverflow.com/questions/26573944/how-to-get-ios-user-set-device-name-with-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!