ios 是否可以给予一个电话呼叫操作给一个UILabel?

5m1hhzi4  于 11个月前  发布在  iOS
关注(0)|答案(3)|浏览(85)

我从Parse.com获取一个字符串到我的应用程序中的一个UILabel中。字符串是一个电话号码。所以我的问题是,是否有可能在点击时给UILabel给予一个动作来打电话。。或者我必须从一个按钮获取数据,或者更好的是,这可能吗?
如果可能的话,我应该怎么做?有没有人有一个例子或教程,我可以遵循?任何帮助将不胜感激。谢谢!

gmol1639

gmol16391#

你的问题分为两部分:
1.在录制UILabel时执行任何类型的操作
1.执行电话呼叫操作(第一部分中使用的操作)
首先,要在标签粘贴时执行操作,您必须向此标签添加一个点击手势识别器:

phoneNumberLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = 
   [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneNumberLabelTap)];
[phoneNumberLabel addGestureRecognizer:tapGesture];

字符串
然后你必须实现你的phoneNumberLabelTap方法:

-(void)phoneNumberLabelTap
{
    NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",phoneNumberLabel.text]];

    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
        [[UIApplication sharedApplication] openURL:phoneUrl];
    } else {
        UIAlertView * calert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Call facility is not available!!!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [calert show];
    }
}

balp4ylt

balp4ylt2#

在Swift 3.0中:

mobileLabel.isUserInteractionEnabled = true;
let tap = UITapGestureRecognizer(target: self, action:#selector(self.phoneNumberLabelTap))
mobileLabel.addGestureRecognizer(tap)

func phoneNumberLabelTap() {        
    let phoneUrl = URL(string: "telprompt:\(mobileLabel.text ?? "")")!

    if(UIApplication.shared.canOpenURL(phoneUrl)) {
        UIApplication.shared.openURL(phoneUrl)
    } else {
        Constants.ShowAlertView(title: "Alert", message: "Cannot place call", viewController: self, isFailed: false)
    }
}

字符串

qq24tv8q

qq24tv8q3#

SWIFT 3+
标签添加点击手势识别器

let tap = UITapGestureRecognizer(target: self, action:#selector(self.phoneNumberLabelTap))
    supportCallLabel.isUserInteractionEnabled = true
    supportCallLabel.addGestureRecognizer(tap)
}

字符串

phoneNumberLabelTap方法

  • 这里,常量.supportCall是你的号码 *
@objc func phoneNumberLabelTap() {
    if let url = URL(string: "tel://\(Constants.supportCall)"), UIApplication.shared.canOpenURL(url) {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}

相关问题