【问题标题】:Button to make phone call in iOS Xcode在 iOS Xcode 中拨打电话的按钮
【发布时间】:2018-03-13 03:19:35
【问题描述】:

我有以下代码可以在应用内拨打电话。但是,每当我点击按钮时 - 什么都没有发生。有什么想法我可能做错了吗?

- (void)phonePressed:(id)sender
{
    UIButton *btn = (UIButton* )sender;
    NSString *key = [self.dictArray.allKeys objectAtIndex:btn.tag];
    NSMutableArray *arrData = [self.dictArray objectForKey:key];
    NSMutableDictionary *dict = [arrData objectAtIndex:btn.tag];

    UIApplication *application = [UIApplication sharedApplication];
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",[dict objectForKey:@"contact_phone"]]];
    [application openURL:URL options:@{} completionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"Opened url");
        }
    }];
}

【问题讨论】:

  • 没有人知道 dictArray 是什么。
  • 你能给我们看看你的dictArray吗??
  • 如果您以编程方式添加了操作,则显示按钮选择器的代码;如果您使用故事板连接,则共享故事板按钮操作连接快照。
  • 你能显示你要打开的网址吗? . ?

标签: ios objective-c uibutton ibaction


【解决方案1】:

试试这个看看(在你按下电话按钮之前清除你的控制台日志。让我知道你的按钮操作打印了什么)

- (void)phonePressed:(UIButton *)btn {

    NSLog(@"dictArray = %@", dictArray);
    if (self.dictArray == nil || self.dictArray.count == 0) {
      NSLog(@"There is not data/elements in self.dictArray");
    }

    NSDictionary *dict = [self.dictArray objectAtIndex:btn.tag];
    NSLog(@"dict = %@", dict);

    if (dict == nil || dict.count == 0) {
      NSLog(@"There is not data/elements in dict");
    }

    NSString * contact_phone = [dict objectForKey:@"contact_phone"]
    NSLog(@"contact_phone = %@", contact_phone);

    if (contact_phone == nil) {
      NSLog(@"There is not value for contact_phone");
    }

    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat: @"tel://%@", contact_phone]];

    NSLog(@"URL = %@", URL);

    if (URL == nil) {
      NSLog(@"URL is nil");
    }


    UIApplication *application = [UIApplication sharedApplication];

    if ([application canOpenURL: URL]) {
      [application openURL:URL options:@{} completionHandler:^(BOOL success) {
         if (success) {
          NSLog(@"Opened url");
         }
      }];
    } else {
       NSLog(@"Application cannot open URL");
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多