History of previously connected bluetooth devices using MCNearbyServiceBrowser - ios7

I'm using MCNearbyServiceBrowser to discover nearby bluetooth devices & inviting using invitePeer: toSession: withContext: timeout:.
Now the problem is that i want to keep history of all connected devices & re-connect them in future (manually by clicking them in UITableview) if the are nearby again. Is this possible? And is it compatible according to Apple's policy? Thanks.
UPDATE:
I already achieved the goal as "jamdaddy25" answered (before he answered :P) & it's working fine too.. But what if two devices have the same name? In that case it will be a problem of not showing one of the two or more devices in the list. And i don't want that. BTW thanks for response.

You could keep the peerID display name. This is a property on MCPeerID. When you have started browsing, either prior to user selecting a previously connected peer or starting up browsing upon coming to that screen, cycle through the nearby peers and see if u have a display name match. If so, you can systematically send an invite and reestablish the connection.
You could even do this so the only selectable previous connections are those which are currently nearby peers.
Update:
So to make sure that you don't have peer name collisions you need to make the names unique. The best way I know how to do this is to create a UUID based name. I save this inside of a simple object (UserPeerInfo below) and saving / load this to NSUserDefaults so this peer name will be used always for this peer
// Initialize with any stored data
if (!_userPeerInfo) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"userPeerInfo"]) {
NSData *userPeerInfoData = [defaults objectForKey:#"userPeerInfo"];
_userPeerInfo = (UserPeerInfo*)[NSKeyedUnarchiver unarchiveObjectWithData:userPeerInfoData];
} else
{
NSString *peerName = [[NSUUID UUID] UUIDString];
_userPeerInfo.peerName = peerName;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Create an NSData representation
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_userPeerInfo];
[defaults setObject:data forKey:#"userPeerInfo"];
[defaults synchronize];
}
}
Then when setting the name of your peer and initializing, use that peer name like normal
self.peerId = [[MCPeerID alloc] initWithDisplayName:self.userPeerInfo.peerName];
self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.peerId discoveryInfo:info serviceType:kServiceType];
self.advertiser.delegate = self;
[self.advertiser startAdvertisingPeer];

Related

How to Persist Application State in cocoa

I need to store the state of application at application termination time, so that when user re-run app, App run from the state in which it was closed last time. It is some kind of restoring app but restore methods called when app close unexpectedly. But i need to restore app each time when it close unexpectedly of user close it manually.
I just need to store the App UI not the data of application.
Any idea would be helpful for me.
Thanks
You can persist the state in any of the available methods like:
i. NSUserDefaults
Example:
//saving
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"testBool"];
//retrieving
[defaults boolForKey:#"testBool"];
ii. Serializing the state object.
iOS 4 iPhone Data Persistence using Archiving
Correct way to save/serialize custom objects in iOS
iii. Saving as a plist file
Example:
NSMutableDictionary *stateDictionary = [NSMutableDictionary dictionary];
//set state
...
//saving
[stateDictionary writeToFile:<filePath> atomically:YES];
//retrieve
stateDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:<filePath>]
iv. Using sqlite or Core-Data
(Most probably not needed unless the
state of your app is in some kind of a object relational model)
UPDATE:
For preserving the UI state of windows,
Check this link and under the heading USER INTERFACE PRESERVATION.
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
//saveData
return NSTerminateNow;
}
If you want to save NSWindow position , you can use [window saveFrameUsingName:#"myWindow"];
and use
[window setFrameAutosaveName:#"myWindow"]; # the app launch.

How to keep information from reseting

I have a check box type application, kinda like notes on the iPhone. When the user closes the application and double tabs the home button and deletes the application from the background, the things they have typed also get deleted or reset. I was thinking of calling
[self saveContext];
But that didn't work. WHat should I be calling?
You can usual use something such as NSUserDefaults to save information. Or, you can go another route and save the information in a data structure and write that to a file. There are many easy ways of doing this. For instance, if you want to preserve a checked value, you could do the following:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:"checked"];
[defaults synchronize];
Very simple, and then can be queried across app exits, like so:
if( [defaults boolForKey:"checked"] ) {
//do stuff here
}

NSUserDefaults error for "objectAtIndex"

Getting an error message on this line of code:
Team *team = [leagueLocal.teams objectAtIndex:indexpath.row];
Error is "Use of undeclared identifier 'indexpath'"... but I'm not sure what I would put at objectAtIndex: other than indexpath.row, because thats what works for me in the TableView part of this app.
Big picture, I'm trying to use NSUserDefaults save the Team Name that was selected. Team Names are being pulled in via JSON from a web service.
Here is the full code for the NSUserDefaults part I have so far:
// Create User Defaults Variable
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Tell it where to look
Sport *sportLocal = [sports objectAtIndex:0];
League *leagueLocal = [sportLocal.leagues objectAtIndex:0];
Team *team = [leagueLocal.teams objectAtIndex:indexpath.row]; // This is where error is
// Store the data, this is the stuff I want to save
[defaults setObject:team.abbreviation forKey:[NSString stringWithFormat:#"teamAbbreviation%d", _buttonNumber]];
// After saving, synchronize data
[defaults synchronize];
Thanks for the help! Let me know if any more info is needed, or where I can help clarify if needed.
Edit- I added an NSLog in my viewDidLoad right below the error to see what I was getting, and it came back null: NSLog(#"object at index: %#", team.abbreviation);
If this is inside viewDidLoad, then unless you've initialized the indexPath variable it will not be valid. It's only valid in your tableView methods because it's passed in through the function.
You have a couple options to grab the indexPath that your looking for though:
Team *team = [leagueLocal.teams objectAtIndex:[NSIndexPath indexPathForRow:i inSection:k];
//where i and k are integers that indicate the row/section for the tableViewCell your looking for...(k would just be equal to 0 if you only have one section)
Or another way would be if the user has previously selected what your looking for then you can use:
NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
Team *team = [leagueLocal.teams objectAtIndex:indexPath.row];//now you can use it the way you have it written in your question
Hope it helps!

Get ios multi-value user preferences

I try to retrieve the value selected in Settings Application using "Settings Bundle". The field is "PSMultiValueSpecifier". In "Root.plist" I implemented:
Type: PSMultiValueSpecifier
Title: Abcdefg
Key: abcdefg
DefaultValue: aaa
Values (array):
Item 0: aaa
Item 1: bbb
Titles (array):
Item 0: A
Item 1: B
And, In the implement file I have wrote:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *string = [defaults objectForKey:#"abcdefg"];
But "string" is empty. When I do the same with a user default of kind: "PSTextFieldSpecifier" I can retrieve the value.
I follow the instructions explained in the book "Apress - Beginning iOS 5 Development: Exploring the iOS SDK" but I can not retrieve the value selected.
Instructions are the same of the "Apple Development Help".
I do not understand what is the problem. It seems easy.
I have found the bug.
If you implement "Multi Value" user preferences in "Settings.bundle" (with its "Default Value"), but never enter in settings iPhone simulator and never change the multi-value preference, the sentence:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *string = [defaults objectForKey:#"key"];
Returns a "nil" value. Incredible, I lost a week trying to find the problem!!!!
But I have a doubt:
If I implement a "Multi Value" user preferences and I publish the App in iTunes Store, but user not use this "Multi Value" user preferences, "NSUserDefaults" will return nill value?
Do I have to consider the possibility of receiving an empty value?
Really, I think it is ridiculous.
It seems that DefaultValue is used only in the official Settings app. Apparently, you have to register the defaults by 1) testing one of your settings to see if it's NULL, then 2) iterating through the settings, grabbing and setting the defaults:
sample Obj-C code for this by just.do.it
The documentation on PSMultiValueSpecifier does seem deficient here. Confusion on this shows up all over SO and the web. For example:
defaultvalue-for-psmultivaluespecifier on SO
app-preferences-how-to-get-psmultivaluespecifier-property on SO
No, I think you need to register your default value, for example in:
-(BOOL) application: (UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
NSDictionary* defaults = #{#"key": #"default value"};
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
This is because the app knows nothing about the settings bundle it contains. The page 430 of book Beginning iOS6 Development: Exploring the iOS SDK has a clear explanation of this.
if the returned object is nil then set the default value.
NSString * key = #"the key";
NSUserDefaults * uD = [NSUserDefaults standardUserDefaults];
NSString * stringValue = [uD objectForKey:key];
if (!stringValue) {
stringValue = #"the default value";
[uD registerDefaults:#{key: stringValue}];
}

How to know if window is minimizable when titlebar was double clicked?

This image is from SystemPreferences > Appearance
I want to know How do I get that value programmatically?
I ask because I am drawing a window with a customized titlebar and I want it to resemble (in behavior) as much as possible to normal (non-customized) cocoa windows.
Maybe a terminal command I can pipe or is there an cocoa API that does this?
EDIT:
Answer (thanks to NSGod)
- (void)mouseUp:(NSEvent *)event{
if ([event clickCount] == 2) {
//Get settings from "System Preferences" > "Appearance" > "Double-click on windows title bar to minimize"
NSString *const MDAppleMiniaturizeOnDoubleClickKey = #"AppleMiniaturizeOnDoubleClick";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary
BOOL shouldMiniaturize = [[userDefaults objectForKey:MDAppleMiniaturizeOnDoubleClickKey] boolValue];
if (shouldMiniaturize) {
[self miniaturize:self];
}
}
}
Later I found that Appearance (Aqua/Graphite) can be found:
NSString * const kAppleAquaColorVariant = #"AppleAquaColorVariant";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary
NSNumber *color = [userDefaults objectForKey:kAppleAquaColorVariant];
if ([color intValue] == 6) {//graphite is 6
imageName = [imageName stringByAppendingFormat:#"_graphite"];
}else{//defaults to aqua, (aqua is 1)
imageName = [imageName stringByAppendingFormat:#"_colorsryg"];
}
Which can be helpful too :)
The way I would do it is probably read the value in from user defaults.
NSString * const MDAppleMiniaturizeOnDoubleClickKey = #"AppleMiniaturizeOnDoubleClick";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary
NSNumber *miniaturize = [userDefaults
objectForKey:MDAppleMiniaturizeOnDoubleClickKey];
NSLog(#"AppleMiniaturizeOnDoubleClick == %#",
([miniaturize boolValue] ? #"YES" : #"NO"));
(This preference setting is stored in the invisible .GlobalPreferences.plist in your ~/Library/Preferences/ folder).
Note that by default, the "double-click to minimize" option is turned off, so if you check for the presence of the AppleMiniaturizeOnDoubleClick and it returns nil, that means it is off. (User defaults only start to store values if they differ from the defaults).
This key is the same in Leopard as it is in Snow Leopard. (It hasn't changed in Lion or Mountain Lion either).
Of course, there is a secret (private) method in NSWindow, -(BOOL)_shouldMiniaturizeOnDoubleClick, but I wouldn't recommend using private methods.
[UPDATE] Regarding Catfish_Man's comment: you are correct in that the line [userDefaults addSuiteNamed:NSGlobalDomain]; is unnecessary, as NSUserDefaults already has the ability to read global preferences. (I modified the code above to reflect this).
"Additionally, NSGlobalDomain is not translated to
.GlobalPreferences.plist for that method."
I'm not sure I follow you there. NSUserDefaults is built on top of CFPreferences which defines the following 6 constants:
Application:
kCFPreferencesAnyApplication,
kCFPreferencesCurrentApplication
Host:
kCFPreferencesAnyHost,
kCFPreferencesCurrentHost
User:
kCFPreferencesAnyUser,
kCFPreferencesCurrentUser
Given a fictional application bundle identifier of "com.markdouma.App" and a single host (based on your current network location that won't change for this example), there are generally 8 locations where preference information could be stored on your disk. (NOTE: The paths shown are for demonstration purposes only; the exact file path locations are subject to change). The 8 different locations arise from the different combination of the CFPreferences constants:
/Library/Preferences/.GlobalPreferences.plist
(kCFPreferencesAnyApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost)
/Library/Preferences/com.markdouma.App.plist
(kCFPreferencesCurrentApplication, kCFPreferencesAnyUser,
kCFPreferencesAnyHost)
/Library/Preferences/ByHost/.GlobalPreferences.UNIQUE_HOST_IDENTIFIER.plist (kCFPreferencesAnyApplication, kCFPreferencesAnyUser, kCFPreferencesCurrentHost)
/Library/Preferences/ByHost/com.markdouma.App.UNIQUE_HOST_IDENTIFIER.plist (kCFPreferencesCurrentApplication,
kCFPreferencesAnyUser, kCFPreferencesCurrentHost)
~/Library/Preferences/.GlobalPreferences.plist
(kCFPreferencesAnyApplication, kCFPreferencesCurrentUser,
kCFPreferencesAnyHost)
~/Library/Preferences/com.markdouma.App.plist
(kCFPreferencesCurrentApplication, kCFPreferencesCurrentUser,
kCFPreferencesAnyHost)
~/Library/Preferences/ByHost/.GlobalPreferences.UNIQUE_HOST_IDENTIFIER.plist (kCFPreferencesAnyApplication,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost)
~/Library/Preferences/ByHost/com.markdouma.App.UNIQUE_HOST_IDENTIFIER.plist (kCFPreferencesCurrentApplication,
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost)
While NSUserDefaults can only write to the domain combination shown in italics, it automatically has read access to the domain combinations shown in bold. In other words, without having to do anything, I can automatically run the following code and print the results:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *miniaturize = [userDefaults
objectForKey:#"AppleMiniaturizeOnDoubleClick"];
NSNumber *fastUserSwitching = [userDefaults
objectForKey:#"MultipleSessionEnabled"];
NSLog(#"AppleMiniaturizeOnDoubleClick == %#",
([miniaturize boolValue] ? #"YES" : #"NO"));
NSLog(#"MultipleSessionEnabled == %#",
([fastUserSwitching boolValue] ? #"YES" : #"NO"));
Running that code on my system prints the following results:
AppleMiniaturizeOnDoubleClick == YES
MultipleSessionEnabled == YES
This makes sense, since I have both Fast User Switching and Double-click to minimize options enabled. MultipleSessionsEnabled is stored in the local domain at /Library/Preferences/.GlobalPreferences.plist, and AppleMiniaturizeOnDoubleClick is stored in the user domain at ~/Library/Preferences/.GlobalPreferences.plist.
Sample project: NSUserDefaultsFinagler.zip
"Additionally additionally, that's slow. Please don't do this."
Sorry, but that makes no sense (assuming that we've agreed that we're no longer using addSuiteNamed:). User defaults are cached by the application, making calls take in the matter of milliseconds. Wouldn't there be little noticeable difference between asking user defaults for the value for a key that represents a local application value or one that represents a global value?
AFAIK, this is the only "legal" (App-store-compatible) way to achieve the OP's goal. If there's another more efficient means, then please elaborate.