I'm trying to make my program provide a service to look up words. It works when I drag some text onto the icon but when I try to use the menu item it opens another instance of the application, freezes the application I sent the request from for about 30 seconds and then doesn't execute the method it was supposed to.
Here is my method:
- (void)serviceQuery:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {
// Test for strings on the pasteboard.
NSArray *classes = [NSArray arrayWithObject:[NSString class]];
NSDictionary *options = [NSDictionary dictionary];
if (![pboard canReadObjectForClasses:classes options:options]) {
*error = NSLocalizedString(#"Error: couldn't encrypt text.",
#"pboard couldn't give string.");
return;
}
NSString *baseURL = #"http://labs.valtyrorn.com/bin/leit.php?q=";
NSString *q = [pboard stringForType:NSPasteboardTypeString];
NSString * encoded = [q stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *query = [NSString stringWithFormat:#"%#%#", baseURL, encoded];
[webView setMainFrameURL:query];
NSLog(query);
}
and the service part of the plist looks like this:
<key>NSServices</key>
<array>
<dict>
<key>NSSendTypes</key>
<array>
<string>NSStringPboardType</string>
</array>
<key>NSMessage</key>
<string>serviceQuery</string>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Fáðu beygingarlýsingu</string>
</dict>
<key>NSPortName</key>
<string>BIN</string>
<key>NSRequiredContext</key>
<dict></dict>
</dict>
</array>
Does anyone know how to fix this?
Related
I'm using this project to get me started with the WebKit framework under OSX.
It loads resources locally, and I've been able to get it to work. However, when I alter awakeFromNib to load from a URL string like http://www.example.com/index.html It doesn't work, the view is blank, even though this is a valid URL.
Original:
- (void)awakeFromNib {
WebPreferences *prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:#"~/Library/WebViewExample/LocalStorage"];
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:#"/htdocs/640x480_naked.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
}
Modified for external URL:
- (void)awakeFromNib {
WebPreferences *prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:#"/tmp"];
NSString *urlText = #"http://www.example.com/index.html";
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
}
What could be wrong?
Thanks to the comment and link from #danh. This solved it for me.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
I have a plist that is structured like this:
<dict>
<key>memos</key>
<array>
<dict>
<key>title</key>
<string>First Memo</string>
<key>content</key>
<string>Testing one two three</string>
<key>category</key>
<string>testing</string>
</dict>
<dict>
<key>title</key>
<string>Test</string>
<key>content</key>
<string>This is a test memo.</string>
<key>category</key>
<string>testing</string>
</dict>
</array>
</dict>
I'm reading it into an array like this:
self.memos = [NSMutableArray array];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Memos" ofType:#"plist"];
NSDictionary *tempDictionary = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *tempArray = [tempDictionary objectForKey:#"memos"];
for(id dict in tempArray) {
NSString *title = [dict objectForKey:#"title"];
NSString *content = [dict objectForKey:#"content"];
NSString *category = [dict objectForKey:#"category"];
Memo *m = [[Memo alloc] initWithTitle:title content:content category:category];
[self.memos addObject:m];
}
My question is: how do I add a new Memo (a dictionary of three strings) to the plist (specifically, the array of Memo dictionaries in my plist)?
You have to grab the plist in a NSDictionary, then read the Memos array into a NSMutableArray, add a object to it, then save the mutable memos array as memos in the dictionary and write it back to the plist file.
Change this line:
NSArray *tempArray = [tempDictionary objectForKey:#"memos"];
to
self.memos = [tempDictionary objectForKey:#"memos"];
Hope it helps...
and make sure you write back the self.memos data back to the file.
Also note you cannot edit a file in the bundle. You need to ensure that you copy it over to the documents directory before you start editing it and save it.
Try this:
[tempArray addObject:NewDictionary That you want to insert];
then add this temp array to TempDictionary.
[tempDictionary setObject:tempArray forKey:#"memos"];
and then save tempdictionary to file.
[tempDictionary writeToFile:path atomically:YES];
Hope it Helps!!
I can't get it work to have the 8 items from the plist below.
I have a plist "settings.plist". Now I wan't from vragen -> category (self.catagorie - 1) -> question. The 8 items. But the array keeps empty.
What am I doing wrong?
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"instellingen.plist"];
NSMutableDictionary *instellingen = [NSMutableDictionary dictionaryWithContentsOfFile:path];
NSArray *vragen = [instellingen objectForKey:#"vragen"];
NSArray *cata = [vragen objectAtIndex: (self.catagorie-1)];
NSArray *question = [cata objectAtIndex: 0];
NSLog(#"count: %i", [question count]);
Plist file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>vragen</key>
<array>
<array> <--catagorydata
<array> <-- questiondata
<string>vraag</string>
<string>A</string>
<string>B</string>
<string>C</string>
<string>D</string>
<string>1</string>
<string>standaard</string>
<string></string>
</array>
<array>
<string>vraag2</string>
<string>A</string>
<string>B</string>
<string>C</string>
<string>D</string>
<string>1</string>
<string>afbeelding</string>
<string>afbeelding.jpg</string>
</array>
</array>
</array>
<key>firstBoot</key>
<true/>
<key>regelAtRuntime</key>
<true/>
</dict>
</plist>
First of all this line
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"instellingen.plist"]; ?
should be
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"settings.plist"];
I also had to delete these from the dictionary:
<--catagorydata
<-- questiondata
Besides the two things above, there is nothing wrong with your code or your dictionary.
After making those changes I tested your code and the output I got was:
tester[69637:c07] size of dict 3
tester[69637:c07] count: 8
which is correct according to the .plist file
So your problem is at the first 4 lines. Make sure your file is in Documents directory (use something like the following code to see whats going wrong)
NSLog(#"Path : %#",path);
I copied the file in the project folder (because I used the simulator) and used the following code and everything worked fine
NSBundle* b=[NSBundle mainBundle];
NSURL* path1=[b URLForResource:#"settings" withExtension:#"plist"];
NSMutableDictionary *instellingen = [NSMutableDictionary dictionaryWithContentsOfURL:path1];
NSLog(#"size of dict %d",[instellingen count]);
NSArray *vragen = [instellingen objectForKey:#"vragen"];
NSArray *cata = [vragen objectAtIndex: 0];
NSArray *question = [cata objectAtIndex: 0];
NSLog(#"count: %i", [question count]);
Just a quick read, but looks like you have an array of "categoryData" items, that contains an array of questionData items; questionData items are strings.
NSArray *vragen = [instellingen objectForKey:#"vragen"]; // count = 1
NSArray *cata = [vragen objectAtIndex: (self.catagorie-1)]; // count = 2
NSString *question = [cata objectAtIndex: 0];
Recommend you add some breakpoints and look at your data. OR, NSLog(#"data = %#", cata); etc.
I'm trying to read a plist file into NSArray using initWithContentsOfFile as described here
My code looks like this
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:
#"routes" ofType:#"plist" ];
routes = [[NSArray alloc] initWithContentsOfFile:plistPath ];
NSLog:(#"contents of myArray %#", routes);
routes.plist has a perfectly simple structure of array and 2 string values inside
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Root</key>
<array>
<string>1</string>
<string>2</string>
</array>
</dict>
</plist>
Everything seems to be fine. plistPath initializes with correct value, which means file is copied to the bundle and can be readed. But routes = [[NSArray alloc] initWithContentsOfFile:plistPath ]; returns 0x0 value. I think this is equal to nil
What is wrong with my code?
Actually your plist is a dictionary.
Change this:
<dict>
<key>Root</key>
<array>
<string>1</string>
<string>2</string>
</array>
</dict>
to this:
<array>
<string>1</string>
<string>2</string>
</array>
I think you want to do something like this:
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:
#"routes" ofType:#"plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *routes = [dictionary objectForKey:#"Root"];
I'm not english, new to objective c and new to this website. But i hope you will understand my problem. I tried to use the search engine but i couldn't find a answer...
THE PROBLEM:
i have a plist file like this
<plist version="1.0">
<array>
<dict>
<key>ID</key>
<string>001</string>
<key>CAT</key>
<string>A</string>
<key>TITLE</key>
<string>AAAA01</string>
</dict>
<dict>
<key>ID</key>
<string>002</string>
<key>CAT</key>
<string>A</string>
<key>TITLE</key>
<string>AAAA02</string>
</dict>
<dict>
<key>ID</key>
<string>003</string>
<key>CAT</key>
<string>B</string>
<key>TITLE</key>
<string>BBBB01</string>
</dict>
.....
</array>
</plist>
so i have many entries like this. every entry has a category (CAT) like in my example "A" and "B".
now i need a code to transform this to the following:
NSArray
NSDictionary
CAT => "A"
VALUES => {"AAAA01","AAAA02"}
NSDictionary
CAT => "B"
VALUES => {"BBBB01", ...}
ETC.
MY SOLUTION:
i get the plist with the following code
NSString *path = [[NSBundle mainBundle] pathForResource:#"Dictonary" ofType:#"plist"];
arrayIndex = [[NSArray alloc] initWithContentsOfFile:path];
Now i have everything saved in the arrayIndex but then i dont find a way to transform the code in a new array to get my wanted result.
please can anybody help me?
THANK YOU very much!
This is straight programming.
It is more easily done in two steps saving trying to find an entry to add to.
// First create a dictionary of CAT and the values,
NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
for (NSDictionary *item in arrayIndex) {
NSString *cat = [item valueForKey:#"CAT"];
NSMutableArray *tempArray = [tempDict valueForKey:cat];
if (!tempArray) {
tempArray = [NSMutableArray array];
[tempDict setValue:tempArray forKey:cat];
}
NSString *v = [item valueForKey:#"TITLE"];
[tempArray addObject:v];
}
NSLog(#"tempDict: %#", tempDict);
// This may well be a good final result
// This step transforms the dictionary into a list of dictionaries
NSMutableArray *newList = [NSMutableArray array];
NSArray *keys = [[tempDict allKeys] sortedArrayUsingSelector:#selector(compare:)];
for (NSString *cat in keys) {
[newList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
cat, #"CAT",
[tempDict valueForKey:cat], #"VALUES",
nil]];
}
NSLog(#"newList: %#", newList);