Ios PacketTunnelProvider网络扩展不称为Swift 3
我正在尝试将PacketTunnerProvider网络扩展添加到我的项目中。方法startTunnelWithOptions(options:[String:NSObject]?,completionHandler:[NSError?->Void)从未被调用 但是,我能够使用providerBundleIdentifier的网络扩展包id成功地建立VPN连接 这是我用来建立连接的代码Ios PacketTunnelProvider网络扩展不称为Swift 3,ios,swift,networking,vpn,Ios,Swift,Networking,Vpn,我正在尝试将PacketTunnerProvider网络扩展添加到我的项目中。方法startTunnelWithOptions(options:[String:NSObject]?,completionHandler:[NSError?->Void)从未被调用 但是,我能够使用providerBundleIdentifier的网络扩展包id成功地建立VPN连接 这是我用来建立连接的代码 let vpnManager = NETunnelProviderManager.shared() fun
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)")
}
}
}
}
这是我的打包授权文件
`<?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>`
`
com.apple.developer.networking.vpn.api
允许vpn
com.apple.security.application-groups
group.touchcore.Connectionapp
密钥链访问组
$(AppIdentifierPrefix)touchcore.Connectionapp.PacketTunnel
com.apple.developer.networking.networkextension
包隧道提供者
应用程序代理提供商
内容筛选器提供程序
`
方法startTunnelWithOptions(options:[String:NSObject]?,completionHandler:[NSError?->Void)从未被调用
但是,我能够使用providerBundleIdentifier的网络扩展包id成功地建立VPN连接
你到底是什么意思,它从来没有被呼叫过?如果您能够成功建立连接,则会调用startTunnelWithOptions
如果试图通过使用NSLog()
确定调用它,请记住,只有将调试器附加到提供程序而不是容器应用程序时,才会在调试日志中显示
这将很困难,因为提供程序已初始化,并且在您有机会附加调试器之前调用了startTunnelWithOptions
函数。
在这种情况下,一个有用的解决方法是休眠,以便给调试器附加时间
- (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
函数中完全输入您的逻辑
因此,在XCode中的这段时间内,您可以转到Debug->Attach to Process->YourVPNProviderProcess
,等待它完全初始化
嘿,nathan,我有一个网络分机,但它可以从我的主应用程序呼叫。请给我一些建议或解释为什么它不能打电话给我。谢谢。根据这里的回答,你说执行需要10秒,但在我的情况下,从来没有调用过。