问题描述
我正在尝试向我的项目添加 PacketTunnerProvider 网络扩展.方法 startTunnelWithOptions(options: [String : NSObject]?, completionHandler: (NSError?) -> Void) 永远不会被调用
I am trying to add a PacketTunnerProvider network extension to my project. The method startTunnelWithOptions(options: [String : NSObject]?, completionHandler: (NSError?) -> Void) Never gets called
但是,我能够使用 providerBundleIdentifier 的网络扩展包 ID 成功建立 VPN 连接
However, I am able to succesfully establish a VPN connection using the network extensions bundle id for the providerBundleIdentifier
这是我用来建立连接的代码
This is my code used to establish a connection
let vpnManager = NETunnelProviderManager.shared()
func initVPNTunnelProviderManager() {
let config = NETunnelProviderProtocol()
config.providerBundleIdentifier = self.tunnelBundleId
config.providerConfiguration = ["lol": 1]
config.serverAddress = self.serverAddress
config.username = self.username
config.passwordReference = passwordRef
vpnManager.loadFromPreferences {
(error: Error?) in
self.vpnManager.protocolConfiguration = vpnProtocol
self.vpnManager.localizedDescription = "Connect_1.0.0"
self.vpnManager.isEnabled = true
self.vpnManager.saveToPreferences {
(error: Error?) in
do {
try self.vpnManager.connection.startVPNTunnel()
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
}
}
这是我的 PacketTunnel 权限文件
This is my PacketTunnel entitlements file
`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.networking.vpn.api</key>
<array>
<string>allow-vpn</string>
</array>
<key>com.apple.security.application-groups</key>
<array>
<string>group.touchcore.Connectionapp</string>
</array>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)touchcore.Connectionapp.PacketTunnel</string>
</array>
<key>com.apple.developer.networking.networkextension</key>
<array>
<string>packet-tunnel-provider</string>
<string>app-proxy-provider</string>
<string>content-filter-provider</string>
</array>
</dict>
</plist>`
推荐答案
但是,我能够使用 providerBundleIdentifier 的网络扩展包 ID 成功建立 VPN 连接
However, I am able to succesfully establish a VPN connection using the network extensions bundle id for the providerBundleIdentifier
你的意思是它永远不会被调用?如果您能够成功建立连接,则正在调用 startTunnelWithOptions
.
What exactly do you mean it never gets called? If you're able to successfully establish a connection then startTunnelWithOptions
is being called.
如果您试图确定它是通过使用 NSLog()
调用的,请记住,如果您将调试器附加到提供程序,则只会在调试日志中显示容器应用程序.
If you're trying to determine that it' being called by using an NSLog()
, keep in mind that that will only show in the debug log if you attatch the debugger to the provider instead of your container application.
这会很困难,因为提供程序已初始化,并且 startTunnelWithOptions
函数在您有机会附加调试器之前被调用.
This will be difficult as the provider is initialized and the startTunnelWithOptions
function is called before you get a chance to attach the debugger.
在这种情况下,一个有用的解决方法是休眠,以便让调试器有时间进行连接.
A useful work around in this situation is to sleep in order to give the debugger time to attach.
- (void) startTunnelWithOptions:(NSDictionary *) options
completionHandler:(void (^)(NSError *)) completionHandler
{
// Give debugger time to attach, 10 seconds is usually enough
// Comment this out before you release the app or else you
// will be stuck with a 10 second delay on all connections.
sleep(10);
// Continue with execution
. . .
}
然后,当您初始化 PacketTunnelProvider 时,它将等待 10 秒钟,然后才能完全进入 startTunnelWithOptions
函数内部的逻辑.
Then, when you initialize your PacketTunnelProvider it will wait 10 seconds before fully entering your logic inside of the startTunnelWithOptions
function.
因此,在此期间,您可以在 XCode 中转到 Debug->Attach To Process->YourVPNProviderProcess
并等待它完全初始化.
So, during this time in XCode you can go to Debug->Attach To Process->YourVPNProviderProcess
and wait for it to fully initialize.
这篇关于PacketTunnelProvider 网络扩展未称为 Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!