How to check the “Allow Full Access” is enabled in iOS 10? - ios

How to check the “Allow Full Access” is enabled in iOS 10?
This method is not working in iOS 10
-(BOOL)isOpenAccessGranted{
return [UIPasteboard generalPasteboard];
}

Here's an objective c version
-(BOOL) checkIfFullAccessEnabled {
NSOperatingSystemVersion osv = [NSProcessInfo processInfo].operatingSystemVersion;
if(osv.majorVersion >= 10) {
UIPasteboard *pb = [UIPasteboard generalPasteboard];
if(pb.hasStrings || pb.hasURLs || pb.hasColors || pb.hasImages) {
return YES;
}
else {
NSString *ustr = pb.string;
pb.string = #"777-TEST-123";
if(pb.hasStrings) {
pb.string = ustr;
return YES;
}
else {
return NO;
}
}
}
else if([UIPasteboard generalPasteboard] != nil) {
return YES;
}
else {
return NO;
}
}

User #user979686 posted a solution in the following thread but I have not tested this.
https://stackoverflow.com/a/38903406/4792451
Posting the code here for clarity.
let originalString = UIPasteboard.general.string
UIPasteboard.general.string = "TEST"
if UIPasteboard.general.hasStrings
{
UIPasteboard.general.string = originalString
hasFullAccess = true
}
else
{
hasFullAccess = false
}

I've been testing this today and it appears that you don't have to set something on the pasteboard to verify that you have access. All you need to do is test to see if something is on the pasteboard. Of course if the pasteboard happens to be completely empty then it's not an exhaustive test so in that case you do need to set something, but then you've already ruled out anything being replaced. Here's what I came up with
func checkFullAccess() -> Bool
{
if #available(iOSApplicationExtension 10.0, *) {
let pasty = UIPasteboard.general
if pasty.hasURLs || pasty.hasColors || pasty.hasStrings || pasty.hasImages {
return true
} else {
pasty.string = "TEST"
if pasty.hasStrings {
pasty.string = ""
return true
}
}
} else {
// Fallback on earlier versions
var clippy : UIPasteboard?
clippy = UIPasteboard.general
return clippy != nil
}
return false
}
In my tests I found that all the checks (hasString, hasImages, etc) would return false even with content on the pasteboard with full access turned off. If they all return false then it could be truly empty so you try setting something in that case. If it wasn't empty then you don't have access and you won't be replacing anything, if it was empty and you do have access then the text will be placed there and you can return it to it's previous state.
HTH,
Mike

Related

Handle properly number in Jetpack Compose

How can I properly handle number in text component in jetpack compose (with MVVM pattern)
Please note that the price can be null or have a value (maybe 0 btw)
I have a poor implementation for now, I changed the keyboard like this :
OutlinedTextField(
value = if (vm.viewState.collectAsState().value.price != null) vm.viewState.collectAsState().value.price.toString() else "",
onValueChange = { vm.onProductPriceChange(it) },
label = { Text(stringResource(id = R.string.price)) },
keyboardOptions = KeyboardOptions(
capitalization = KeyboardCapitalization.None,
autoCorrect = true,
keyboardType = KeyboardType.Number
),
)
and for onValueChange :
fun onProductPriceChange(it: Any) {
if (it.toString() == "") {
_viewState.value = _viewState.value.copy(price = null)
} else {
try
{
_viewState.value = _viewState.value.copy(price = it.toString().toDouble())
}
catch (e: NumberFormatException)
{ // dismiss the bad entries
}
}
}
there can be multiple bad output of the user for example write 22..0 (I dismissed them which is a workaround acceptable)
but there are bad behaviour, when you want to write 10, it will convert it to 10.0. it is not huge but it has backwards
when you delete number in the EditText, 10.0 will become 10.0 and then 100.0 and then 10.0 and finally 1.0. btw it is impossible to go back to the null value (for this case, I can consider 0.0 = no value)
I saw that VisualTransformation (https://medium.com/google-developer-experts/hands-on-jetpack-compose-visualtransformation-to-create-a-phone-number-formatter-99b0347fc4f6) could handle my case but the documentation seems complicated
class DoubleVisualTransformation : VisualTransformation {
override fun filter(str: AnnotatedString): TransformedText {
val strNullDouble = str.text.toBigDecimalOrNull()
var transformedString: String
if (str.text == "" || strNullDouble == null)
return TransformedText(AnnotatedString(""), OffsetMapping.Identity)
else if (strNullDouble.toDouble() % 1 == 0.0 && str.text.last() != '.')
transformedString = strNullDouble.toInt().toString()
else
transformedString = str.text
return TransformedText(
text = AnnotatedString(transformedString),
offsetMapping = object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int {
return offset
}
override fun transformedToOriginal(offset: Int): Int {
return offset
}
}
)
}
}
how can I improve the behavior ?
What about not returning a double to your TextField but just the String?
fun onProductPriceChange(it: String) {
if (it == "") {
_viewState.value = _viewState.value.copy(price = null)
} else {
if (it.toDoubleOrNull() != null) {
_viewState.value = _viewState.value.copy(price = it)
}
}
}

Cannot convert value of type 'inout NSNumber?' to expected argument type 'AutoreleasingUnsafeMutablePointer<AnyObject?>' error

I have this script use to check whether the *downloaded file from iCloud is available or not. But unfortunately I encountered error Cannot convert value of type 'inout NSNumber?' to expected argument type 'AutoreleasingUnsafeMutablePointer<AnyObject?>' in some lines of code. Please help me to solve this issue because it is my first time to create a code to check whether the downloaded file is available in the icloud or not.
Please refer to the image below as sample of the error and also codes are available below for your reference. Hope you could help me. Thank you.
Sample screenshot of error
//-------------------------------------------------------------------
// ダウンロードできるか判定 Judgment or can be downloaded
//-------------------------------------------------------------------
func downloadFileIfNotAvailable(_ file: URL?) -> Bool {
var isIniCloud: NSNumber? = nil
do {
try (file as NSURL?)?.getResourceValue(&isIniCloud, forKey: .isUbiquitousItemKey)
if try (file as NSURL?)?.getResourceValue(&isIniCloud, forKey: .isUbiquitousItemKey) != nil {
if isIniCloud?.boolValue ?? false {
var isDownloaded: NSNumber? = nil
if try (file as NSURL?)?.getResourceValue(&isDownloaded, forKey: .ubiquitousItemIsDownloadedKey) != nil {
if isDownloaded?.boolValue ?? false {
return true
}
performSelector(inBackground: #selector(startDownLoad(_:)), with: file)
return false
}
}
}
} catch {
}
return true
}
It looks like you copied and pasted some really old code. Besides, this is Swift, not Objective-C. Do not use NSURL or getResourceValue. Your code should look more like this:
if let rv = try file?.resourceValues(forKeys: [.isUbiquitousItemKey]) {
if let isInCloud = rv.isUbiquitousItem {
// and so on
}
}
And so on; the same pattern applied to other keys. Note that there is no .ubiquitousItemIsDownloadKey either. You can condense things like this:
if let rv = try file?.resourceValues(
forKeys: [.isUbiquitousItemKey, .ubiquitousItemDownloadingStatusKey]) {
if let isInCloud = rv.isUbiquitousItem {
if let status = rv.ubiquitousItemDownloadingStatus {
if status == .downloaded {
}
}
}
}

How to check the “Allow Full Access” is enabled in iOS 11?

How to check the “Allow Full Access” is enabled in iOS 11?
I have tried multiple methods which do not seem to be working in iOS 10 or iOS 11.
Here is one that I tried:
func hasFullAccess() -> Bool
{
var hasFullAccess = false
if #available(iOSApplicationExtension 10.0, *) {
let pasty = UIPasteboard.general
if pasty.hasURLs || pasty.hasColors || pasty.hasStrings || pasty.hasImages {
hasFullAccess = true
} else {
pasty.string = "TEST"
if pasty.hasStrings {
hasFullAccess = true
pasty.string = ""
}
}
} else {
// Fallback on earlier versions
var clippy : UIPasteboard?
clippy = UIPasteboard.general
if clippy != nil {
hasFullAccess = true
}
}
return hasFullAccess
}
Every time it returns true and I am running this on a device not on the simulator.
First of all, it feels like you're checking the iOS version wrong. It usually looks like this if #available(iOS 11.0, *) where iOS version you pass is the latest one you use. In your declaration iOS 11 is not even there, it checks for iOS 10 and below.
Second, you're using || which is OR operator. So if any of those statements is true, the whole thing will return true. You need && the AND operator to check whether everything is matching.

iOS 11 - How does one read/parse the NDEF Message from CoreNFC?

I have a bunch of tags that are URL tags that have the content "http://WEBSITE.com". Let's say WEBSITE is youtube so http://youtube.com. When I scan them on Android etc, it keeps the http or https.
I'm trying to scan these same tags using the Core NFC framework. I scan them and I get a bunch of bytes that I convert using NSSString initWithData with UTF8 Encoding. I get back \^Cyoutube.com. I want to get http://youtube.com.
How can I interept the payload to get what I need? If I'm to assume the http in front of the string, how am I supposed to know if it is http or https or even ftp?
Edit 1:
I'm having issues with the below answer code for pure text records. When making a text record for "hello world" I get the following outputs from the console:
2017-06-09 12:45:35.151806-0400 testNFC[2963:190724] Payload string:https://www.enhello world
2017-06-09 12:45:35.154959-0400 testNFC[2963:190724] Payload data:<02656e68 656c6c6f 20776f72 6c64>
To get the string I use
NSString *nfcMessage = [nfcType stringByAppendingString:[[[NSString alloc] initWithData:payload.payload encoding:NSUTF8StringEncoding] substringFromIndex:1]];
nfcType is the return from your function but for the None case I return #"";
I'm expecting to just get hello world.
For this, you will first need to make sure you have properly formatted NDEF tags. You can use an Android phone or one of these reader accessories along with an NDEF writing app.
Implement the following methods:
- (NSString *)getType:(NSData *)NDEFData {
NSString *firstByte = [self getFirstByte:NDEFData];
if ([firstByte isEqualToString:#"00"]) {
return #"None";
} else if ([firstByte isEqualToString:#"01"]) {
return #"http://www.";
} else if ([firstByte isEqualToString:#"02"]) {
return #"https://www.";
} else if ([firstByte isEqualToString:#"03"]) {
return #"http://";
} else if ([firstByte isEqualToString:#"04"]) {
return #"https://";
} else if ([firstByte isEqualToString:#"05"]) {
return #"tel:";
} else if ([firstByte isEqualToString:#"06"]) {
return #"mailto:";
} else if ([firstByte isEqualToString:#"07"]) {
return #"ftp://anonymous:anonymous#";
} else if ([firstByte isEqualToString:#"08"]) {
return #"ftp://ftp.";
} else if ([firstByte isEqualToString:#"09"]) {
return #"ftps://";
} else if ([firstByte isEqualToString:#"0A"]) {
return #"sftp://";
} else if ([firstByte isEqualToString:#"0B"]) {
return #"smb://";
} else if ([firstByte isEqualToString:#"0C"]) {
return #"nfs://";
} else if ([firstByte isEqualToString:#"0D"]) {
return #"ftp://";
} else if ([firstByte isEqualToString:#"0E"]) {
return #"dav://";
} else if ([firstByte isEqualToString:#"0F"]) {
return #"news:";
} else if ([firstByte isEqualToString:#"10"]) {
return #"telnet://";
} else if ([firstByte isEqualToString:#"11"]) {
return #"imap:";
} else if ([firstByte isEqualToString:#"12"]) {
return #"rtsp://";
} else if ([firstByte isEqualToString:#"13"]) {
return #"urn:";
} else if ([firstByte isEqualToString:#"14"]) {
return #"pop:";
} else if ([firstByte isEqualToString:#"15"]) {
return #"sip:";
} else if ([firstByte isEqualToString:#"16"]) {
return #"sips:";
} else if ([firstByte isEqualToString:#"17"]) {
return #"tftp:";
} else if ([firstByte isEqualToString:#"18"]) {
return #"btspp://";
} else if ([firstByte isEqualToString:#"19"]) {
return #"btl2cap://";
} else if ([firstByte isEqualToString:#"1A"]) {
return #"btgoep://";
} else if ([firstByte isEqualToString:#"1B"]) {
return #"tcpobex://";
} else if ([firstByte isEqualToString:#"1C"]) {
return #"irdaobex://";
} else if ([firstByte isEqualToString:#"1D"]) {
return #"file://";
} else if ([firstByte isEqualToString:#"1E"]) {
return #"urn:epc:id:";
} else if ([firstByte isEqualToString:#"1F"]) {
return #"urn:epc:tag:";
} else if ([firstByte isEqualToString:#"20"]) {
return #"urn:epc:pat:";
} else if ([firstByte isEqualToString:#"21"]) {
return #"urn:epc:raw:";
} else if ([firstByte isEqualToString:#"22"]) {
return #"urn:epc:";
} else if ([firstByte isEqualToString:#"23"]) {
return #"urn:nfc:";
}
return #"";
}
/*!
* gets the the NDEF content
*/
- (NSString *)getNDEFContent:(NSData *)data {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return [dataString substringFromIndex:2];
}
/*!
* gets the first byte of the input NSData
*/
- (NSString *)getFirstByte:(NSData *)data {
return [[self dataToHexString:data] substringToIndex:2];
}
/*!
* transforms NSData to NSString
*/
- (NSString *)dataToHexString:(NSData *)data;
{
// get the length of the data
NSUInteger bytesCount = data.length;
if (bytesCount) {
// string with all the Hex characters
const char *hexChars = "0123456789ABCDEF";
// put bytes into an array and initialize the response array
const unsigned char *dataBuffer = data.bytes;
char *chars = malloc(sizeof(char) * (bytesCount * 2 + 1));
char *s = chars;
// go through data bytes making the transformations so a hex will literally translate to a string, so for example 0x0A will translate to "0A"
for (unsigned i = 0; i < bytesCount; ++i) {
// get hexChars character at binary AND between the current byte and 0xF0 bitwise to the right by 4 index and assign it to the current chars pointer
*s++ = hexChars[((*dataBuffer & 0xF0) >> 4)];
// get hexChars character at binary AND between the current byte and 0x0F index and assign it to the current chars pointer
*s++ = hexChars[(*dataBuffer & 0x0F)];
dataBuffer++;
}
*s = '\0';
// chars to string
NSString *hexString = [NSString stringWithUTF8String:chars];
free(chars);
return hexString;
}
return #"";
}
And call the getType method:
[self getType:yourNDEFPayloadNSData]
I'm assuming that all the methods are in the same class and,
that the payload NSData is NDEF compliant, but I modeled the code based on the NFCNDEFPayload payload
NFC NDEF message payload is much more complex than you expected. But CoreNFC does not support parsing NFC NDEF message payload. I created an open source parser VYNFCKit to parse payload. Example projects are available in both Objective-C and Swift. Check my tutorial https://medium.com/#vinceyuan/reading-and-parsing-nfc-tag-on-ios-11-60f4bc7a11ea
Regarding your EDIT1:
You are using the wrong Record Type.
You need to write a "Text Record", not a "URI Record".
If you have an Android Phone at hand, you could use tools like "NFC TagWriter by NFC" to write the correct record.
It might not be important for your use case, but think of interopability with Android Phones, resp. other apps. They would try to open "https://www.enhello world" instead of showing "Hello World", using EN encoding as a Text String.

Display image when bool variable is true

How can i do this:
I have 5 incentive images. I need to display each image if my bool variable is true.
Here's What I did:
BOOL var1 = true;
BOOL var2 = true;
BOOL var3 = false;
BOOL var4 = true;
BOOL var5 = false;
if(var1)
{
cell.incentive1.hidden = false;
}
if(var2)
{
cell.incentive2.hidden = false;
}
if(var3)
{
cell.incentive3.hidden = false;
}
if(var4)
{
cell.incentive4.hidden = false;
}
if(var5)
{
cell.incentive5.hidden = false;
}
this is working..
I have a follow up question,
how will i be able to display the var4's uiimageview var2's uiimageview. I need the uiimageview to be dynamically created. That when the app detects the bool variables that are true there will be no gaps.
thanks..
When you use if..else if condition, if any one condition passes then the remaining will not be evaluated.
So for fixing the issue, you need to change all to if conditions.
But I'll suggest using like following is a better approach (There is no need of if else conditions):
cell.incentive1.hidden = !var1;
cell.incentive2.hidden = !var2;
cell.incentive3.hidden = !var3;
cell.incentive4.hidden = !var4;
cell.incentive5.hidden = !var5;
You should not use if...else as all images are independent. You should:
if(var1)
{
cell.incentive1.hidden = false;
}
if(var2)
{
cell.incentive2.hidden = false;
}
if(var3)
{
cell.incentive3.hidden = false;
}
if(var4)
{
cell.incentive4.hidden = false;
}
if(var5)
{
cell.incentive5.hidden = false;
}
You've got chained if..else if statements going on there. This means that once one of the conditions is satisfied, the entire evaluation is done and it jumps to the next bit of code. Remove the else everywhere, so that every condition is evaluated in isolation.
Try this.
cell.incentive1.hidden = (var1 == true)?true:false;
cell.incentive2.hidden = (var2 == true)?true:false;
cell.incentive3.hidden = (var3 == true)?true:false;
cell.incentive4.hidden = (var4 == true)?true:false;
cell.incentive5.hidden = (var5 == true)?true:false;
As given code if var 1 is true then it will goes to 1st block other wise it goes to next.
and best way to hide and display images you can set alpha 0 for hide and 1 for display.

Resources