2009-05-24 122 views
7

可能重複:
iOS - Detecting whether or not device support phone calls?如何判斷Cocoa Touch設備是否可以撥打電話?

我正在寫一個iPhone應用程序,它提供了一個按鈕,以撥打電話號碼。我使用如下代碼以通常的方式使用tel: URL撥打的號碼:

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

它可以在一個真正的iPhone很好,但我只是得到一個「不支持的URL」在模擬器警報。據推測,這也會發生在iPod Touch上,儘管我還沒有測試過。在無法撥打電話的設備上運行時移除按鈕將會很好。

有沒有辦法通過編程方式檢測Cocoa Touch設備是否可以撥打電話?

回答

-1

您可以查詢[[UIDevice currentDevice] model],並檢查它是否是iPhone。

+5

這不是首選的解決方案。您應該檢查設備具有哪些功能,而不是針對特定型號進行測試。見neilkimmett的答案。 – 2010-12-20 23:16:45

+0

這是錯誤的答案,這個問題是重複的,正確的答案在這裏提供:http://stackoverflow.com/questions/5094928/ios-detecting-whether-or-not-device-support-phone-calls – 2012-10-09 00:23:42

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 
40

從諾亞·威瑟斯彭在Make a call from my iPhone application

模擬器不支持大量的iOS的URL方案,其中包括爲手機,地圖,YouTube和短信應用。 iPod touch和iPad等設備也沒有電話功能;通過-openURL使用任何URL方案之前:,你應該使用-canOpenURL :,這將是返回檢查支持該方案或NO根據當前設備是否支持你使用

所以URL方案查詢[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]] 找出設備是否可以撥打電話。

0

下面是一個簡單的代碼片段,我用它來檢查設備型號是手機而不是模擬器,以確保它可以撥打電話。

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