programing

프로그래밍 방식으로 전화 걸기

cafebook 2023. 4. 28. 21:32
반응형

프로그래밍 방식으로 전화 걸기

아이폰에서 프로그래밍 방식으로 전화를 걸려면 어떻게 해야 합니까?다음 코드를 시도했지만 아무 일도 일어나지 않았습니다.

NSString *phoneNumber = mymobileNO.titleLabel.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

원래 앱으로 돌아가려면 tel:// 대신 telprompt://를 사용할 수 있습니다. tell 프롬프트가 먼저 사용자에게 표시되지만 통화가 완료되면 앱으로 돌아갑니다.

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

아마 내 휴대폰이NO.titleLabel.text 값에 체계가 포함되어 있지 않습니다. //

코드는 다음과 같아야 합니다.

목표 C

NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

스위프트

if let url = URL(string: "tel://\(mymobileNO.titleLabel.text))") {
    UIApplication.shared.open(url)
}

@Cristian Radu와 @CraigMellon의 답변과 @joel.d의 의견을 결합하여 다음을 수행해야 합니다.

NSURL *urlOption1 = [NSURL URLWithString:[@"telprompt://" stringByAppendingString:phone]];
NSURL *urlOption2 = [NSURL URLWithString:[@"tel://" stringByAppendingString:phone]];
NSURL *targetURL = nil;

if ([UIApplication.sharedApplication canOpenURL:urlOption1]) {
    targetURL = urlOption1;
} else if ([UIApplication.sharedApplication canOpenURL:urlOption2]) {
    targetURL = urlOption2;
}

if (targetURL) {
    if (@available(iOS 10.0, *)) {
        [UIApplication.sharedApplication openURL:targetURL options:@{} completionHandler:nil];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [UIApplication.sharedApplication openURL:targetURL];
#pragma clang diagnostic pop
    }
} 

이렇게 하면 먼저 "telprompt://" URL을 사용하려고 하고, 실패하면 "tel://" URL을 사용합니다. 둘 다 실패하면 iPad 또는 iPod Touch에 전화를 걸려고 합니다.

빠른 버전:

let phone = mymobileNO.titleLabel.text
let phoneUrl = URL(string: "telprompt://\(phone)"
let phoneFallbackUrl = URL(string: "tel://\(phone)"
if(phoneUrl != nil && UIApplication.shared.canOpenUrl(phoneUrl!)) {
  UIApplication.shared.open(phoneUrl!, options:[String:Any]()) { (success) in
    if(!success) {
      // Show an error message: Failed opening the url
    }
  }
} else if(phoneFallbackUrl != nil && UIApplication.shared.canOpenUrl(phoneFallbackUrl!)) {
  UIApplication.shared.open(phoneFallbackUrl!, options:[String:Any]()) { (success) in
    if(!success) {
      // Show an error message: Failed opening the url
    }
  }
} else {
    // Show an error message: Your device can not do phone calls.
}

여기서의 답은 완벽하게 작동합니다.크레이그 멜론의 답변을 스위프트로 변환하는 중입니다.만약 누군가가 빠른 답을 찾아 온다면, 이것은 그들에게 도움이 될 것입니다.

 var phoneNumber: String = "telprompt://".stringByAppendingString(titleLabel.text!) // titleLabel.text has the phone number.
        UIApplication.sharedApplication().openURL(NSURL(string:phoneNumber)!)

Xamarin을 사용하여 iOS 응용프로그램을 개발하는 경우 응용프로그램 내에서 전화를 걸 수 있는 C#이 있습니다.

string phoneNumber = "1231231234";
NSUrl url = new NSUrl(string.Format(@"telprompt://{0}", phoneNumber));
UIApplication.SharedApplication.OpenUrl(url);

스위프트 3

let phoneNumber: String = "tel://3124235234"
UIApplication.shared.openURL(URL(string: phoneNumber)!)

Swift 3.0에서는

static func callToNumber(number:String) {

        let phoneFallback = "telprompt://\(number)"
        let fallbackURl = URL(string:phoneFallback)!

        let phone = "tel://\(number)"
        let url = URL(string:phone)!

        let shared = UIApplication.shared

        if(shared.canOpenURL(fallbackURl)){
            shared.openURL(fallbackURl)
        }else if (shared.canOpenURL(url)){
            shared.openURL(url)
        }else{
            print("unable to open url for call")
        }

    }

Java RoboVM과 동등한 기능:

public void dial(String number)
{
  NSURL url = new NSURL("tel://" + number);
  UIApplication.getSharedApplication().openURL(url);
}

위의 Swift 3 옵션을 사용해 보았지만 작동하지 않았습니다.스위프트 3에서 iOS 10+에 대항하려면 다음이 필요하다고 생각합니다.

Swift 3(iOS 10+):

let phoneNumber = mymobileNO.titleLabel.text       
UIApplication.shared.open(URL(string: phoneNumber)!, options: [:], completionHandler: nil)
let phone = "tel://\("1234567890")";
let url:NSURL = NSURL(string:phone)!;
UIApplication.sharedApplication().openURL(url);

'openURL:'은(는) 더 이상 사용되지 않습니다. iOS 10.0에서 처음으로 사용되지 않습니다. openURL:options:completionHandler: 대신에 사용하십시오.

Objective-ciOS 10+에서는 다음을 사용합니다.

NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber] options:@{} completionHandler:nil];

스위프트

if let url = NSURL(string: "tel://\(number)"), 
    UIApplication.sharedApplication().canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

openurl을 사용합니다.

swift 5.1에서 전화를 걸려면 다음 코드를 사용하십시오. (Xcode 11에서 테스트했습니다.

let phone = "1234567890"
if let callUrl = URL(string: "tel://\(phone)"), UIApplication.shared.canOpenURL(callUrl) {
     UIApplication.shared.open(callUrl)
}

편집: Xcode 12.4, swift 5.3의 경우 다음을 사용합니다.

UIApplication.shared.open(NSURL(string: "tel://555-123-1234")! as URL)

UIKit을 가져오는지 확인하십시오. 그렇지 않으면 범위에서 UIA 응용 프로그램을 찾을 수 없다고 표시됩니다.

언급URL : https://stackoverflow.com/questions/4929717/make-a-phone-call-programmatically

반응형