How to get isHealthDataAvailable from angular2 nativescript using api - nativescript

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

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 signal strength of sim network using Nativescript?

import * as application from 'application';
const TnsOneSignal = require('nativescript-onesignal').TnsOneSignal
if (application.android) {
application.on(application.launchEvent, function(args: application.ApplicationEventData) {
try {
console.log('TnsOneSignal', TnsOneSignal)
TnsOneSignal.startInit(application.android.context).init()
} catch (error) {
console.log('error', error)
}
})
}
I tried using above code and plugin but it does not work..
Can anyone help please with my code or provide better solution!
TO get the signal strength of the current mobile phone you could use the native APIs on Android and iOS and convert the code to JavaScript based on the NativeScript's marshaling rules.
For example, let's take this native solution. Here is how it would look like in NativeScript
let telephonyManager = application.android.context.getSystemService(android.content.Context.TELEPHONY_SERVICE);
let cellinfogsm = telephonyManager.getAllCellInfo().get(0);
let cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
Of course, in Android to be able to get this information you would need an explicit permission for ACCESS_COARSE_LOCATION so you could use nativescript-permissions plugin to grant them.
Here you can find a test project demonstrating the above technique. based on the same principle you could find an iOS example and convert the code to JS using the marshaling from Objective-C to JS.

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

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

Swift Error : Use of module 'CMSampleBuffer' as a type

I saw below question to use 'captureStillImageAsynchronouslyFromConnection' function in swift:
How to convert code AVFoundation objective c to Swift?
When I try to use a AVFoundation function as following:
var stillImageOutput: AVCaptureStillImageOutput!
//...Initialize stillImageOutput
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(imageSampleBuffer, error) in
if imageSampleBuffer {
var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer as CMSampleBuffer) // I get error on this line saying: 'Use of module ' CMSampleBuffer' as a type'
self.processImage(UIImage(data: imageData))
}
})
I got the error on XCode, which says 'Use of module ' CMSampleBuffer' as a type'.
I tried to rewrite that same line multiple times with slightly different ways, but I could not figure out the solution.
Am I doing something wrong or is it possibly the shaky XCode Beta not working properly?
Thank you in advance.
#Jack and #Christian's solutions worked for me.
I was not careful enough to import CoreMedia.
I never used this framework before, and did not know if the problem was due to not having imported a correct framework.
Thank you for your help!

Cache won't work in Appcelerator

Titanium SDK version: 1.6.
iPhone SDK version: 4.2
I am trying out the cache snippet found on the Appcelerator forum but I get an error: [ERROR] Script Error = Can't find variable: utils at cache.js (line 9).
I put this one (http://pastie.org/1541768) in a file called cache.js and implemented the code from this one (http://pastie.org/pastes/1541787) in the calling script, but I get the error.
What is wrong? I copied the code exactly.
Your problems is whilst the first pastie defines utils.httpcache. The variable utils is not defined outside of this function closure (because it is not defined anywhere in global namespace). As below shows.
(function() {
utils.httpcache = {
};
})();
To make it all work in this instance add the following code to the top of your cache.js file.
var utils = {};
This declares the utils variable in global namespace. Then when the function closure is executed below it will add utils.httpcache to the utils object.
The problem is actually not specific to Appcelerator and is just a simple JavaScript bug. Checkout Douglas Crockfords book, JavaScript the Good Parts. Reading it will literally make you a more awesome JavaScript developer.
You can't use utils.httpcache.getFromCache(url) until you add this to your code:
var utils = {};
That's because how the author created his function, it's called JavaScript module pattern and it's generally used to structure the code.
I seem to lose this value "value.httpCacheExpire = expireTime;" when the code does the "Titanium.App.Properties.setString(key,JSON.stringify(value));" so when I get it back using the getString method, there's no longer the "value.httpCacheExpire.
Anyone else have this issue? Am I missing something to get this working?

Resources