i have this part of code in my app
(void) trackWithCategory:(NSString*)category withAction:(NSString*)action withValue:(float)value
{
AppController *ac = (AppController*) [UIApplication sharedApplication].delegate;
BOOL result = [ac.tracker trackEventWithCategory:category
withAction:action
withLabel:[UIDevice currentDevice].uniqueIdentifier
withValue:[NSNumber numberWithInt:(int)(value+0.5)]];
if (!result)
NSLog(#"Google Analytics track event failed");
}
and when i'm trying to build it gives me en error about this line:
withLabel:[UIDevice currentDevice].uniqueIdentifier
it rights,
uniqueidentifier is deprecated first in ios 5
please
how can i fix it ?
how can i write it differently so that it will be ok .. ?
Use CFUUID
Create and store using the NSUserdefaults
some sample
NSString *identifierString = [[NSUserDefaults standardUserDefaults] objectForKey:#"myID"];
if (!identifierString) {
CFUUIDRef identifier = CFUUIDCreate(NULL);
identifierString = (NSString*)CFUUIDCreateString(NULL, identifier);
[[NSUserDefaults standardUserDefaults] setObject:identifierString forKey:#"myID"];
}
NSLog(#"%#",identifierString);
/* ... */
Related
I'm having trouble with my In-App Purchases. Upon leaving the MasterViewController (where the IAP items are listed and bought) I come to the ViewController which in the viewDidLoad it checks for purchased items, but I also have a viewWillAppear checking for purchased items and unlocking those purchased items right when the ViewController is loaded.
The problem is, every time the ViewController is reloaded or refreshed, left and then gone back to, the viewWillAppear checks for purchased items and unlocks the items again, which in my case the purchased items come from javascript files, which happens to load the JS multiple times, cluttering the app with the same IAP item over and over again.
For instance, if you purchased 'Bookmarks', every time you reload the ViewController, you'll get another 'Bookmarks' tool, over and over again.
How do I get it so that the IAP is loaded only one time?
My viewWillAppear looks like this:
- (void)viewWillAppear:(BOOL)animated {
// Check To See If The Uniques IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_UNIQUES_KEY] isEqualToString: #"YES"]){ // NEW CODE
// Code to show bookmarks
} else {
NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"Uniques" withExtension:#"js"] encoding:NSUTF8StringEncoding error:nil];
[viewWeb stringByEvaluatingJavaScriptFromString:jsString]; }
NSLog(#"Shows uniques?: %#", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_UNIQUES_KEY]);
// Check To See If The PL Numbers IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY] isEqualToString: #"YES"]){ // NEW CODE
// Code to show bookmarks
} else {
NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"PortalLevel" withExtension:#"js"] encoding:NSUTF8StringEncoding error:nil];
[viewWeb stringByEvaluatingJavaScriptFromString:jsString]; }
NSLog(#"Shows portal levels?: %#", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY]);
// Check To See If The PN IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTALS_KEY] isEqualToString: #"YES"]){ // NEW CODE
// Code to show bookmarks
} else {
NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"PortalNames" withExtension:#"js"] encoding:NSUTF8StringEncoding error:nil];
[viewWeb stringByEvaluatingJavaScriptFromString:jsString]; }
NSLog(#"Shows portal names?: %#", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTALS_KEY]);
// Check To See If The Bookmarks IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_BOOKMARKS_KEY] isEqualToString: #"YES"]){ // NEW CODE
// Code to show bookmarks
} else {
NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"bookmarks" withExtension:#"js"] encoding:NSUTF8StringEncoding error:nil];
[viewWeb stringByEvaluatingJavaScriptFromString:jsString]; }
NSLog(#"Shows bookmarks?: %#", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_BOOKMARKS_KEY]);
// Check To See If The Poke A Bowl Ad Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_ADS_KEY] isEqualToString: #"No"]){ // NEW CODE
// Code to show ads
_PokeABowlAd.hidden = NO;
} else {
_PokeABowlAd.hidden = YES;
}
NSLog(#"Shows ads?: %#", [[NSUserDefaults standardUserDefaults] objectForKey:SHOW_ADS_KEY]);
}
I think what you should do is make it so that each one is called at most once. You should use NSUserDefaults to accomplish this. Do something like this:
Put this in your AppDelegate.m:
//One new key for each IAP - put at the top of the AppDelegate.m and VC.m
#define IAP_FIRST_USED #"IAP 1 Used"
...
#define IAP_LAST_USED #"IAP X Used"
and
// In applicationDidFinishLaunchingWithOptions, should be the first method listed
[[NSUserDefaults standardUserDefaults] setObject: #"No" forKey: IAP_FIRST_USED];
...
[[NSUserDefaults standardUserDefaults] setObject: #"No" forKey: IAP_LAST_USED];
[[NSUserDefaults standardUserDefaults] synchronize];
And then in the VC.m modify viewWillAppear to be like this (this is an example of what just the first IAP should look like):
// Check To See If The PL Numbers IAP Has Been Paid For
if (![[[NSUserDefaults standardUserDefaults] objectForKey:SHOW_PORTAL_LEVEL_KEY] isEqualToString: #"YES"]){ // NEW CODE
// NEW CODE BEGINS HERE
if ([[[NSUserDefaults standardUserDefaults] objectForKey:IAP_FIRST_USED] isEqualToString: #"No"]){
[[NSUserDefaults standardUserDefaults] setObject: #"Yes" forKey: IAP_FIRST_USED];
[[NSUserDefaults standardUserDefaults] synchronize];
// Code to show bookmarks
// End of code to show bookmarks
// NEW CODE ENDS HERE
}
} else {
NSString *jsString = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"PortalLevel" withExtension:#"js"] encoding:NSUTF8StringEncoding error:nil];
[viewWeb stringByEvaluatingJavaScriptFromString:jsString];
}
Also remember to include the define statements in VC.m
I want to get the unique identifier which should support all iOS versions..Can any one help me on this issue. As you know that apple is deprecated the UDID method, So there is possibility to generate Unique id using wifi-mac address.But apple is going to remove the wifi mac address in iOS7 version.So my requirement is to generate a new unique code which should work in all iOS versions.Thanks in advance..
Note: Don't change the UDID once user restart the device or reinstall the application.
I was updating my application that was working based only on Unique Identifier which supported iOS 4.3 and above. So,
1) I was unable to use [UIDevice currentDevice].uniqueIdentifier; as it was no longer available
2) I could not use [UIDevice currentDevice].identifierForVendor.UUIDString because it was Available in iOS 6.0 and later only and was unable to use for lower iOS versions.
3) The mac address was not an option as it wasn't allowed in iOS-7
4) OpenUDID was deprecated some time ago and also had issues with iOS-6.
5) Advertisement identifiers were also not available for iOS-5 and below
Finally this was what i did
a) Added SFHFKeychainUtils to the project
b) Generated CFUUID key String
CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
udidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
c) Saved it to Key Chain Utils or else it will generate a new Unique Each Time
Final Code
+ (NSString *)GetDeviceID {
NSString *udidString;
udidString = [self objectForKey:#"deviceID"];
if(!udidString)
{
CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
udidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
CFRelease(cfuuid);
[self setObject:udidString forKey:#"deviceID"];
}
return udidString;
}
+(void) setObject:(NSString*) object forKey:(NSString*) key
{
NSString *objectString = object;
NSError *error = nil;
[SFHFKeychainUtils storeUsername:key
andPassword:objectString
forServiceName:#"LIB"
updateExisting:YES
error:&error];
if(error)
NSLog(#"%#", [error localizedDescription]);
}
+(NSString*) objectForKey:(NSString*) key
{
NSError *error = nil;
NSString *object = [SFHFKeychainUtils getPasswordForUsername:key
andServiceName:#"LIB"
error:&error];
if(error)
NSLog(#"%#", [error localizedDescription]);
return object;
}
For further Details
Now Device Identifier change to UUID.You can get UUID With the help of following code:
- (NSString *)getUUID
{
NSString *UUID = [[NSUserDefaults standardUserDefaults] objectForKey:#"uniqueID"];
if (!UUID) {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
UUID = [(__bridge NSString*)string stringByReplacingOccurrencesOfString:#"-"withString:#""];
[[NSUserDefaults standardUserDefaults] setValue:UUID forKey:#"uniqueID"];
}
return UUID;
}
It's Work in all iOS version.
I don't have access to the code right now (can post in a few hours if you still need it) but what I've done is create a static method 'deviceIdentifier' in a helper class.
the method does a basic check for the current iOS version, returns UDID if below 6.0 and uniqueIdentifier otherwise
Let me know if you'd like the code for that and I'll post it when I can..it's only 10-15 lines or so if I remember right but makes a big difference as then you can just call '[myHelper deviceIdentifier]' wherever you need a device ID and not have to worry about which iOS version they are using
The app's current behavior is, the user logged in once will not be logged out unless the user explicitly clicks on the logout.
I keep the user logged in, even if the user closes the app and opens it again.
When newer version of my app is released in appstore, I want to find out whether the user updated my app and opened it for the first time.
At that point I want to make them login again.
Is there a way to find out at the first time launch of the app after its been updated to latest version?
Create some kind of version #'s scheme. Note: You can enable Xcode to create backups and versions whenever you make substantial changes to the code.
There are a number of ways one could create a version constant, save it, and read it back.
When you update an app from the store, there is app data that persists from the previous installed version of the app, which you can read back to determine the version and, then update that persistent data to be ready for the next update cycle.
This answer was a very popular solution in another similar question.
Or, try something like #JitendraGandhi's ObjC answer below, or if you use Swift, try something like my port of #JitendraGandhi's ObjC example to Swift:
func hasAppBeenUpdatedSinceLastRun() -> Bool {
var bundleInfo = Bundle.main.infoDictionary!
if let currentVersion = bundleInfo["CFBundleShortVersionString"] as? String {
let userDefaults = UserDefaults.standard
if userDefaults.string(forKey: "currentVersion") == (currentVersion) {
return false
}
userDefaults.set(currentVersion, forKey: "currentVersion")
userDefaults.synchronize()
return true
}
return false;
}
You can save your currentversion to NSUserDefaults and use this method to check your version every time the app awakes:
#pragma mark - NSBundle Strings
- (NSString *)currentVersion
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"];
}
if the currentversion is different from stored... its time to show the login!
Hope it helps you.
Use NSUserDefaults to store the CFBundleVersion. Then check against it every time the application is launched.
// Check if new version
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *currentAppVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
if ([defaults objectForKey:#"savedAppVersionKey"] != nil) {
// Key exists
NSString *savedAppVersion = [defaults objectForKey:#"savedAppVersionKey"];
if ([currentAppVersion isEqualToString:savedAppVersion]) {
// Still running the same app version
// Do nothing
NSLog(#"App version: SAME");
}
else {
// The app version changed from the last launch
// Do something here
NSLog(#"App version: UPDATED");
}
}
// Set the key & synchronize
[defaults setObject:currentAppVersion forKey:#"savedAppVersionKey"];
If you want simple and easy solution, Use this function :
-(BOOL)isAppUpdated
{
NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [bundleInfo objectForKey:#"CFBundleShortVersionString"];
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"currentVersion"] isEqualToString:currentVersion])
{
return NO ;
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"currentVersion"];
return YES ;
}
}
Following code will return NO / YES. You can call this method multiple times to know whether app was updated before this launch or not.
- (BOOL)launchedFirstTimeAfterUpdate
{
static NSString *lastVersion;
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *versionKeyName = #"lastLaunchedVersion";
lastVersion = [[NSUserDefaults standardUserDefaults] stringForKey:versionKeyName];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:versionKeyName];
[[NSUserDefaults standardUserDefaults] synchronize];
});
if (!lastVersion.length)
{
// No last version means, launched first time
return NO;
}
if ([lastVersion compare:currentVersion options:NSNumericSearch] == NSOrderedAscending)
{
// Last version is less than current version
return YES;
}
return NO;
}
Is it possible to save and load data on Today Extension using NSUserDefaults?
After closing the Notification Center, the widget behaves like an app which is terminated, so any data results lost. How could I solve this issue?
This is my code:
NSUserDefaults *defaults;
- (void)viewDidLoad {
[super viewDidLoad];
defaults = [NSUserDefaults standardUserDefaults];
NSArray *loadStrings = [defaults stringArrayForKey:#"savedStrings"];
if ([loadStrings objectAtIndex:0] != nil) {
[display setText:[NSString stringWithFormat:#"%#", [loadStrings objectAtIndex:0]]];
}
if ([loadStrings objectAtIndex:1] != nil) {
calculatorMemory = [NSString stringWithFormat:#"%#", [loadStrings objectAtIndex:1]].doubleValue;
}
}
- (IBAction)saveData:(id)sender {
NSString *displayString;
NSString *memoryString;
NSArray *saveStrings = [[NSArray alloc] initWithObjects: displayString, memoryString, nil];
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:saveStrings forKey:#"savedStrings"];
[defaults synchronize];
}
You need to use app group identifier instead of com.*
For instance:
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"group.company.appgroup"];
Don't forget to synchronise when you store data
[shared synchronize];
You need to add the App Group stuff detailed under here and then if it actually worked (pretty iffy under beta) it should allow you to share NSUserDefault data like normal between the host and widget.
Edit: Normal NSUserDefaults does not work. Apple has implemented a new method. To use, simply redefine your NSUserDefaults instance like this:
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"com.you.app.container"];
For anyone wondering how in the world do you save and get values then look at this code.
In your regular app add this to save whatever you like in your *.m file.
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"group.yourcompanyname.TodayExtensionSharingDefaults"];
//save dic
[shared setObject:dictionary2 forKey:#"dicForTodayWidget"];
//save array
[shared setObject:tempArray2 forKey:#"arrayForTodayWidget"];
//save some value
[shared setObject:#"1234" forKey:#"myValForTodayWidget"];
[shared synchronize];
In your today widget under TodayViewController.m in viewDidLoad add this.
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"group.yourcompanyname.TodayExtensionSharingDefaults"];
//get dic
NSMutableDictionary *dictionary = [shared objectForKey:#"dicForTodayWidget"];
You first need the App Groups set up for both targets (application and the extension).
Then, use the
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"group.company.myapp"];
to obtain the defaults object which you can read from/write to as usual.
If you want to be notified of changes to the defaults, use the NSUserDefaultsDidChangeNotification in your widget (or app).
For a step-by-step tutorial explaining all this, take a look at this blog post.
#edukulele
Today Extension and Main app run on two processes. Today Extension can't receive NSUserDefaultsDidChangeNotifications. I tried use MMWormhole. It is very good.
I am using following code to check iPhone silent switch is ON or OFF :-
if (self)
{
self.detector = [SharkfoodMuteSwitchDetector shared];
CheckInViewController* sself = self;
self.detector.silentNotify = ^(BOOL silent)
{
[sself.silentSwitch setOn:silent animated:YES];
};
}
It works fine in iOS 6 and below but in iOS 7 it always gives TRUE value. So, Please any one tell, how to resolve this issue.
Thanks in advance.
It doesn't work in iOS 7, and it never really worked in iOS 6 if you look at why it doesn't work in iOS 7. This solution is based on the same code, so all credit to the original author though.
Keep mute.caf from your SharkfoodMuteSwitchDetector.
Create a new class, called HASilentSwitchDetector (or whatever), or replace the code in SharkfoodMuteSwitchDetector.
In the header file:
#import <AudioToolbox/AudioToolbox.h>
typedef void(^HASilentSwitchDetectorBlock)(BOOL success, BOOL silent);
#interface HASilentSwitchDetector : NSObject
+ (void)ifMute:(HASilentSwitchDetectorBlock)then;
#end
In the implementation file:
#import "HASilentSwitchDetector.h"
void MuteSoundPlaybackComplete(SystemSoundID ssID, void *clientData)
{
//Completion
NSDictionary *soundCompletion = CFBridgingRelease(clientData);
//Mute
NSTimeInterval interval = [soundCompletion[#"interval"] doubleValue];
NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] - interval;
BOOL isMute = elapsed < 0.2; // mute.caf is .2s long...
//Then
HASilentSwitchDetectorBlock then = soundCompletion[#"then"];
then(YES, isMute);
//Cleanup
SystemSoundID soundID = [soundCompletion[#"soundID"] integerValue];
AudioServicesRemoveSystemSoundCompletion(soundID);
AudioServicesDisposeSystemSoundID(soundID);
}
#implementation HASilentSwitchDetector
+ (void)ifMute:(HASilentSwitchDetectorBlock)then
{
//Check
if ( !then ) {
return;
}
//Create
NSURL *url = [[NSBundle mainBundle] URLForResource:#"mute" withExtension:#"caf"];
SystemSoundID soundID;
if ( AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID) == kAudioServicesNoError ) {
//UI Sound
UInt32 yes = 1;
AudioServicesSetProperty(kAudioServicesPropertyIsUISound, sizeof(soundID), &soundID,sizeof(yes), &yes);
//Callback
NSDictionary *soundCompletion = #{#"then" : [then copy], #"soundID" : #(soundID), #"interval" : #([NSDate timeIntervalSinceReferenceDate])};
AudioServicesAddSystemSoundCompletion(soundID, CFRunLoopGetMain(), kCFRunLoopDefaultMode, MuteSoundPlaybackComplete, (void *)CFBridgingRetain(soundCompletion));
//Play
AudioServicesPlaySystemSound(soundID);
} else {
//Fail
then(NO, NO);
}
}
#end
Use like so:
[HASilentSwitchDetector ifMute:^(BOOL success, BOOL silent) {
if ( success ) {
if ( ![[NSUserDefaults standardUserDefaults] boolForKey:forKey:kHasShownMuteWarning] && silent ) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kHasShownMuteWarning];
[[[UIAlertView alloc] initWithTitle:[#"Mute Warning" localized] message:[NSString stringWithFormat:[#"This %#'s mute switch is on. To ensure your alarm will be audible, unmute your device." localized], [[[UIDevice currentDevice] isiPad]? #"iPad" : #"iPhone" localized]] delegate:nil cancelButtonTitle:nil otherButtonTitles:[#"Ok" localized], nil] show];
}
}
}];