代码之家  ›  专栏  ›  技术社区  ›  Will Harris

如何判断可可触摸设备是否可以打电话?[复制品]

  •  7
  • Will Harris  · 技术社区  · 17 年前

    可能重复:
    iOS - Detecting whether or not device support phone calls?

    我正在编写一个iPhone应用程序,它提供了一个呼叫电话号码的按钮。我用下面的代码用 tel: 通常的网址:

    NSURL* contactTelURL = [NSURL
                            URLWithString:[NSString
                                           stringWithFormat:@"tel:%@",
                                           contactTel]];
    [[UIApplication sharedApplication] openURL:contactTelURL];
    

    它在真正的iPhone上运行良好,但我在模拟器中得到了一个“不支持的URL”警报。这大概也会发生在iPod touch上,尽管我还没有测试过。在不打电话的设备上运行时,最好删除按钮。

    有没有一种方法可以通过编程检测可可触摸设备是否可以打电话?

    4 回复  |  直到 8 年前
        1
  •  -1
  •   Vadim Kotov First Zero    8 年前

    你可以查询 [[UIDevice currentDevice] model] ,然后检查它是否是iPhone。

        2
  •  40
  •   Community Mohan Dere    9 年前

    来自诺亚威瑟斯彭 Make a call from my iPhone application

    模拟器不支持很多iOS的URL方案,包括手机、地图、YouTube和短信应用程序的URL方案。对于iPod touch和iPad等没有电话功能的设备也是如此;在通过-openurl:使用任何URL方案之前,您应该使用-canopenurl:检查该方案的支持情况,根据当前设备是否支持您使用的URL方案,该方案将返回yes或no。

    所以查询 [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]] 以确定设备是否可以拨打电话。

        3
  •  7
  •   Kriem    17 年前

    iphonedevelopment.blogspot.com

    #import <sys/utsname.h>
    
    enum {
        MODEL_IPHONE_SIMULATOR,
        MODEL_IPOD_TOUCH,
        MODEL_IPHONE,
        MODEL_IPHONE_3G
    };
    
    @interface DeviceDetection : NSObject
    
    + (uint) detectDevice;
    + (NSString *) returnDeviceName:(BOOL)ignoreSimulator;
    
    @end
    
    
    @implementation DeviceDetection
    
    + (uint) detectDevice {
        NSString *model= [[UIDevice currentDevice] model];
    
        // Some iPod Touch return "iPod Touch", others just "iPod"
    
        NSString *iPodTouch = @"iPod Touch";
        NSString *iPodTouchLowerCase = @"iPod touch";
        NSString *iPodTouchShort = @"iPod";
    
        NSString *iPhoneSimulator = @"iPhone Simulator";
    
        uint detected;
    
        if ([model compare:iPhoneSimulator] == NSOrderedSame) {
            // iPhone simulator
            detected = MODEL_IPHONE_SIMULATOR;
        } else if ([model compare:iPodTouch] == NSOrderedSame) {
            // iPod Touch
            detected = MODEL_IPOD_TOUCH;
        } else if ([model compare:iPodTouchLowerCase] == NSOrderedSame) {
            // iPod Touch
            detected = MODEL_IPOD_TOUCH;
        } else if ([model compare:iPodTouchShort] == NSOrderedSame) {
            // iPod Touch
            detected = MODEL_IPOD_TOUCH;
        } else {
            // Could be an iPhone V1 or iPhone 3G (model should be "iPhone")
            struct utsname u;
    
            // u.machine could be "i386" for the simulator, "iPod1,1" on iPod Touch, "iPhone1,1" on iPhone V1 & "iPhone1,2" on iPhone3G
    
            uname(&u);
    
            if (!strcmp(u.machine, "iPhone1,1")) {
                detected = MODEL_IPHONE;
            } else {
                detected = MODEL_IPHONE_3G;
            }
        }
        return detected;
    }
    
    
    + (NSString *) returnDeviceName:(BOOL)ignoreSimulator {
        NSString *returnValue = @"Unknown";
    
        switch ([DeviceDetection detectDevice]) {
            case MODEL_IPHONE_SIMULATOR:
                if (ignoreSimulator) {
                    returnValue = @"iPhone 3G";
                } else {
                    returnValue = @"iPhone Simulator";
                }
                break;
            case MODEL_IPOD_TOUCH:
                returnValue = @"iPod Touch";
                break;
            case MODEL_IPHONE:
                returnValue = @"iPhone";
                break;
            case MODEL_IPHONE_3G:
                returnValue = @"iPhone 3G";
                break;
            default:
                break;
        }        
        return returnValue;
    }
    
    @end
    
        4
  •  0
  •   Josh Peak    15 年前

    这里有一个简单的代码片段,我用它来检查设备型号是否是一部手机而不是一个模拟器,以确保它可以打电话。

    if ([[[UIDevice currentDevice] model] rangeOfString:@"Phone"].location != NSNotFound &&
        [[[UIDevice currentDevice] model] rangeOfString:@"Simulator"].location == NSNotFound ) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", number]  ] ];
    }