Mac OS X 10.10 Reorder preferred networks - objective-c

What is the best way to programmatically change the order of "preferred" networks in OS X? Objective-C preferred...
I can use CoreWLAN to gather the list, and even add to it, but as far as re-ordering I am at a loss. I can create a copy of the preference file, edit it and change the order of precedence, and then use a bash script to write over the existing configuration, but that seems a but messy.
I am aware of the networksetup -addpreferredwirelessnetworkatindex command, but it does not work correctly in 10.10 (works fine for 10.9 systems) - it adds but does not set order properly.
SystemConfiguration framework? Something else?
Thanks!

I was looking for a way to accomplish this after transitioning a user from an open wireless network to a WPA2E network using EAP-TTLS. Since the user connects to the open network first, it remains higher in the Preferred Networks list.
Here is what I came up with:
CWInterface *interface = [CWInterface interfaceWithName:[
[CWInterface interfaceNames] anyObject]
];
CWMutableConfiguration *config = [CWMutableConfiguration
configurationWithConfiguration:interface.configuration
];
NSMutableArray *networks = [NSMutableArray arrayWithArray:
[config.networkProfiles array]
];
//Remove URI_Open (if present) and
//move URI_Secure (if present) to index 0
for (CWNetworkProfile *profile in [networks copy]) {
if ([[profile ssid] isEqualToString:#"URI_Secure"]) {
[networks removeObject:profile];
} else if ([[profile ssid] isEqualToString:#"URI_Open"]) {
CWNetworkProfile *tmp = profile;
[networks removeObject:tmp];
[networks insertObject:tmp atIndex:0];
}
}
config.networkProfiles = [NSOrderedSet orderedSetWithArray:networks];
SFAuthorization *auth = [SFAuthorization authorization];
BOOL authResult = [auth obtainWithRight:"system.preferences"
flags:(
kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize
) error:nil
];
NSError *error = nil;
[interface commitConfiguration:config authorization:auth error:&error];
Some notes/disclaimers:
I do not use OS X regularly. I have one test Mac in my office. It has 10.7.5 installed.
This is the first thing I have ever written in Objective-C. It is the result of a single afternoon; as such it is probably broken and ugly. YMMV.
Question specified 10.10. I used interfaceWithName and interfaceNames, which are deprecated in 10.10. I am not sure what the proper replacement is, but I suspect CWWifiClient.
My approach was based loosely on this ruby program.
I removed error handling for brevity.
I did look into using networksetup or removing the open network right in the .mobileconfig, but neither seemed to work quite right.
Since I just pull the network profile list out into a mutable array, this is easily adaptable to any arbitrary sorting etc.

Related

Overwriting SystemVersion.plist (Objective-c)

I'm writing an application that can change your Mac OS X version for a short period (few seconds) to launch an application that is to old or to new for your Version.
It reads the version from SystemVersion.plist (/System/Library/CoreServices/SystemVersion.plist) then sets the new required version.
Save (overwrite) the file and launch the app and then change the version back
to the orginal version.
This is the first part of the code (reads version and tries to overwrite)
// Get SystemVersion.plist as Dictionary
NSBundle *bundle = [NSBundle bundleWithPath:#"/System/Library/CoreServices"];
NSString *SystemVersionString = [bundle pathForResource:#"SystemVersion" ofType:#"plist"];
NSMutableDictionary *SystemVersionDir = [NSDictionary dictionaryWithContentsOfFile:SystemVersionString];
// Get current version (Systemversion, ProductUserVisibleVersion)
NSString *SystemVersion = [SystemVersionDir objectForKey:#"ProductVersion"];
// Get desired version from text field
NSString *DesiredVersion = [_Text_version stringValue];
[_Text_Info setStringValue:SystemVersion];
// Set new version
[SystemVersionDir setObject:DesiredVersion forKey:#"ProductVersion"];
// Write (save) plist
if([SystemVersionDir writeToFile:#"/System/Library/CoreServices/SystemVersion.plist" atomically:YES])
{
NSLog(#"YES");
}
else
{
NSLog(#"NO");
}
The problem is with the 'writeToFile'. If I write to my desktop or something everything goes fine and it creates a new file. Also if I copy the file to my desktop and overwrite it there it goes fine. But it needs to overwrite the one in /System/Library/CoreServices/.
I guess I need authorization or something. However I've looked into that but it seems an awful lot of work and my mind is mess because of that.
Isn't there a simpler way to do this, perhaps a class that helps?
Note: Mac OS X 10.8 Mountain Lion Developer Preview 3 (Xcode 4.3.1)

Automatic Core Data migration fails on Mac OS X 10.5 but not on 10.6 or 10.7

I've got a NSPersistentDocument-based Core Data app that targets 10.5 Leopard and above. I'm about to release an update that makes changes to the data model, and therefore need to migrate existing documents to the new model. The changes are relatively straightforward, and I've created a mapping model for them. Note that I'm not trying to do automatic lightweight migration, I do have a mapping model (lightweight migration isn't supported on Leopard, but my model changes rule it out anyway). In my NSPersistentDocument subclass, I override -configurePersistentStoreCoordinatorForURL... as follows:
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url
ofType:(NSString *)fileType
modelConfiguration:(NSString *)configuration
storeOptions:(NSDictionary *)storeOptions
error:(NSError **)error
{
NSMutableDictionary *newOptions = (storeOptions ?
[NSMutableDictionary dictionaryWithDictionary:storeOptions] :
[NSMutableDictionary dictionary]);
[newOptions setObject:(id)kCFBooleanTrue forKey:NSMigratePersistentStoresAutomaticallyOption];
return [super configurePersistentStoreCoordinatorForURL:url
ofType:fileType
modelConfiguration:configuration
storeOptions:newOptions
error:error];
}
This works fine on 10.6 and 10.7. However, on 10.5, the call to [super configurePersistentStore...] throws an exception and fails. The error is:
Error Domain=NSCocoaErrorDomain Code=134020 UserInfo=0x15812d70 "The model configuration used to open the store is incompatible with the one that was used to create the store."
If I instead initiate the migration manually, using this code:
NSArray *bundles = [NSArray arrayWithObject:[NSBundle mainBundle]];
NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:bundles forStoreMetadata:sourceMetadata];
NSManagedObjectModel *destinationModel = [psc managedObjectModel];
NSMappingModel *mappingModel = [NSMappingModel mappingModelFromBundles:bundles forSourceModel:sourceModel destinationModel:destinationModel];
NSMigrationManager *migrationManager = [[[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel] autorelease];
BOOL migrationSuccessful = [migrationManager migrateStoreFromURL:backupURL
type:NSXMLStoreType
options:storeOptions
withMappingModel:mappingModel
toDestinationURL:url
destinationType:NSXMLStoreType
destinationOptions:storeOptions
error:error];
return [psc addPersistentStoreWithType:NSXMLStoreType configuration:configuration URL:url options:storeOptions error:error] != nil;
the migration works OK. However, I'd prefer to use automatic migration, if for no other reason than because it makes for cleaner code. Has anyone seen a similar problem with automatic migration that works on 10.6+ but not on 10.5? My hunch is that it's something fairly simple like the built in migration code can't find the mapping model for some reason, but I can't figure out what it must be.
I'm not 100% this relates to your problem, but there is a documented workaround from Apple on how to migrate Core Data models for 10.6 that also must be compatible with 10.5. It appears that a method is missing in 10.5 that 10.6 relies on for migration.
destinationInstancesForSourceRelationshipNamed:sourceInstances:
is the missing method.
Hopefully this helps.
Reference: http://developer.apple.com/library/mac/#/legacy/mac/library/releasenotes/Cocoa/MigrationCrashBuild106Run105/_index.html
NSPersistentStore is the culprit. It does not implement migrationManagerClass until 10.6.
http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSPersistentStore_Class/NSPersistentStore.html
The Apple work around (as you already have used) is to creation the migration manager yourself rather than relying on the NSPersistentStore to provide it.

Checking File sizes for changes

I have an app that watches a folder for incoming jobs and then processes them. A job consists of a Folder with several job files inside the folder. Jobs are usually copied over the internet so when a folder is added to my Watched Folder I'm having my app get the attributes for the files inside the job folder, wait 20 seconds, and compare the current attributes for NSFileSize to see if there have been any changes. When everything matches, and no changes are detected it can pass the job folder along to be processed. This is the code I have:
while (fileSizes == NO && fileCount == NO) {
NSLog(#"going for a loop");
NSArray *jobFiles = [fm subpathsAtPath:jobPath];
NSMutableArray *jobFileAttrs = [[NSMutableArray alloc] init];
int i = 0;
while (i < [jobFiles count]) {
NSString *filePath = [jobPath stringByAppendingPathComponent:[jobFiles objectAtIndex:i]];
[jobFileAttrs addObject:[fm attributesOfItemAtPath:filePath error:nil]];
++i;
}
sleep(20);
NSArray *jobFiles2 = [fm subpathsAtPath:jobPath];
NSMutableArray *jobFileAttrs2 = [[NSMutableArray alloc] init];
i = 0;
while (i < [jobFiles2 count]) {
NSString *filePath = [jobPath stringByAppendingPathComponent:[jobFiles2 objectAtIndex:i]];
[jobFileAttrs2 addObject:[fm attributesOfItemAtPath:filePath error:nil]];
++i;
}
if ([jobFiles count] == [jobFiles2 count]) {
i = 0;
fileSizes = YES;
while (i < [jobFiles count]) {
NSLog(#"Does %ul = %ul", [[jobFileAttrs objectAtIndex:i] objectForKey:NSFileSize], [[jobFileAttrs2 objectAtIndex:i] objectForKey:NSFileSize]);
if ([[jobFileAttrs objectAtIndex:i] objectForKey:NSFileSize] != [[jobFileAttrs2 objectAtIndex:i] objectForKey:NSFileSize]){
fileSizes = NO;
}
++i;
}
if (fileSizes)
fileCount = YES;
}
This code works as intended in Lion, but when I run the App on Snow Leopard I get inconsistent values for the NSFileSize attribute. Every time the loop runs I get a completely different set of value than even the previous loop. This is obviously for a folder full of files that is no longer being copied and should give matching values for the file sizes.
Why doesn't this work in Snow Leopard, and what do I need to do to fix this? Part of my problem is I'm only set up for development on a Lion Machine, so I have to make build, and then transfer it to a snow leopard machine with out a debugger to test. It's making it hard for me to trouble shoot.
There are a few problems with this but for one thing, you are comparing the value of two objects using a boolean operator (!=). This is comparing the pointer locations of the two objects, not their value.
To compare two objects, you must use the isEqual: method:
if (![[[jobFileAttrs objectAtIndex:i] objectForKey:NSFileSize] isEqual:[[jobFileAttrs2 objectAtIndex:i] objectForKey:NSFileSize]])
{
fileSizes = NO;
}
Secondly, this is fundamentally bad design. What your code is doing is called polling, and the presence of sleep() in the code is a bad sign. You should never use sleep in Cocoa code, especially if your code is executing on the main thread as it will block the main run loop.
If you absolutely must use polling, you should use an NSTimer object.
However, in this particular case you don't need to use polling to determine when there has been a change to the folder contents, you can use the FSEvents API instead. The API is C-based and a bit obtuse so you might want to use Stu Connolly's SCEvents Objective-C wrapper.
What you should be doing is maintaining an array of the current files and their file sizes (probably as an NSArray instance variable containing dictionaries with keys for file name and file attributes) and then when you are notified of a change on disk, get the current status of the files and compare that with the information in your stored array. You would then replace your stored array with the updated file information.
The other file monitoring option is kqueues. These differ from FSEvents in that they are specific to a particular file, whereas FSEvents monitors directories, not individual files. In your case, kqueues may actually be more appropriate. Uli Kusterer has written a great Objective-C wrapper for kqueues. If you use this, you just need to start monitoring a particular file and you'll be notified whenever it changes via a delegate. This would be a simpler option if you only need to check one file at a time.
I hope this makes sense.

Get list of installed apps on iPhone

Is there a way (some API) to get the list of installed apps on an iPhone device.
While searching for similar questions, I found some thing related to url registration, but I think there must be some API to do this, as I don't want to do any thing with the app, I just want the list.
No, apps are sandboxed and Apple-accepted APIs do not include anything that would let you do that.
You can, however, test whether a certain app is installed:
if the app is known to handle URLs of a certain type
by using [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"thisapp://foo"]
You can get a list of apps and URL schemes from here.
For jailbroken devices you can use next snipped of code:
-(void)appInstalledList
{
static NSString* const path = #"/private/var/mobile/Library/Caches/com.apple.mobile.installation.plist";
NSDictionary *cacheDict = nil;
BOOL isDir = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] && !isDir)
{
cacheDict = [NSDictionary dictionaryWithContentsOfFile: path];
NSDictionary *system = [cacheDict objectForKey: #"System"]; // First check all system (jailbroken) apps
for (NSString *key in system)
{
NSLog(#"%#",key);
}
NSDictionary *user = [cacheDict objectForKey: #"User"]; // Then all the user (App Store /var/mobile/Applications) apps
for (NSString *key in user)
{
NSLog(#"%#",key);
}
return;
}
NSLog(#"can not find installed app plist");
}
for non jailbroken device, we can use third party framework which is called "ihaspp", also its free and apple accepted. Also they given good documentation how to integrate and how to use. May be this would be helpful to you. Good luck!!
https://github.com/danielamitay/iHasApp
You could do this by using the following:
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
SEL selector = NSSelectorFromString(#"defaultWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector];
SEL selectorALL = NSSelectorFromString(#"allApplications");
NSMutableArray *Allapps = [workspace performSelector:selectorALL];
NSLog(#"apps: %#", Allapps);
And then by accessing each element and splitting it you can get your app name, and even the Bundle Identifier, too.
Well, not sure if this was available back when the last answer was given or not (Prior to iOS 6)
Also this one is time intensive, yet simple:
Go into settings > Gen. >usage. The first category under usage at least right now is Storage.
It will show a partial list of apps. At the bottom of this partial list is a button that says "show all apps".
Tap that and you'll have to go through screen by screen, and take screenshots (Quick lock button and home button takes a screenshot).
I'm doing this now and I have hundreds of apps on my iPhone. So it's going to take me a while. But at least at the end of the process I'll have Images of all my apps.

How to make QTMovie play file from URL with forced (MP3) type?

I'm using QTKit to progressively download and play an MP3 from a URL. According to this documentation, this is the code I should use to accomplish that:
NSURL *mp3URL = [NSURL URLWithString:#"http://foo.com/bar.mp3"];
NSError *error = nil;
QTMovie *sound = [[QTMovie alloc] initWithURL:mp3URL error:&error];
[sound play];
This works, and does exactly what I want — the MP3 URL is lazily downloaded and starts playing immediately. However, if the URL does not have the ".mp3" path extension, it fails:
NSURL *mp3URL = [NSURL URLWithString:#"http://foo.com/bar"];
NSError *error = nil;
QTMovie *sound = [[QTMovie alloc] initWithURL:mp3URL error:&error];
[sound play];
No error is given, no exception is raised; the duration of the sound is just set to zero, and nothing plays.
The only way I have found to work around this is to force a type by loading the data manually and using a QTDataReference:
NSURL *mp3URL = [NSURL URLWithString:#"http://foo.com/bar"];
NSData *mp3Data = [NSData dataWithContentsOfURL:mp3URL];
QTDataReference *dataReference =
[QTDataReference dataReferenceWithReferenceToData:mp3Data
name:#"bar.mp3"
MIMEType:nil];
NSError *error = nil;
QTMovie *sound = [[QTMovie alloc] initWithDataReference:dataReference error:&error];
[sound play];
However, this forces me to completely download ALL of the MP3 synchronously before I can start playing it, which is obviously undesirable. Is there any way around this?
Thanks.
Edit
Actually, it seems that the path extension has nothing to do with it; the Content-Type is simply not being set in the HTTP header. Even so, the latter code works and the former does not. Anyone know of a way to fix this, without having access to the server?
Edit 2
Anyone? I can't find information about this anywhere, and Google frustratingly now shows this page as the top result for most of my queries...
Two ideas. (The first one being a bit hacky):
To work around the missing content type, you could embed a small Cocoa webserver that supplements the missing header field and route your NSURL over that "proxy".
Some Cocoa http server implementations:
http://code.google.com/p/cocoahttpserver/
http://cocoawithlove.com/2009/07/simple-extensible-http-server-in-cocoa.html
http://culturedcode.com/cocoa/
The second one would be, to switch to a lower level framework (From QTKit to AudioToolbox).
You'd need more code, but there are some very good resources out there on how to stream mp3 using AudioToolbox.
e.g.:
http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html
Personally I'd go with the second option. AudioToolbox isn't as straightforward as QTKit but it offers a clean solution to your problem. It's also available on both - iOS and Mac OS - so you will find plenty of information.
Update:
Did you try to use another initializer? e.g.
+ (id)movieWithAttributes:(NSDictionary *)attributes error:(NSError **)errorPtr
You can insert your URL for the key QTMovieURLAttribute and maybe you can compensate the missing content type by providing other attributes in that dictionary.
This open source project has a QTMovie category that contains methods to accomplish similar things:
http://vidnik.googlecode.com/svn-history/r63/trunk/Source/Categories/QTMovie+Async.m
If you thought weichsel's first solution was hacky, you're going to love this one:
The culprit is the Content-Type header, as you have determined. Had QTKit.framework used Objective-C internally, this would be a trivial matter of overriding -[NSHTTPURLResponse allHeaderFields] with a category of your choosing. However, QTKit.framework (for better or worse) uses Core Foundation (and Core Services) internally. These are both C-based frameworks and there is no elegant way of overriding functions in C.
That said, there is a method, just not a pretty one. Function interposition is even documented by Apple, but seems to be a bit behind the times, compared to the remainder of their documentation.
In essence, you want something along the following lines:
typedef struct interpose_s {
void *new_func;
void *orig_func;
} interpose_t;
CFStringRef myCFHTTPMessageCopyHeaderFieldValue (
CFHTTPMessageRef message,
CFStringRef headerField
);
static const interpose_t interposers[] __attribute__ ((section("__DATA, __interpose"))) = {
{ (void *)myCFHTTPMessageCopyHeaderFieldValue, (void *)CFHTTPMessageCopyHeaderFieldValue }
};
CFStringRef myCFHTTPMessageCopyHeaderFieldValue (
CFHTTPMessageRef message,
CFStringRef headerField
) {
if (CFStringCompare(headerField, CFSTR("Content-Type"), 0) == kCFCompareEqualTo) {
return CFSTR("audio/x-mpeg");
} else {
return CFHTTPMessageCopyHeaderFieldValue(message, headerField);
}
}
You might want to add logic specific to your application in terms of handling the Content-Type field lest your application break in weird and wonderful ways when every HTTP request is determined to be an audio file.
Try replacing http:// with icy://.
Just create an instance like this...
QTMovie *aPlayer = [QTMovie movieWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
fileUrl, QTMovieURLAttribute,
[NSNumber numberWithBool:YES], QTMovieOpenForPlaybackAttribute,
/*[NSNumber numberWithBool:YES], QTMovieOpenAsyncOKAttribute,*/
nil] error:error];