【发布时间】:2011-01-19 17:43:24
【问题描述】:
如何在应用内拨打电话或在通话结束后立即启动应用?我知道这是可能的,因为应用商店中的一些应用已经在这样做了。
【问题讨论】:
-
使用 webview 拨打电话。它在这个问题中得到了正确解释:stackoverflow.com/questions/5317783/…
标签: iphone phone-call
如何在应用内拨打电话或在通话结束后立即启动应用?我知道这是可能的,因为应用商店中的一些应用已经在这样做了。
【问题讨论】:
标签: iphone phone-call
这是通过使用 telprompt 而不是 tel 来完成的。请看下面的代码
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt:18004912200"]];
【讨论】:
我从 Apple 网站获得了这段代码,它运行良好:
-(IBAction) dialNumber:(id)sender{
NSString *aPhoneNo = [@"tel://" stringByAppendingString:[itsPhoneNoArray objectAtIndex:[sender tag]]] ; NSURL *url= [NSURL URLWithString:aPhoneNo];
NSURL *url= [NSURL URLWithString:aPhoneNo];
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
if ([osVersion floatValue] >= 3.1) {
UIWebView *webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
webview.hidden = YES;
// Assume we are in a view controller and have access to self.view
[self.view addSubview:webview];
[webview release];
} else {
// On 3.0 and below, dial as usual
[[UIApplication sharedApplication] openURL: url];
}
}
【讨论】:
我认为这有两个部分
在第一种情况下,您的UIApplicationDelegate 将收到消息application:willChangeStatusBarFrame:、application:didChangeStatusBarFrame:、applicationWillResignActive: 和applicationDidBecomeActive: 都可能多次收到消息,这取决于用户是否选择接听电话,并且可能是applicationWillTerminate:,如果他们选择离开你的申请。您还可以使用未注册为应用程序委托的类中的NSNotificationCenter 观察这些事件,有关详细信息,请参阅UIApplication 类参考的“通知”部分。
在第二种情况下,我不知道官方 SDK 是否可以在电话结束时启动您的应用程序。您能否提供执行此操作的应用程序列表?
编辑:
我想我现在明白你的意思了。你应该听从@jessecurry 的建议,UIApplication 上的openURL 使用tel: 协议将拨打电话。至于他们声称“做不可能的事情”并且在拨打电话时不退出应用程序,我不确定他们是如何做到的,因为我没有写它。他们可能正在使用 Skype 等外部 VOIP 服务,或者只是将tel: URL 加载到不可见的网页中。这两个我都不能评论,因为我还没有尝试过。
【讨论】:
openURL,用户现在会收到提示。在 2.x 中它只会拨号
如果您想在应用内拨打电话,可以使用 tel: 网址。
这是一个将电话号码作为字符串并发起呼叫的方法。
- (void)dialNumber: (NSString*)telNumber
{
// fix telNumber NSString
NSArray* telComponents = [telNumber componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
telNumber = [telComponents componentsJoinedByString: @""];
NSString* urlString = [NSString stringWithFormat: @"tel:%@", telNumber];
NSURL* telURL = [NSURL URLWithString: urlString];
//NSLog( @"Attempting to dial %@ with urlString: %@ and URL: %@", telNumber, urlString, telURL );
if ( [[UIApplication sharedApplication] canOpenURL: telURL] )
{
[[UIApplication sharedApplication] openURL: telURL];
}
else
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: NSLocalizedString( @"Dialer Error", @"" )
message: [NSString stringWithFormat: NSLocalizedString( @"There was a problem dialing %@.", @"" ), telNumber]
delegate: nil
cancelButtonTitle: NSLocalizedString( @"OK", @"" )
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
【讨论】: