0

我想通过每天拨打八次电话来阻止来自特定国家的电话,这些电话让我感到不安。我想创建一个允许使用特定扩展名(例如 +33 France、+212 Maroc、+973 Bahrain 等)阻止任何号码的应用程序。

我添加了 CallKit,我按照教程没有成功。我试过这个,但似乎我什至无法阻止一个简单的号码......

private func addAllBlockingPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
        print("1234")
        let phoneNumbers: [CXCallDirectoryPhoneNumber] = [ 1234 ]
        for phoneNumber in phoneNumbers {
          context.addBlockingEntry(withNextSequentialPhoneNumber: phoneNumber)
        }
      }

      private func addOrRemoveIncrementalBlockingPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
        print("1234")
        // Retrieve any changes to the set of phone numbers to block from data store. For optimal performance and memory usage when there are many phone numbers,
        // consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
        let phoneNumbersToAdd: [CXCallDirectoryPhoneNumber] = [ 1234 ]
        for phoneNumber in phoneNumbersToAdd {
          context.addBlockingEntry(withNextSequentialPhoneNumber: phoneNumber)
        }

        let phoneNumbersToRemove: [CXCallDirectoryPhoneNumber] = [ 1234 ]
        for phoneNumber in phoneNumbersToRemove {
          context.removeBlockingEntry(withPhoneNumber: phoneNumber)
        }

        // Record the most-recently loaded set of blocking entries in data store for the next incremental load...
      }

有任何想法吗 ?

4

2 回答 2

4

使用 CallKit,您可以创建一个应用扩展,称为 Call Directory 应用扩展。Apple 首先检查来电号码是否在系统或用户的阻止列表中被阻止,然后检查您的应用目录的阻止列表。

在此页面的底部,对其进行了深入解释: https ://developer.apple.com/documentation/callkit?language=objc

阻止来电

当电话收到来电时,系统首先查询用户的阻止列表来确定是否应该阻止呼叫。如果电话号码不在用户或系统定义的阻止列表中,则系统会查询您应用的呼叫目录分机以查找匹配的阻止号码。这对于应用程序很有用,例如,维护已知律师的数据库,或允许用户阻止任何符合一组标准的号码。

要阻止特定电话号码的来电,您可以在 beginRequestWithExtensionContext: 的实现中使用 addBlockingEntryWithNextSequentialPhoneNumber: 方法。

笔记

您可以指定您的呼叫目录应用程序扩展在 beginRequestWithExtensionContext: 的实现中添加标识和/或阻止电话号码。

@interface CustomCallDirectoryProvider: CXCallDirectoryProvider
@end

@implementation CustomCallDirectoryProvider
- (void)beginRequestWithExtensionContext:(NSExtensionContext *)context {
    NSArray<NSNumber *> *blockedPhoneNumbers.sorted = @[ … ];
     for (NSNumber *phoneNumber in [blockedPhoneNumbers.sorted sortedArrayUsingSelector:@selector(compare:)]) {
        [context addBlockingEntryWithNextSequentialPhoneNumber:(CXCallDirectoryPhoneNumber)[phoneNumber unsignedLongLongValue]];
     }

    [context completeRequestWithCompletionHandler:nil];
}
@end
于 2020-05-11T13:59:24.283 回答
0

您必须在 addBlockingPhoneNumbers 和 addIdentificationPhoneNumbers 中提供国家代码和联系人,它会阻止来电:

class CallDirectoryHandler: CXCallDirectoryProvider {
    
    var callDirectory: CXCallDirectoryExtensionContext?
    
    override func beginRequest(with context: CXCallDirectoryExtensionContext) {
        context.delegate = self
        self.callDirectory = context
        
        do {
            try removeFromBlock(to: context)
        } catch {
            NSLog("Unable to remove blocking phone numbers")
            let error = NSError(domain: "CallDirectoryHandler", code: 1, userInfo: nil)
            context.cancelRequest(withError: error)
            return
        }
    
        do {
            try addBlockingPhoneNumbers(to: context)
        } catch {
            NSLog("Unable to add blocking phone numbers")
            let error = NSError(domain: "CallDirectoryHandler", code: 1, userInfo: nil)
            context.cancelRequest(withError: error)
            return
        }
        
        do {
            try addIdentificationPhoneNumbers(to: context)
        } catch {
            NSLog("Unable to add identification phone numbers")
            let error = NSError(domain: "CallDirectoryHandler", code: 2, userInfo: nil)
            context.cancelRequest(withError: error)
            return
        }
        context.completeRequest()
    }
    
    // 1.
    private func addBlockingPhoneNumbers(to context: CXCallDirectoryExtensionContext) throws {
        let defaults =  UserDefaults(suiteName:"group._.in._._")
        if let countryCode =  defaults?.object(forKey:"countrycode") as? String {
            let num      =  Int64("\(countryCode)"+"\(defaults?.object(forKey:"Contact") as! String)")
            let blockedPhoneNumbers: [CXCallDirectoryPhoneNumber] = [num ?? 0000000]
            for phoneNumber in blockedPhoneNumbers.sorted(by: <) {
                context.addBlockingEntry(withNextSequentialPhoneNumber: phoneNumber)
            }
        }
    }
    
    // 2.
    private func addIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) throws {
        let defaults    =  UserDefaults(suiteName:"group._.in._._")
        if let countryCode =   defaults?.object(forKey:"countrycode") as? String {
            let num         =  Int64("+\(countryCode)"+"\(defaults?.object(forKey:"Contact") as! String)")
            let phoneNumbers: [CXCallDirectoryPhoneNumber] = [num ?? 0000000]
            let labels = ["_ Team"]
            
            for (phoneNumber, label) in zip(phoneNumbers, labels) {
                context.addIdentificationEntry(withNextSequentialPhoneNumber: phoneNumber, label: label)
            }
         }
    }
    
    // 1.
    private func removeFromBlock(to context: CXCallDirectoryExtensionContext) throws {
        let defaults    =  UserDefaults(suiteName:"group._.in._._")
        if let countryCode =  defaults?.object(forKey:"removeCountrycode") as? String {
            let num         =  Int64("\(countryCode)"+"\(defaults?.object(forKey:"ContactRemove") as! String)")
            let blockedPhoneNumber:CXCallDirectoryPhoneNumber = num ?? 0000000
            context.removeBlockingEntry(withPhoneNumber:blockedPhoneNumber)
            defaults!.removeObject(forKey:"ContactRemove")
            defaults!.removeObject(forKey:"removeCountrycode")
        }
       
    }
}
于 2020-09-06T19:36:24.977 回答