Can't setup VPN connection using Network Extension Framework iOS 8 in Swift -
so i've been trying use ios 8 network extension framework setup vpn connection when users presses uibutton. i've used following tutorial: http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/
but reason keeps asking vpn password , shared secret. though set passwordreference , sharedsecretreference. , if enter these details when installing profile still not work. doesn't when starting connection using framework. when trying connect using settings app gives "there's no sharedsecret" error.
this code use set connection.
func toggleconnection(sender: uibutton) { if(!self.connected){ self.manager.loadfrompreferenceswithcompletionhandler { (error) -> void in if((error) != nil) { println("vpn preferences error: 1") } else { var p = nevpnprotocolipsec() p.username = "$username" p.serveraddress = "$vpn" p.passwordreference = keychainservice.dataforkey("vpnpassword")! println(p.passwordreference) p.authenticationmethod = nevpnikeauthenticationmethod.sharedsecret p.sharedsecretreference = keychainservice.dataforkey("sharedsecret")! println(p.sharedsecretreference) p.localidentifier = "vpn" p.remoteidentifier = "vpn" p.disconnectonsleep = false self.manager.`protocol` = p self.manager.ondemandenabled = true self.manager.localizeddescription = "vpn" self.manager.savetopreferenceswithcompletionhandler({ (error) -> void in if((error) != nil) { println("vpn preferences error: 2") println(error) } else { var starterror: nserror? self.manager.connection.startvpntunnelandreturnerror(&starterror) if((starterror) != nil) { println("vpn preferences error: 3") println(starterror) } else { println("start vpn") } } }) } } } }
these functions use keychain reference.
class func save(service: nsstring, key: string, data: nsstring) { var datafromstring: nsdata = data.datausingencoding(nsutf8stringencoding, allowlossyconversion: false)! var keychainquery: nsmutabledictionary = nsmutabledictionary(objects: [ksecclassgenericpassword, service, key, datafromstring], forkeys: [ksecclass, ksecattrservice, ksecattraccount, ksecvaluedata]) secitemdelete(keychainquery cfdictionaryref) if data == "" { return } var status: osstatus = secitemadd(keychainquery cfdictionaryref, nil) println(status) } class func load(service: nsstring, key: string) -> nsdata? { var keychainquery: nsmutabledictionary = nsmutabledictionary(objects: [ksecclassgenericpassword, service, key, kcfbooleantrue, ksecmatchlimitone, kcfbooleantrue], forkeys: [ksecclass, ksecattrservice, ksecattraccount, ksecreturndata, ksecmatchlimit, ksecreturnpersistentref]) var datatyperef :unmanaged<anyobject>? let status: osstatus = secitemcopymatching(keychainquery, &datatyperef) println(status) if (status != errsecsuccess) { return nil } let opaque = datatyperef?.toopaque() var contentsofkeychain: nsdata? = nil if let op = opaque { let retrieveddata = unmanaged<nsdata>.fromopaque(op).takeunretainedvalue() contentsofkeychain = retrieveddata } println(contentsofkeychain) return contentsofkeychain }
any appreciated!
so had replace swift library used access keychain following obj-c methods. solved problem far i'm able tell.
+ (void) storedata: (nsstring * )key data:(nsdata *)data { nslog(@"store data"); nsmutabledictionary * dict = [[nsmutabledictionary alloc] init]; [dict setobject:(__bridge id)ksecclassgenericpassword forkey:(__bridge id)ksecclass]; nsdata *encodedkey = [key datausingencoding:nsutf8stringencoding]; [dict setobject:encodedkey forkey:(__bridge id)ksecattrgeneric]; [dict setobject:encodedkey forkey:(__bridge id)ksecattraccount]; [dict setobject:@"vpn" forkey:(__bridge id)ksecattrservice]; [dict setobject:(__bridge id)ksecattraccessiblealwaysthisdeviceonly forkey:(__bridge id)ksecattraccessible]; [dict setobject:data forkey:(__bridge id)ksecvaluedata]; osstatus status = secitemadd((__bridge cfdictionaryref)dict, null); if(errsecsuccess != status) { nslog(@"unable add item key =%@ error:%d",key,(int)status); } } + (nsdata *) getdata: (nsstring *)key { nsmutabledictionary * dict = [[nsmutabledictionary alloc] init]; [dict setobject:(__bridge id)ksecclassgenericpassword forkey:(__bridge id)ksecclass]; nsdata *encodedkey = [key datausingencoding:nsutf8stringencoding]; [dict setobject:encodedkey forkey:(__bridge id)ksecattrgeneric]; [dict setobject:encodedkey forkey:(__bridge id)ksecattraccount]; [dict setobject:@"vpn" forkey:(__bridge id)ksecattrservice]; [dict setobject:(__bridge id)ksecattraccessiblealwaysthisdeviceonly forkey:(__bridge id)ksecattraccessible]; [dict setobject:(__bridge id)ksecmatchlimitone forkey:(__bridge id)ksecmatchlimit]; [dict setobject:(id)kcfbooleantrue forkey:(__bridge id)ksecreturnpersistentref]; cftyperef result = null; osstatus status = secitemcopymatching((__bridge cfdictionaryref)dict,&result); if( status != errsecsuccess) { nslog(@"unable fetch item key %@ error:%d",key,(int)status); return nil; } nsdata *resultdata = (__bridge nsdata *)result; return resultdata; }
Comments
Post a Comment