iphone 拨打电话的 两种方法

本文分享了两种在iOS应用中拨打电话的方法:一种是通过UIWebView加载电话,符合AppStore规定;另一种是使用私有方法,无法上架AppStore。文章提供了具体的实现代码。



大家想不想在自己的应用中拨打电话呀?打电话可以用openURL:这个API, 如:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];但是当电话结束后,返回的是系统的拨打电话界面,如何才能返回自己的应用呢?这儿有两种方法与大家分享。

第一种是用UIWebView加载电话,这种是合法的,可以上App Store的。

代码如下:

  1. //assumingyouhaveanivartostoreaweakreferencetoaUIWebView:
  2. //UIWebView*phoneCallWebView;
  3. -(void)dialPhoneNumber:(NSString*)aPhoneNumber
  4. {
  5. NSURL*phoneURL=[NSURLURLWithString:[NSStringstringWithFormat:@"tel:%@",aPhoneNumber]];
  6. if(!phoneCallWebView){
  7. phoneCallWebView=[[UIWebViewalloc]initWithFrame:CGRectZero];
  8. }
  9. [phoneCallWebViewloadRequest:[NSURLRequestrequestWithURL:phoneURL]];
  10. }
  11. -(void)dealloc
  12. {
  13. //cleanup
  14. [phoneCallWebViewrelease],phoneCallWebView=nil;
  15. [superdealloc];
  16. }

第二种是私有方法,不能上App Store的。

  1. [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"telprompt://10086"]];

大家想不想在自己的应用中拨打电话呀?打电话可以用openURL:这个API, 如:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];但是当电话结束后,返回的是系统的拨打电话界面,如何才能返回自己的应用呢?这儿有两种方法与大家分享。

第一种是用UIWebView加载电话,这种是合法的,可以上App Store的。

代码如下:

  1. //assumingyouhaveanivartostoreaweakreferencetoaUIWebView:
  2. //UIWebView*phoneCallWebView;
  3. -(void)dialPhoneNumber:(NSString*)aPhoneNumber
  4. {
  5. NSURL*phoneURL=[NSURLURLWithString:[NSStringstringWithFormat:@"tel:%@",aPhoneNumber]];
  6. if(!phoneCallWebView){
  7. phoneCallWebView=[[UIWebViewalloc]initWithFrame:CGRectZero];
  8. }
  9. [phoneCallWebViewloadRequest:[NSURLRequestrequestWithURL:phoneURL]];
  10. }
  11. -(void)dealloc
  12. {
  13. //cleanup
  14. [phoneCallWebViewrelease],phoneCallWebView=nil;
  15. [superdealloc];
  16. }

第二种是私有方法,不能上App Store的。

  1. [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"telprompt://10086"]];

[DllImport("phone.dll")] private static extern IntPtr PhoneMakeCall(ref PhoneMakeCallInfo ppmci); /// /// Dials the specified phone number. /// /// Phone number to dial. public static void MakeCall(string PhoneNumber) { MakeCall(PhoneNumber, false); } /// /// Dials the specified phone number. /// /// Phone number to dial. /// Prompts the user before the call is placed. unsafe public static void MakeCall(string PhoneNumber, bool PromptBeforeCall) { IntPtr res; PhoneNumber += '\0'; char[] cPhoneNumber = PhoneNumber.ToCharArray(); fixed (char* pAddr = cPhoneNumber) { PhoneMakeCallInfo info = new PhoneMakeCallInfo(); info.cbSize = (IntPtr)Marshal.SizeOf(info); info.pszDestAddress = (IntPtr)pAddr; if (PromptBeforeCall) { info.dwFlags = (IntPtr)PMCF_PROMPTBEFORECALLING; } else { info.dwFlags = (IntPtr)PMCF_DEFAULT; } res = PhoneMakeCall(ref info); } } } /// /// Reads information from the Subscriber Identity Module (SIM) /// public class Sim { private static long SERVICE_PROVIDER = 0x00006F46; [StructLayout(LayoutKind.Sequential)] private struct SimRecord { public IntPtr cbSize; public IntPtr dwParams; public IntPtr dwRecordType; public IntPtr dwItemCount; public IntPtr dwSize; } [DllImport("sms.dll")] private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress); [DllImport("cellcore.dll")] private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr lpfnCallBack, IntPtr dwParam, out IntPtr lphSim); [DllImport("cellcore.dll")] private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo); [DllImport("cellcore.dll")] private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData, IntPtr dwBufferSize, ref IntPtr lpdwBytesRead); [DllImport("cellcore.dll")] private static extern IntPtr SimDeinitialize(IntPtr hSim); /// /// Gets the phone number from the SIM. /// /// PhoneAddress structure with phone number. unsafe public static PhoneAddress GetPhoneNumber() { PhoneAddress phoneaddr = new PhoneAddress(); Byte[] buffer = new Byte[516]; fixed (byte* pAddr = buffer) { IntPtr res = SmsGetPhoneNumber((IntPtr)pAddr); if (res != IntPtr.Zero) throw new Exception("Could not get phone number from SIM"); byte* pCurrent = pAddr; phoneaddr.AddressType = (AddressType)Marshal.ReadInt32((IntPtr)pCurrent); pCurrent += Marshal.SizeOf(phoneaddr.AddressType); phoneaddr.Address = Marshal.PtrToStringUni((IntPtr)pCurrent); } return phoneaddr; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值