How to change safari setting programmatically - objective-c

is it posible to change Mac safari setting by app after getting user permission if yes then how its done by objective c code. if no why.

An application can only change settings of another application if sandbox is disabled, by editing the application settings dictionary file:
NSString *safariSettingsPath = [#"~/Library/Preferences/com.apple.Safari.plist" stringByExpandingTildeInPath];
NSMutableDictionary *safariSettings = [[NSMutableDictionary alloc] initWithContentsOfFile:safariSettingsPath];
safariSettings[#"ExtensionsEnabled"] = #(NO);
[safariSettings writeToFile:safariSettingsPath atomically:YES];

Related

How to create .plist file under /Library/LaunchAgents

I'm trying to develop a launch agent for macOS via Apple Doc
https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
One of my requirements is that the agent should work for all users. What I understood from above document is I have to put my .plist under "/Library/LaunchAgents" folder.
When I try to create this file programatically nothing happens with the below code.
NSMutableDictionary *plist = [[NSMutableDictionary alloc] init];
[plist setObject:#"test" forKey: #"test 1"];
NSString *userLaunchAgentsPath = [[NSString alloc] initWithFormat:#"%#", #"/Library/LaunchAgents/com.xxx.agent.plist"];
[plist writeToFile:userLaunchAgentsPath atomically:YES];
Probably the reason is a privilege issue. Do you have any ideas for solving this issue?
As to privileges, the plist should be owned by root and if you want the app to run as a different user, you can do that easily by providing the username/password in the plist. Your app is probably not running as root.

how to get version of default browser on my mac os x

I want to get system information in mac using objective C.
I am searching a lot but did not got single line of code for
my use.They provided solutions via javascript but i want them in
objective C.
Provide me some help to go ahead.
You can use launch services to get the path to the default browser as below
LSGetApplicationForURL((CFURLRef)[NSURL URLWithString: #"http:"],
kLSRolesAll, NULL, (CFURLRef *)&appURL);
NSString *infoPlistPath = [[appURL path] stringByAppendingPathComponent:#"Contents/info.plist"];
Now read the CFBundleShortVersionString from the info.plist.
Here you go :
NSString *userName=NSUserName();
NSLog(#"UserName: %#",userName);
NSArray *ipAddress=[[NSHost currentHost] addresses];
NSLog(#"IP Address=%#",ipAddress[0]);
Updating my answer
This is tested and works well
NSWorkspace *nsSharedWorkspace = [NSWorkspace sharedWorkspace];
NSString *nsAppPath = [nsSharedWorkspace fullPathForApplication:appName];
NSBundle *nsAppBundle = [NSBundle bundleWithPath: nsAppPath];
NSDictionary *nsAppInfo = [nsAppBundle infoDictionary];
//Now you can print all dictionary to view all its contents and pick which you want
NSLog(#"%#",nsAppInfo);
//or you can get directly using following methods
NSLog(#"%#",[nsAppInfo objectForKey:#"CFBundleShortVersionString"]);
NSLog(#"%#",[nsAppInfo objectForKey:#"CFBundleVersion"]);
Dont forget to add AppKit framework

NSWorkspace vs NSTask to start iTunes from a sandboxed app

I'm trying to run iTunes from my ObjectiveC app that runs in a sandbox.
Apple documentation mentions that 'child processes created with the NSTask class inherit the sandbox of the parent app'. The result is that when running iTunes, some permission error pops up and iTunes is closed.
When running it using NSWorkspace methods it does not crash and seems it's running outside any sandbox. Does that mean that i have permission to insert some dynamic library at launch time using DYLD_INSERT_LIBRARIES ?
Here's some code:
NSString* appPath = #"/Applications/iTunes.app";
// Get application URL
NSBundle *targetBundle = [NSBundle bundleWithPath:appPath];
NSURL *applicationURL = [targetBundle executableURL];
NSString* libPath = [NSHomeDirectory() stringByAppendingPathComponent:#"myLib.dylib"];
// Environment setup
NSDictionary *config = nil;
NSDictionary *env = [NSDictionary dictionaryWithObject:libPath forKey:#"DYLD_INSERT_LIBRARIES"];
NSNumber *arch = [NSNumber numberWithInt:(int)NSBundleExecutableArchitectureI386];
config = [[NSDictionary alloc] initWithObjectsAndKeys:env, NSWorkspaceLaunchConfigurationEnvironment,
arch, NSWorkspaceLaunchConfigurationArchitecture, nil];
// Launch application
[[NSWorkspace sharedWorkspace] launchApplicationAtURL:applicationURL
options:0
configuration:config
error:nil];
[config release];
When the above code runs in a sandbox iTunes starts without any lib. Any suggestion?
Thanks,
Vlad.

I want to delete all items in my self created KeyChain on Mac OS X

I'm writing a little tool to synchronize passwords. I'm using my own KeyChain for this purpose. Prior to saving, I want to clear this KeyChain. However, it seems I don't understand how to use the SecItemDelete function.
NSMutableDictionary *deleteQuery = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
kSecClassGenericPassword, kSecClass,
kSecMatchLimit, kSecMatchLimitAll, nil];
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)deleteQuery);
NSLog(#"%#", SecCopyErrorMessageString(status, NULL));
This is what I've written so far, but unfortunately my items (called Root.Foo and Root.Bar) remain in the KeyChain. Also I'm wondering, how this function knows, which KeyChain should be searched? Most examples I'm fonding are about iOS, where every Application has it own KeyChain by default.
Thanks for any help :)
Solved it:
I've missed passing in an array of KeyChains to look for! It seems on iOS, always the default KeyChain of an app is used but on Mac OS you need to specify the KeyChain, as an array containing SecKeychainRefs:
NSMutableDictionary *q = [NSMutableDictionary dictionary];
[q setObject:kSecClassGenericPassword forKey:kSecClass];
[q setObject:[NSArray arrayWithObject:(__bridge id)keyChain] forKey:kSecMatchSearchList];
[q setObject:kSecMatchLimitAll forKey:kSecMatchLimit];
SecItemDelete((__bridge CFDictionaryRef)q);
This code worked perfectly.

Post photo to Instagram using their iOS hooks

I use the following code in my iOS app to use Instagram iPhone hooks to post a photo to Instagram. I only want the "Open In..." menu to have Instagram app, no other apps. But in my case Camera+ also shows up. How can I restrict to Instagram?
Also, can I directly open Instagram instead of showing Open In menu?
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
//imageToUpload is a file path with .ig file extension
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imageToUpload]];
self.documentInteractionController.UTI = #"com.instagram.photo";
self.documentInteractionController.annotation = [NSDictionary dictionaryWithObject:#"my caption" forKey:#"InstagramCaption"];
[self.documentInteractionController presentOpenInMenuFromBarButtonItem:self.exportBarButtonItem animated:YES];
}
BTW Instagram added an exclusive file extention (ig) and UTI (com.instagram.exclusivegram) for this. It still opens the Open with... menu but the only option is Instagram.
More info here: https://instagram.com/developer/mobile-sharing/iphone-hooks/
You can get the solution from this link.
Save image with the .igo extension instead of .ig. This is the "exclusive" version of the filetype.
Create a UIDocumentInteractionController, then assign the value com.instagram.exclusivegram to the property UTI.
Present your UIDocumentInteractionController with presentOpenInMenuFromRect:inView:animated.
This worked for me, do it like this and you will have only Instagram as the exclusive app to open your image.
NSString *documentDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
// *.igo is exclusive to instagram
NSString *saveImagePath = [documentDirectory stringByAppendingPathComponent:#"Image.igo"];
NSData *imageData = UIImagePNGRepresentation(filteredImage);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
_docController=[[UIDocumentInteractionController alloc]init];
_docController.delegate=self;
_docController.UTI=#"com.instagram.photo";
[_docController setURL:imageURL];
_docController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:#"#yourHashTagGoesHere",#"InstagramCaption", nil];
[_docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
To answer only your first question: you may probably be able to restrict the "Open in ..." menu to just showing Instagram for your device (by deleting the Camera+ App, for example), but you won't be able to restrict users that install your app to their devices. And that's because the iPhone recognizes which applications are able to open a specific kind of files and it automatically show every one that does.
self.documentInteractionController = [self setupControllerWithURL:imgurl usingDelegate:self];
self.documentInteractionController=[UIDocumentInteractionController interactionControllerWithURL:imgurl];
self.documentInteractionController.UTI = #"com.instagram.exclusivegram";
use this code in same sequence .
here documentInteractionController is object of UIDocumentInteractionController.just for your knowledge.
you will get instagram only in "open in" window.