iPhone SDK:在通话结束后启动应用程序

时间:2010-02-22 17:16:57

标签: iphone phone-call

如何在应用程序内拨打电话或在通话结束后立即启动应用程序?我知道这是可能的,因为应用程序商店中的某些应用程序已经在这样做了。

4 个答案:

答案 0 :(得分:6)

我从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];
}


}

答案 1 :(得分:3)

我认为这有两个部分

  1. 应用程序已在运行,用户收到提示,指示正在接听电话,并要求接受或拒绝
  2. 用户接到电话,但应用程序未运行
  3. 在第一种情况下,您的UIApplicationDelegate会收到所有邮件application:willChangeStatusBarFrame:application:didChangeStatusBarFrame:applicationWillResignActive:applicationDidBecomeActive:所有可能的邮件,具体取决于是否用户选择是否接听电话,如果他们选择离开您的应用程序,可能会applicationWillTerminate:。您还可以使用未注册为应用程序委托的类中的NSNotificationCenter来观察这些事件,有关详细信息,请参阅UIApplication类参考的“通知”部分。

    在第二种情况下,我不知道在电话结束时,还有正式的SDK来启动您的应用程序。你能提供一份这样做的应用程序列表吗?

    编辑:

    我想我明白你的意思了。您应该遵循@jessecurry的建议,使用openURL协议的UIApplication上的tel:会拨打电话。至于他们声称“做不可能”并且在打电话时没有退出应用程序,我不确定他们是怎么做到的,因为我没有写它。他们可能正在使用像Skype这样的外部VOIP服务,或只是将tel: URL加载到不可见的网页中。我没有评论,因为我没有尝试过。

答案 2 :(得分:1)

使用telprompt而不是tel来完成。请查看以下代码

[[UIApplication sharedApplication]      openURL:[NSURL URLWithString:@“telprompt:18004912200”]];

答案 3 :(得分:0)

如果您想在自己的应用中拨打电话,可以使用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];
    }
}