未安装 Sim 卡弹出在呼叫功能中显示两次

No Sim Card Installed Pop up showing twice in calling feature

我正在使用 App 中的通话功能。问题是,如果设备中没有安装 SIM 卡,那么 "No sim card installed" 警报视图显示 2 次。我正在使用此代码:

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:phoneNumber]])
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
    }

注意:第一个警报视图自动隐藏,第二个警报视图再次出现。

import UIKit
import Alamofire
import NVActivityIndicatorView
import Reachability

//pod 'NVActivityIndicatorView'
//pod 'Alamofire', '~> 4.0'
//pod 'ReachabilitySwift'


typealias CompletionHandler = (_ success:Bool,_ reponsedata:Data) -> Void
typealias CompletionHandlerJson = (_ success:Bool,_ reponsedata:NSMutableDictionary) -> Void
typealias ResponseHandler = (_ success:Bool,_ data:NSData, _ error : String) -> Void
typealias ConnectivityHandler = (_ success:Bool) -> Void

class Connectivity {

    class func internetConnection(completionHandler: @escaping ConnectivityHandler) {

        //var Status:Bool = false

        let url = NSURL(string: "http://google.com/")
        let request = NSMutableURLRequest(url: url! as URL)
        request.httpMethod = "HEAD"
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData
        request.timeoutInterval = 1.0
        let session = URLSession.shared

        session.dataTask(with: request as URLRequest as URLRequest, completionHandler: {(data, response, error) in

            if let httpResponse = response as? HTTPURLResponse {
                if httpResponse.statusCode == 200 {
                    completionHandler(true)
                }
            }
            completionHandler(false)

        }).resume()

    }

     class func connetivityAvailable() ->Bool {
        return NetworkReachabilityManager()!.isReachable
    }

}

class WebserviceHelper: NSObject {

    class func postWebServiceCall(urlString:String, parameters:[String:AnyObject], encodingType: String, ShowProgress:Bool, completionHandler: @escaping CompletionHandler){

        if Connectivity.connetivityAvailable() == false {
            log(message: "internet is not available.")
            HUD.hide()
            AppDelegate.showMessage(message:NO_INTERNET_AVAILABLE)
            completionHandler(false,Data())
            return
        }

        if ShowProgress {
            HUD.show(.systemActivity)
        }
        log(message: "\(urlString): + parametersnew")
        let encoding: ParameterEncoding!
        if encodingType == DefaultEncoding{
            encoding = JSONEncoding.default
        }else{
            encoding = URLEncoding.httpBody
        }

        Alamofire.request(urlString, method: HTTPMethod.post, parameters: parameters, encoding: encoding , headers: HelperClass.sharedInstance.getApiHeader()).responseData(completionHandler: { (response) in

            if  response.result.isSuccess {
                if ShowProgress {
                    HUD.hide()
                }
                if let result = response.result.value {
                    completionHandler(true,result)
                }
            }

            if response.result.isFailure {
                if ShowProgress{
                    HUD.hide()
                }
                print("Lost Connection %@",urlString)
                if let responseCode = response.response?.statusCode as Int?{
                    print("Lost Connection with code %@",response.result.value as Any)
                    let responseDic = NSMutableDictionary()
                    responseDic.setObject(responseCode, forKey: "statusCode" as NSCopying)
                    completionHandler(false,Data())
                }
                completionHandler(false,Data())
            }
        })

  }
    class func postWebServiceJson(urlString:String, parameters:[String:AnyObject], encodingType: String, ShowProgress:Bool, completionHandler: @escaping CompletionHandlerJson){

        if Connectivity.connetivityAvailable() == false {
            AppDelegate.showMessage(message:NO_INTERNET_AVAILABLE)
            completionHandler(false,NSMutableDictionary())
            return
        }

        if ShowProgress {
            HUD.show(.systemActivity)
        }
        log(message: "\(urlString): + parametersnew")
        let encoding: ParameterEncoding!

        if encodingType == DefaultEncoding{
            encoding = JSONEncoding.default
        }else{
            encoding = URLEncoding.httpBody
        }

        Alamofire.request(urlString, method: HTTPMethod.post, parameters: parameters, encoding: encoding , headers: HelperClass.sharedInstance.getApiHeader()).responseJSON { (response) in

            if  response.result.isSuccess {
                if ShowProgress {
                    HUD.hide()
                }
                if let result = response.result.value as? NSDictionary {
                    completionHandler(true,NSMutableDictionary(dictionary: result))
                }
            }
            if response.result.isFailure {
                if ShowProgress{
                    HUD.hide()
                }
                print("Lost Connection %@",urlString)
                if let responseCode = response.response?.statusCode as Int?{
                    print("Lost Connection with code %@",response.result.value as Any)
                    let responseDic = NSMutableDictionary()
                    responseDic.setObject(responseCode, forKey: "statusCode" as NSCopying)
                    completionHandler(false,NSMutableDictionary())
                }
                completionHandler(false,NSMutableDictionary())
            }
        }

    }




    class func WebapiGetMethod(strurl:String, ShowProgress: Bool = false, completionHandler: @escaping CompletionHandlerJson){

        if Connectivity.connetivityAvailable() == false {
           log(message:"internet is not available.")
           completionHandler(false,NSMutableDictionary())
//..           AppDelegate .showMessage(message:"Please check your internet connection")
           return
        }
        if ShowProgress{
            HUD.show(.systemActivity)
        }
       Alamofire.request(strurl).responseJSON { response in
        if let status = response.response?.statusCode {
        switch(status){
        case 200:
            if let result = response.result.value {
                if let JSONdict = result as? NSMutableDictionary{
                    DispatchQueue.main.async {
                        completionHandler(true,JSONdict)
                    }
                }
                if ShowProgress {
                    HUD.hide()
                }

          }else{
            if ShowProgress {
                 HUD.hide()
            }
            completionHandler(false,NSMutableDictionary())
          }
          log(message:"example success")
        default:
          log(message:"error with get response status: \(status)")
            DispatchQueue.main.async {
                 if ShowProgress {
                  HUD.hide()
             }
          }
        completionHandler(false,NSMutableDictionary())
        }
        }
        DispatchQueue.main.async {
            if ShowProgress {
                HUD.hide()
            }
        }
    }
  }
    class func getWebServiceCall(urlString:String,ShowProgress:Bool, completionHandler: @escaping CompletionHandler){

          if Connectivity.connetivityAvailable() == false {
              log(message: "internet is not available.")
              HUD.hide()
              AppDelegate.showMessage(message:NO_INTERNET_AVAILABLE)
              completionHandler(false,Data())
              return
          }

          if ShowProgress {
              HUD.show(.systemActivity)
          }
          log(message: "\(urlString): + parametersnew")

        Alamofire.request(urlString, method: .get, parameters: nil, encoding: URLEncoding.methodDependent, headers: HelperClass.sharedInstance.getApiHeader()).responseData { (response) in

            if  response.result.isSuccess {
                if ShowProgress {
                    HUD.hide()
                }
                if let result = response.result.value {
                    completionHandler(true,result)
                }
            }

            if response.result.isFailure {
                if ShowProgress{
                    HUD.hide()
                }
                print("Lost Connection %@",urlString)
                if let responseCode = response.response?.statusCode as Int?{
                    print("Lost Connection with code %@",response.result.value as Any)
                    let responseDic = NSMutableDictionary()
                    responseDic.setObject(responseCode, forKey: "statusCode" as NSCopying)
                    completionHandler(false,Data())
                }
                completionHandler(false,Data())
            }

        }


    }






   }

终于找到替代解决方案:

其实这不是问题,这是过渡效果:

为了解决这个问题,我在调用功能之前集成了以下代码:

@import CoreTelephony;


CTTelephonyNetworkInfo *networkInfo = [CTTelephonyNetworkInfo new];
    CTCarrier *carrier = [networkInfo subscriberCellularProvider];
    if (!carrier.isoCountryCode) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No SIM Card Installed" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    else{

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
    }