While building an app using Android Studio with the Flutter SDK I decided to get the shared_preferences plugin and play around with it. Soon after, I noticed that although the app was running fine on my Android device, it wouldn't build to run on Simulator. In an attempt to figure out a solution, I tried running it directly from Xcode but again, it's failing there too.
Following the errors, I opened the GeneratedPluginRegistrant.m file where the editor is just saying "shared_preferences/SharedPreferencesPlugin.h file not found"
Running flutter doctor in the terminal doesn't give any useful info and running pod install doesn't help.
I've also tried adding FlutterFire plugins and they all produce the same result.
Any help is appreciated!
I think this is a known issue https://github.com/flutter/flutter/issues/15099#issuecomment-372375566
To apply the fix in #15437 to a Swift-based Flutter project created before the fix landed, add [these lines] to ios/Podfile.
Which I think means https://github.com/mravn-google/flutter/blob/e0c73220a6f69d341ce436244212277d83bc189b/packages/flutter_tools/templates/cocoapods/Podfile-swift#L70-L73
# workaround for https://github.com/CocoaPods/CocoaPods/issues/7463
target.headers_build_phase.files.each do |file|
file.settings = { 'ATTRIBUTES' => ['Public'] }
end
I also got this problem for many days in my Flutter app for iOS and have done almost everything explained here and in github.
My solution was (1) after "flutter upgrade" call "flutter run" (also you have to run simulator or attach device for a correct work of this command) and (2) after successful building of an app through the command line, mentioned above, close it and build in a regular way with Xcode via Product -> Build.
The concept of Shared Preference is android specific and not available in iOS. If you want to save something, I recommend you use Method Channel(Platform Channel) in Flutter to send it to the native layer and save it.
For iOS use userDefaults to save your data.
Additional info:
How to save data using Shared Preference?
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
editor.commit(); // Don't forget to commit when your changes are done.
To retrive data
// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
How to save data using userDefaults?
NSString *valueToSave = #"someValue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave
forKey:#"preferenceName"];
[[NSUserDefaults standardUserDefaults] synchronize];
To retrieve saved data
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"preferenceName"];
UPDATE :
There is a flutter plugin which does this for us.
Add this to your pubspec.yaml file
dependencies:
shared_preferences: "^0.4.0"
And run in command line in your project root directory.
$ flutter packages get
Now in your Dart code you can use,
import 'package:shared_preferences/shared_preferences.dart';
Related
I have a Vue PWA and it stopped creating my IndexDB object stores on first load or upgrade. Here is my code, I am using the latest version of IDB (https://github.com/jakearchibald/idb):
await openDB('dbname', 1, {
upgrade(db, oldVersion, newVersion, transaction) {
switch (newVersion) {
case 0:
// a placeholder case so that the switch block will
// execute when the database is first created
// (oldVersion is 0)
// falls through
case 1:
db.createObjectStore('change_log', {keyPath: 'id'});
db.createObjectStore('person', {keyPath: 'id'})
.createIndex('username', 'username');
break;
}
}
});
I have tried multiple browsers and incognito tabs, etc. and the same thing always happens. The database is created, but no object stores are created. I use developer tools to clear all the data in the PWA and refresh but the same thing happens.
If I increment the version number, the version of my database gets updated in the browser, but the object stores still do not get added.
The upgrade() function does not get called.
I had this happen to me earlier in my development, and I fixed it, but I can't remember how. I feel like it may not actually be a coding issue...
OK, I found the problem. I added a logging mechanism to my App and there was code running BEFORE my upgrade code that was opening the database to create a log entry. Therefore, it was creating the database (with no object stores) before my upgrade method was being called. I changed my open database code to always include the upgrade method to solve my problems.
I am trying to publish our flutter application to the google play store and Im getting what appears to be a show stopper error...
Error
This release is not compliant with the Google Play 64-bit requirement
The following APKs or App Bundles are available to 64-bit devices, but they only have 32-bit native code: 3.
Include 64-bit and 32-bit native code in your app. Use the Android App Bundle publishing format to automatically ensure that each device architecture receives only the native code it needs. This avoids increasing the overall size of your app. Learn More
Does anyone know how to fix this? Im not seeing anything when I google this error. We are just trying to get a Closed Alpha test out there.
I was able to finally fix this by adding this to build.gradle
ndk {
abiFilters "armeabi-v7a", "arm64-v8a"
}
}
splits {
abi {
include "armeabi-v7a", "arm64-v8a"
}
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a":1, "arm64-v8a":2]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
I am trying to use some old IOKit functionality in a new Swift 4.0 Mac app (not iOS). I have created a bridging header to use an existing Objective C third party framework, DDHidLib, and I am current working in Xcode 9.
The code that attempts to create a plug in interface for a usb gamepad falls over on IOCreatePlugInInterfaceForService, returning a non-zero error.
The truly bizarre thing is I have an older app created in a previous version of Xcode that uses the same framework and works correctly after opening in the new Xcode 9. This previous project is still Swift using a bridging header for the same Obj-C framework. I have checked the build settings and tried to make everything match, but I get the same result; the old app works but any new apps do not.
Is there a way to either: find out the exact differences in build settings/compilers to see what the elusive difference may be, OR to step into the IOCreatePlugInInterfaceForService IOKit method to see what may be causing the error to be returned in one project but not another?
EDIT: Here is the method that is failing:
- (BOOL) createDeviceInterfaceWithError: (NSError **) error_; {
io_name_t className;
IOCFPlugInInterface ** plugInInterface = NULL;
SInt32 score = 0;
NSError * error = nil;
BOOL result = NO;
mDeviceInterface = NULL;
NSXReturnError(IOObjectGetClass(mHidDevice, className));
if (error)
goto done;
NSXReturnError(IOCreatePlugInInterfaceForService(mHidDevice, kIOHIDDeviceUserClientTypeID,kIOCFPlugInInterfaceID,&plugInInterface,&score));
if (error)
goto done;
//Call a method of the intermediate plug-in to create the device interface
NSXReturnError((*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &mDeviceInterface));
if (error)
goto done;
result = YES;
done:
if (plugInInterface != NULL)
{
(*plugInInterface)->Release(plugInInterface);
}
if (error_)
*error_ = error;
return result;
}
In the old version that works, IOCreatePlugInInterfaceForService always returns a value of 0. In all the versions that don't work, the return value appears to always be -536870210. The mHidDevice in this function is the io_object_t handle for the device.
EDIT2: Here is the IORegistryExplorer page for the device
Finally managed to resolve this after weeks of head scratching. The new Xcode 9 uses app sandboxing to basically prevent access to USB, bluetooth, camera and microphone etc. by default in a new app. Once I switched this off it reverted to it's expected behaviour.
Glad it was such a simple answer in the end but disappointed Xcode does not provide more descriptive error messages or responses to let a user know they are essentially preventing themselves from accessing the parts of the system they need.
Looks like kIOReturnNoResources is returned if the loop at the end of IOCreatePlugInInterfaceForService completes with haveOne == false for whatever reason. Perhaps Start() is returning false because another process or driver already has exclusive access? I'd check what clients the device has in IORegistryExplorer.
This error also happens when an application is trying to access the camera or bluetooth on MacOS 10.14 and higher. Permission shall be granted either explicitly by user (pop-up window), or through the Security & Privacy. The application should check for permission as shown here.
I wrote an apk to test camera on Android 4.2.2 before. This apk works fine.
However, when I moved this apk to Android 4.4.
I got a problem with Camera::connect().
Fail to call Camera::connect() and it prints message:
W/AppOps ( 1546): Bad call: specified package TestCamera under uid 1000 but it is really -1
I think the reason may be USE_CALLING_UID, security or something that I can't figure out.
Please give me some suggestions, thanks!
My apk is very simple, only one activity. In onCreate(), I called a jni function.
The jni function just do the code belowed:
int cameraId = 0;
String16 clientPackageName("TestToGoService");
sp<Camera> camera = Camera::connect(cameraId, clientPackageName, Camera::USE_CALLING_UID);
if (camera == NULL) {
ALOGE("camera==NULL.");
return -1;
}
ALOGV("camera=%p.",camera.get());
Try:
If I put the code above to a executable (main()), then Camera::connect() works OK.
I have already add permissons on AndroidManifest.xml
Thanks again!
I'm not sure if it's still of any help. I had the same error in the past. The problem is clientPackageName, that has to be set to the exact package name of your application (which must have the proper camera permissions set on the manifest).
Let's assume that we can change in runtime Localizable.strings, that is placed in NSBundle
At the current moment, even if we change it's contents, NSLocalizedString would return old(cached) values.
Run Application
Get LocalizableString for specific key1 <- value1
Change Localizable.strings key1 = value2
<-- Do something in application to invalidate Localization cache -->
Check if LocalizableString for specific key1 == value2
What I've already tried:
[[NSBundble mainBundle] invalidateResourceCache]
[UIApplication _performMemoryWarning]
Tried to see, if there's some dictionaries. used for caching, in ivars in NSBundle.
Tried to see, in GNUStep implementation of NSBundle, but it's different from that we have in ios 6.0
What I cannot do (by definition):
- I cannot swizzle [NSBundle localizableStringForKey:value:table]
- I cannot change macroses
- In general, I cannot affect Any Original Project code, only add something at step #4
This is only for development purposes only. So, I don't need to publish it in AppStore or something, so any private methods, or solutions are OK.
So, the question is. May be someone know the way to do it, or someone who give me another ideas how to do it? Thank you.
NOTE: This solution uses private APIs and your app submissions to the App Store will be rejected if you use this code.
So, after some search I've found link that helped me
How to remove NSBundle cache
// First, we declare the function. Making it weak-linked
// ensures the preference pane won't crash if the function
// is removed from in a future version of Mac OS X.
extern void _CFBundleFlushBundleCaches(CFBundleRef bundle)
__attribute__((weak_import));
BOOL FlushBundleCache(NSBundle *prefBundle) {
// Before calling the function, we need to check if it exists
// since it was weak-linked.
if (_CFBundleFlushBundleCaches != NULL) {
NSLog(#"Flushing bundle cache with _CFBundleFlushBundleCaches");
CFBundleRef cfBundle =
CFBundleCreate(nil, (CFURLRef)[prefBundle bundleURL]);
_CFBundleFlushBundleCaches(cfBundle);
CFRelease(cfBundle);
return YES; // Success
}
return NO; // Not available
}
After flushing bundle cache, new localizations keys are used.
So now I don't need to restart my application in simulator in order to see changes in localizable strings.
You can use the uncache solution.
use Localizable.nocache.strings in your lproj folders.
e.g. example.bundle/Resources/de.lproj/Localizable.nocache.strings
loading localized strings after getting url via FileManager.
func localizedString(key: String) -> String {
let bundle = Bundle(url: bundleUrl)
return bundle.localizedString(forKey: key, value: nil, table: "Localizable.nocache")
}