如何判断Cocoa Touch设备是否可以拨打电话?

Wil*_*ris 7 cocoa-touch objective-c url-scheme ipod-touch ios

可能重复:
iOS - 检测设备是否支持电话呼叫?

我正在编写一个iPhone应用程序,它提供了一个拨打电话号码的按钮.我正在使用以下代码以tel:常规方式使用URL 拨号:

NSURL* contactTelURL = [NSURL
                        URLWithString:[NSString
                                       stringWithFormat:@"tel:%@",
                                       contactTel]];
[[UIApplication sharedApplication] openURL:contactTelURL];
Run Code Online (Sandbox Code Playgroud)

它在真正的iPhone上工作正常,但我只是在模拟器中得到一个"不支持的URL"警报.据推测,这也会发生在iPod Touch上,尽管我还没有测试过.在不能拨打电话的设备上运行时删除按钮会很不错.

有没有办法以编程方式检测Cocoa Touch设备是否可以拨打电话?

nei*_*ett 40

来自我的iPhone应用程序拨打电话的 Noah Witherspoon

模拟器不支持很多iOS的URL方案,包括电话,地图,Youtube和SMS应用程序.对于没有手机功能的iPod touch和iPad等设备也是如此; 在通过-openURL:使用任何URL方案之前,您应该使用-canOpenURL:检查该方案的支持,它将返回YES或NO,具体取决于当前设备是否支持您正在使用的URL方案

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


Kri*_*iem 7

来自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
Run Code Online (Sandbox Code Playgroud)


hen*_*g77 -1

你可以查询一下[[UIDevice currentDevice] model],看看是不是iPhone。

  • 这不是首选解决方案。您应该检查设备具有哪些功能,而不是测试特定型号。请参阅尼尔基米特的回答。 (5认同)