How to create a VPN connection in Swift on OS X? - macos

I followed the steps described in this tutorial.
I tried to translate the code from Objective-C to Swift. This is the code I have:
var l2tpInterface = SCNetworkInterfaceCreateWithInterface(
kSCNetworkInterfaceIPv4,
kSCNetworkInterfaceTypeL2TP
).takeUnretainedValue();
var pppInterface = SCNetworkInterfaceCreateWithInterface(
l2tpInterface,
kSCNetworkInterfaceTypePPP
).takeUnretainedValue();
var prefs = SCPreferencesCreate(nil,"SoybeanVPN",nil).takeUnretainedValue();
var service = SCNetworkServiceCreate(prefs, pppInterface).takeUnretainedValue();
var success = SCNetworkServiceEstablishDefaultConfiguration(service);
This code returns a runtime error EXEC_BAD_ACCESS(code=1) at the first line.
I googled this error and someone said the problem is caused by using released object in Objective-C? Is that the same in Swift? Does anyone have any experience with VPN creation in Swift?
UPDATE:
I tried to debug and find the const value of kSCNetworkInterfaceIPv4 is invalid, see the attached image:
Is that a bug of Swift?

I believe kSCNetworkInterfaceIPv4 is not the problem, because I can use it just fine to create L2TP/IPSec and Cisco VPNs using Swift.
It's rather tricky to handle C pointers in Swift, but this is how I do it, using Swift 2.3. Notice the wrapping/unwrapping with ! of C-Reference objects.
let initialTopInterface: SCNetworkInterfaceRef!
let initialBottomInterface: SCNetworkInterfaceRef!
// L2TP on top of IPv4
initialBottomInterface = SCNetworkInterfaceCreateWithInterface(kSCNetworkInterfaceIPv4, kSCNetworkInterfaceTypeL2TP)
// PPP on top of L2TP
initialTopInterface = SCNetworkInterfaceCreateWithInterface(initialBottomInterface!, kSCNetworkInterfaceTypePPP)
let service = SCNetworkServiceCreate(usingPreferencesRef, initialTopInterface!)
// Now you assign the attributes
SCNetworkServiceSetName(service, ("Some Name" as CFString))
// myConfiguration is a Hash with your relevant Key/Value pairs
SCNetworkInterfaceSetConfiguration(topInterface!, myConfiguration)
// Here is a good example for why Swift may just crash if you're not careful
let temporaryString:CFString = "IPSec"
SCNetworkInterfaceSetExtendedConfiguration(topInterface!, temporaryString, myL2tpConfiguration)
SCNetworkServiceEstablishDefaultConfiguration(service)
You can find a working implementation (for macOS Sierra) on Github

Just need copy all SCNetworkInterface's
SCNetworkInterfaceCopyAll()
Insert it at the beginning, before creating interfaces

Related

iOS what is the value of accessibilitySpeechIPANotation

Based on this WWDC2018 video, we can dynamically change how words are pronounced in AVSpeechSynthesizer.
I'm trying to do that in my Xamarin App, but I can't find the accessibilitySpeechIPANotation constant.
In the doc of NSMutableAttributedString.AddAttribute(...) it says that the constants can be found here: UIKit.UIStringAttributeKey. Yet the one I'm looking for isn't there.
Does anyone know what the actual string value of the constant is? Or even better, where I can find in in Xamarin.iOS?
(My app uses xamarin.ios v4.0.30319, if the constant is in a newer version of the framework, I'll update it, but google doesn't seem to give me any result when I search for it.)
Cause:
Xamarin for iOS is based on Objective-C.And there are some differences between swift and Objective-C.
Solution:
The code is just like the following :
var attriStrng = new NSMutableAttributedString(new NSString("hello iPhone"));
// you can set the voice here ,a͡͡a͡͡a͡͡a͡͡a͡͡ is just for testing
attriStrng.AddAttribute(new NSString("AVSpeechSynthesisIPANotationAttribute"), new NSString("ˈa͡͡a͡͡a͡͡a͡͡a͡͡a͡͡a͡͡.ˈfo͡ʊn"),new NSRange(6,6));
var voice = new AVSpeechUtterance(attriStrng);
AVSpeechSynthesizer synthesizer = new AVSpeechSynthesizer();
synthesizer.SpeakUtterance(voice);

How to get isHealthDataAvailable from angular2 nativescript using api

Since that is no nativescript support with ios HealthKit..I am trying to work with the api.
To start I am trying to get a simple bool for isHealthDataAvailable():
How?
declare var NSBundle: any;
constructor() {
if (Platform.isIOS){
let healthStore = NSBundle.mainBundle.HKHealthStore();
let is_avail = healthStore.isHealthDataAvailable();
}
}
(in promise): TypeError: NSBundle.mainBundle.HKHealthStore is not a function. (In 'NSBundle.mainBundle.HKHealthStore()', 'NSBundle.mainBundle.HKHealthStore' is undefined)
This is a quote from NativeScript:
https://www.nativescript.org/blog/how-to-enable-healthkit-in-your-nativescript-application
var healthStore = HKHealthStore.new();
And this is how to use HealthKit API in NativeScript. Yes, it is THAT simple.
I beg differ...
That's not where HKHealthStore lives, please see my answer to your other question where I suggest you install the platform declarations (npm i tns-platform-declarations --save-dev). It will help you tremendously: nativescript, angular2 and heaththkit - HKHealthStore not found;
To answer this question: it should be HKHealthStore.isHealthDataAvailable() as can be seen in the screenshot below (which is a snippet of those aforementioned TypeScript declarations for iOS:
You will need to use the proper syntax for data conversion (between the Objective-C Apple API and JavaScript) as described here
e.g.
var healthStore = HKHealthStore.new();
Great detailed step by step explanation can be found in this blog post

iOS 8 Photos framework: Get file name from PHAsset

Is there any way to get the file name from PHAsset object in iOS 8, same as like ALAsset's defaultRepresentation provides the file name?
// For ALAsset
let defaultRepresentation = self.asset.defaultRepresentation()
let filename = defaultRepresentation.filename()
NOTE: This can be done in iOS9 using PHAssetResource class. But I am looking for iOS8 and documented way of doing this(No undocumented APIs Please).
I did find the same question on SO but did not find the convincing answers, so I thought of asking it separately.
Any pointers would be greatly appreciated.
There have a private API called PHAsset.filename.
let filename = phAsset.value(forKey: "filename") as! String
You will get something like "IMG_193.PNG".
There is no documented way to do this on iOS 8.
The only thing you "might" use is the PHImageFileURLKey returned in the info Dictionary of the method requestImageDataForAsset: of PHImageManager. But even this is not fully documented.

Swift 2, warning: could not load any Objective-C class information from the dyld shared cache

I have found a few questions regarding this issue, yet none of them were helping with my problem. I am trying to save an object to core data using this code (which worked perfectly fine in Xcode 6 and Simulator...):
let fetchRequest = NSFetchRequest(entityName: "Patient")
let fetchedResults : [NSManagedObject]!
do {
fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
patienten = fetchedResults
} catch {
print("error")
}
I added the do-try-catch once I started working on this project in the Xcode 7 beta and a physical device.
Now, when I hit the Save button, this piece of code is called, the app freezes and I get the following:
warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.
Does anybody know where I went wrong?
For anyone coming across this in the future, I just ran into this problem myself and it turned out that I was actually getting a stack overflow from a recursive function.
Apparently calling setValue:forKey: on an NSObject calls the respective set[Key] function, where [Key] is the (capitalized) name you gave to the forKey section.
So, if like me, you have code that looks like the following, it will cause an infinite loop and crash.
func setName(name: String) {
self.setValue(name, forKey: "name")
}
Choose Product > Clean
I had similar issue. I deleted the app from the device. Then "Product->Clean" in the XCode menu. When I ran the app again, the issue got resolved.
Swift 3:
Actually this problem happened often when you have any property in input declared as type NSError and the compiler expect an Error output, so change the input type to Error usually solve this issue.
What helped me in similar problem (xCode 7, Swift 2):
reading this question
Or more quickly without explaining the reason of solution: just comment #objc(className) in your NSManagedObjectSubclass , that was generated from your CoreData Entity (#objc(Patient) - in your case ).
This solution (if issue still appears) does not applicable to xCode 7.1/Swift 2.1, as the way of generating NSManagedObjectSubclasses was changed.
Don't forget about cleaning your project (Product > Clean) and deleting the app from your device/simulator to replace CoreData storage on it.
let fetchRequest = NSFetchRequest(entityName: "Patient")
do {
let fetchedResults = try managedObjectContext!.executeFetchRequest(fetchRequest)
print("\(fetchedResults)")
} catch {
print("error")
}
The above code worked for me.
Maybe the issue maybe with how your core data is managed.
Check if your managedObjectContext is actually getting created.
Check the modelling of your core data

Is something wrong with Xcode? I am constantly missing important parameters

Recently I have been experimenting with Coredata by making a couple of applications locally. I noticed whenever I make an object and save things onto core data, I do not seem to be able to view the complete parameters provided by the framework when attempting to create an object. I don't know what the terminalogy is for this step, but I was attempting this:
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
let entityDescription = NSEntityDescription.entityForName("Feeditem", inManagedObjectContext: managedObjectContext!)
let feedItem = FeedItem( <--- PROBLEM RIGHT HERE
Instead of getting this to pop up:
(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
I get this:
Why is this so?
Here is a project in which the same thing was happening: https://github.com/Somnibyte/Errands
Works perfectly for me on the project you uploaded:

Resources