• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS 8 VPN 从代码连接,如果我没有证书

[复制链接]
菜鸟教程小白 发表于 2022-12-13 02:17:00 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

VPN 连接无法通过代码工作。 为什么连不上服务器? 代码是:

- (void)viewDidLoad {
    [super viewDidLoad];
    // init VPN manager
    self.vpnManager = [NEVPNManager sharedManager];

    // load config from perference
    [vpnManager loadFromPreferencesWithCompletionHandler:^(NSError *error) {

        if (error) {
            NSLog(@"Load config failed [%@]", error.localizedDescription);
            return;
        }

        if (vpnManager.protocol) {
            // config exists
        }

        // config IPSec protocol
        NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
        p.username = @"username";
        p.serverAddress = @"serveraddress";

        // get password persistent reference from keychain
        NSString *password = @"thepassword";
        NSData *paswordData = [password dataUsingEncoding:NSUTF8StringEncoding];
        p.passwordReference = paswordData;

        // PSK
        p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
        NSString *secret = @"123654987";
        NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
        p.sharedSecretReference = secretData;


        p.useExtendedAuthentication = NO;
        p.disconnectOnSleep = NO;

        vpnManager.protocol = p;
        vpnManager.localizedDescription = @"[email protected]";

        [vpnManager saveToPreferencesWithCompletionHandler:^(NSError *error) {
            NSLog(@"Save config failed [%@]", error.localizedDescription);
        }];

    }];

   }

static NSString * const serviceName = @"com.progserv.vpn_config";

-(NSData *)searchKeychainCopyMatchingNSString *)identifier {
    NSMutableDictionary *searchDictionary = [[NSMutableDictionary alloc] init];

    NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];

    searchDictionary[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
    searchDictionary[(__bridge id)kSecAttrGeneric] = encodedIdentifier;
    searchDictionary[(__bridge id)kSecAttrAccount] = encodedIdentifier;
    searchDictionary[(__bridge id)kSecAttrService] = serviceName;

    searchDictionary[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitOne;
    searchDictionary[(__bridge id)kSecReturnPersistentRef] = @YES;

    CFTypeRef result = NULL;
    SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, &result);

    return (__bridge_transfer NSData *)result;
}


- (IBAction)CreateVPNid)sender {
    NSError *startError;
    [vpnManager.connection startVPNTunnelAndReturnError:&startError];


    if (startError) {
        NSLog(@"Start VPN failed: [%@]", startError.localizedDescription);
    }
}



Best Answer-推荐答案


如果 loadFromPreferencesWithCompletionHandler 从未被调用,您可能还没有在 Apple 的开发者门户中创建一个特定于应用程序的配置文件(其中启用了 VPN 服务)...我相信您已经看到了文章,但配置文件非常重要:http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/

关于iOS 8 VPN 从代码连接,如果我没有证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120425/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap