【问题标题】:How to tell if Cocoa Touch device can make calls? [duplicate]如何判断 Cocoa Touch 设备是否可以拨打电话? [复制]
【发布时间】:2010-10-28 14:03:29
【问题描述】:

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

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

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

它在真正的 iPhone 上运行良好,但我只是在模拟器中收到“不支持的 URL”警报。可能这也会发生在 iPod Touch 上,尽管我还没有测试过。在不会拨打电话的设备上运行时移除按钮会很好。

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

【问题讨论】:

    标签: ios objective-c cocoa-touch url-scheme ipod-touch


    【解决方案1】:

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

    【讨论】:

    • 这不是首选的解决方案。您应该检查设备具有哪些功能,而不是测试特定型号。请参阅 neilkimmett 的回答。
    • 这是错误答案,这个问题是重复的,这里提供正确答案:stackoverflow.com/questions/5094928/…
    • 这并不能告诉您设备是否可以拨打电话(即它有 SIM 卡、是否在网络上等)
    • @MadsMobæk 你的意思是说你应该做一个 API 检查,看看是否有一个用于电话呼叫的 API 可用,如果有就拨打电话?
    【解决方案2】:

    这是一个简单的代码 sn-p 我用来检查设备型号是手机而不是模拟器,以确保它可以拨打电话。

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 这对我来说是正确的答案。在调用 -[UIApplication openURL] 之前,它应该始终检查 -[UIApplication canOpenURL]
      • 这在我运行 iOS8.1 的 iPad 2 上返回 TRUE。然而,在 8.1 模拟器上,它返回 FALSE。所以这在实际设备上不起作用。
      • 这可能与 iOS 8 中添加的连续性功能有关?
      【解决方案4】:

      来自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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-11
        • 2022-06-12
        相关资源
        最近更新 更多