Although I find myself familiar with a bunch of programming languages, I fail for some time now to get used to Cocoa/Objective-C.
I wanted to fill a NSTableView with some content, with the following code:
- (int) numberOfRowsInTableView:(NSTableView *)aTableView {
return (int)[settingsPlist count];}
- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {
NSString *title = [[NSString stringWithString:[[aTableColumn headerCell] stringValue]] autorelease];
NSLog(title); // debug
NSLog(#"%i",rowIndex); // debug
if([title isEqual: #"Plugin Name"]) {
return [[settingsPlist objectForKey:[NSString stringWithFormat:#"%i",rowIndex]] objectForKey:#"name"];
} else {
return #"(n/a)";
}
}
settingsPlist is filled with the contents of a plist, looks like this:
<plist version="1.0">
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>Test-Plugin</string>
<key>version</key>
<string>0.1</string>
<key>type</key>
<string>Car</string>
<key>creator</key>
<string>Icke</string>
<key>checksum</key>
<string>0x32</string>
<key>link</key>
<string>http://</string>
</dict>
</dict>
The problem: It works fine for one row (you can tell by looking at the console output, thrown up by NSLog(title); and NSLog(rowIndex);), however, after that, the debugger halts the execution, throwing up EXC_BAD_ACCESS. I know what that means, but I don't know why I'm getting this and how to fix this.
Thanks for any help. :)
you did over release here,
NSString *title = [[NSString stringWithString:[[aTableColumn headerCell] stringValue]] autorelease];
-[NSString stringWithString:] already autorelease the returned value, so you didn't need to call it again.
Related
I have followed this answer Blocking incomming sms in ios 7. The problem is it blocks every message and its notification. Secondly it continuously call _processReceivedMessage_hooked method when I send message other then this number +923139303006.
I'm using OpenDev with Xcode 5, iOS 7.x.
#include <logos/logos.h>
#import <substrate.h>
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <libkern/OSAtomic.h>
#import "CoreTelephony.h"
id(*_processReceivedMessage_orig)(id, SEL, CTMessage*) = NULL;
id _processReceivedMessage_hooked(id self, SEL _cmd, CTMessage* msg);
#class IMDService;
static void (*_logos_orig$_ungrouped$IMDService$loadServiceBundle)(IMDService*, SEL); static void _logos_method$_ungrouped$IMDService$loadServiceBundle(IMDService*, SEL);
static void _logos_method$_ungrouped$IMDService$loadServiceBundle(IMDService* self, SEL _cmd) {
_logos_orig$_ungrouped$IMDService$loadServiceBundle(self, _cmd);
NSBundle *bundle =[NSBundle mainBundle];
NSLog(#"bundle identifier %# ***** ",[bundle bundleIdentifier]);
// if ([[bundle bundleIdentifier] isEqualToString:#"com.apple.imservice.sms"] && [bundle isLoaded])
// {
NSLog(#"Hoooking ***** ");
MSHookMessageEx(objc_getClass("SMSServiceSession"),
#selector(_processReceivedMessage:),
(IMP)_processReceivedMessage_hooked,
(IMP*)&_processReceivedMessage_orig);
// }
}
id _processReceivedMessage_hooked(id self, SEL _cmd, CTMessage* msg)
{
NSObject<CTMessageAddress>* phonenumber = [msg sender];
NSString *senderNumber = (NSString*) [phonenumber canonicalFormat];
CTMessagePart *itmes = [[msg items] objectAtIndex:0];
NSString* msgtxt = [[NSString alloc] initWithData:itmes.data encoding:NSASCIIStringEncoding];
NSLog(#"message %# ****",msgtxt);
if ([senderNumber isEqualToString:#"+923139303006"])
[[CTMessageCenter sharedMessageCenter] acknowledgeIncomingMessageWithId:[msg messageId]];
else
return _processReceivedMessage_orig(self, _cmd, msg);
}
static __attribute__((constructor)) void _logosLocalInit() {
{
Class _logos_class$_ungrouped$IMDService = objc_getClass("IMDService");
MSHookMessageEx(_logos_class$_ungrouped$IMDService, #selector(loadServiceBundle), (IMP)&_logos_method$_ungrouped$IMDService$loadServiceBundle, (IMP*)&_logos_orig$_ungrouped$IMDService$loadServiceBundle);
}
}
here is 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>Filter</key>
<dict>
<key>Bundles</key>
<array>
<string>com.apple.imagent</string>
</array>
</dict>
</dict>
</plist>
Try uncommenting if ([[bundle bundleIdentifier] isEqualToString:#"com.apple.imservice.sms"] && [bundle isLoaded]) check.
The reason is loadServiceBundle is called multiple times - there're several imagent plugins. Every time it's called you hook _processReceivedMessage: again and again rewriting your previous hooks. Because it all happens inside a single imagent process original _processReceivedMessage: implementation will be lost. As a result you recursively call your hooked function.
Also you using wrong NSBundle instance. [NSBundle mainBundle] returns you bundle of yourself i.e. com.apple.imagent daemon. You need NSBundle of the plugin being loaded. I covered that in my answer - you need to use IMDService -(NSBundle*)bundle. In your case, it will be [self bundle].
I have asked a similar question a couple of days ago...
While I have made some progress, I still cant seem to make it work.
Here is what I got up till now:
customWriteableObj.h:
#interface customWriteableObj : NSObject <NSCoding>
#property int integer;
#property NSString * string;
#property BOOL boo;
#end
customWriteableObj.m:
#import "customWriteableObj.h"
#implementation customWriteableObj
-(id)init
{
_integer = 117;
_string = #"aString";
_boo = YES;
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
_integer = [aDecoder decodeIntForKey:#"integer"];
_boo = [aDecoder decodeBoolForKey:#"boo"];
_string = [aDecoder decodeObjectForKey:#"string"];
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.string forKey:#"string"];
[aCoder encodeBool:self.boo forKey:#"boo"];
[aCoder encodeInteger:self.integer forKey:#"integer"];
}
#end
main:
customWriteableObj * x = [[customWriteableObj alloc]init];
NSMutableData *dat = [[NSMutableData alloc] init];
NSKeyedArchiver* arch1 = [[NSKeyedArchiver alloc]initForWritingWithMutableData:dat];
arch1.outputFormat = NSPropertyListXMLFormat_v1_0;
[arch1 encodeObject:x forKey:#"tester1"];
[arch1 finishEncoding];
[dat writeToFile:#"text.xml";
customWriteableObj * y = [[customWriteableObj alloc]init];
y = [NSKeyedUnarchiver unarchiveObjectWithFile:#"text.xml";
Output in XML:
<?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>$archiver</key>
<string>NSKeyedArchiver</string>
<key>$objects</key>
<array>
<string>$null</string>
<dict>
<key>$class</key>
<dict>
<key>CF$UID</key>
<integer>3</integer>
</dict>
<key>boo</key>
<true/>
<key>integer</key>
<integer>117</integer>
<key>string</key>
<dict>
<key>CF$UID</key>
<integer>2</integer>
</dict>
</dict>
<string>aString</string>
<dict>
<key>$classes</key>
<array>
<string>customWriteableObj</string>
<string>NSObject</string>
</array>
<key>$classname</key>
<string>customWriteableObj</string>
</dict>
</array>
<key>$top</key>
<dict>
<key>tester1</key>
<dict>
<key>CF$UID</key>
<integer>1</integer>
</dict>
</dict>
<key>$version</key>
<integer>100000</integer>
</dict>
</plist>
which doesnt seem to be correct, since I cant see any data here, plus, when I try to read it back I get a nil!
I also need to persist this object to a database on a java based server...
At a glance it looks like you are missing the super calls in your init methods. This means you overrode these initialization methods and never actually initialize an object. So when you deserialize you would just end up with nil. This should fix it:
-(id)init
{
if(self = [super init])
{
_integer = 117;
_string = #"aString";
_boo = YES;
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [self init])
{
_integer = [aDecoder decodeIntForKey:#"integer"];
_boo = [aDecoder decodeBoolForKey:#"boo"];
_string = [aDecoder decodeObjectForKey:#"string"];
}
return self;
}
EDIT with JSON serialization example
Create a method on your custom object class that converts the object into a serializable dictionary. It might look something like:
- (NSDictionary *) jsonDictionary
{
return #{#"integer": #(_integer), #"boo": #(_boo), #"string" : _string};
}
Then just call that iteratively on all the objects in your array whenever you are ready to send them over. That might look something like:
// Assuming you have an NSArray called customObjects
NSMutableArray *customObjectsJSON = [[NSMutableArray alloc] initWithCapacity:customObjects.count];
for(customWriteableObj *object in customObjects)
{
[customObjectsJSON addObject:[object jsonDictionary]];
}
At this point, customObjectsJSON is ready to be set as a parameter for a network request. Depending on what other tools you are using you may need to convert it to JSON yourself using NSJSONSerialization
It looks correct. In the XML you can see your 117 and aString values. The XML is a custom format so you generally shouldn't be trying to use it for anything other than storing and reloading.
Your result object when you try to load the archive back in is nil because you are reloading differently than you are archiving (in terms of structure). If you are archiving with [arch1 encodeObject:x forKey:#"tester1"]; then you need to unarchive with initForReadingWithData: and decodeObjectForKey:.
I am writing an app that copies a plist into the docsdir and then reads it into a mutable array. The code below, however, returns a count of 0 for the array. The line with the dictionary log, however, returns the correct items. I have also verified that the file is being copied to the docsdir.
-(NSString *)docsDir {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *listPath = [[self docsDir]stringByAppendingPathComponent:#"list.plist"];
if (![[NSFileManager defaultManager]fileExistsAtPath:listPath]) {
[[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:#"list" ofType:#"plist"] toPath:listPath error:nil];
NSLog(#"Chicken");
}
NSLog(#"%#", [NSDictionary dictionaryWithContentsOfFile:listPath]);
_array = [NSArray arrayWithContentsOfFile:listPath];
NSLog(#"Count: %i", [_array count]);
}
- (void)viewDidUnload
{
[super viewDidUnload];
/ / Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
Usually this is because by default root element in plist files is a Dictionary.
Right click and select Open as Source Code, your file may look like this:
<?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>foo</key>
<array>
<string>foo1</string>
<string>foo2</string>
<string>foo3</string>
</array>
</dict>
</plist>
where the root element is a dict, change it to:
<?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">
<array>
<string>foo1</string>
<string>foo2</string>
<string>foo3</string>
</array>
</plist>
where the root element is an array. You can now edit as usual.
Open the plist file with Xcode.
Just find the 'key' at the top left corner and Type of the plist.
Make sure the Type is Array.
I think your plist is a dictionary that contains an array.
Try this
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:listPath]
NSArray *array = [dict objectForKey:#"key"];
Plists are usually saved a dictionaries:
Here's an example :
<dict>
<key>tiltingAnim</key>
<dict>
<key>filenamePrefix</key>
<string>radar_</string>
<key>delay</key>
<real>0.25</real>
<key>animationFrames</key>
<string>1,2,3,4,5,5,4,3,2,1,2,3,4,5</string>
</dict>
<key>takingAHitAnim</key>
<dict>
<key>filenamePrefix</key>
<string>radar_</string>
<key>delay</key>
<real>0.1</real>
<key>animationFrames</key>
<string>5,5,7,8,8</string>
</dict>
<key>blowingUpAnim</key>
<dict>
<key>filenamePrefix</key>
<string>radar_</string>
<key>delay</key>
<real>0.2</real>
<key>animationFrames</key>
<string>5,6,7,8,9,10,11,12,13,14,15,16,17</string>
</dict>
<key>transmittingAnim</key>
<dict>
<key>filenamePrefix</key>
<string>radar_</string>
<key>delay</key>
<real>0.3</real>
<key>animationFrames</key>
<string>5,6,5,6,5,6,5</string>
</dict>
</dict>
Now, there are 2 solutions to your question.
Either get the contents into a dictionary then take out the array for a specific key.
Open up the plist with a text editor and change the root key into then change root into . If your plist is static and in the bundle resources then you could do this but if it's a plist generated by your code then i wouldn't recommend this.
I have an NSDocument based app that already handles *.txt files. I can drag and drop those files in the dock and my app will launch correctly.
Now I want to be able to open *.text files too. So I have added:
<key>CFBundleTypeExtensions</key>
<array>
<string>.txt</string>
<string>.text</string>
</array>
to my *plist
However I can't open *.text files by drag&dropping them in the dock. (I can do MyApp> File>Open> myFile.text )
I can open *.txt as usual, and drag and drop them in the dock.
I have tried implementing these NSApp delegate methods but now I get an error and I can't open any kind of file.
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename{
NSURL *url = [NSURL URLWithString:filename];
NSError *error = nil;
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:url display:YES error:&error];
if (error) {
NSLog(#"error: %#", [error localizedDescription]);
error = nil;
return NO;
}
return YES;
}
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames{
for (NSString *filename in filenames) {
[self application:sender openFile:filename];
}
}
Error:
typeForContentsOfURL:error: must be
overridden for your application to
support non-'file:' URLs.
I am not subclassing NSDocumentController, so do I really need to? or is there a better/easier way of doing this?
Thanks in advance
The file extensions in the Info.plist file should not contain a period character (.). What happens if you do this?:
<key>CFBundleTypeExtensions</key>
<array>
<string>txt</string>
<string>text</string>
</array>
Currently there is a button on my app inteface which allow to open a file, here is my open code:
In my app.h:
- (IBAction)selectFile:(id)sender;
In my app.m:
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}
- (IBAction)selectFile:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
NSArray *fileTypes = [NSArray arrayWithObjects:#"xml",nil];
NSInteger result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];
if(result == NSOKButton){
NSString * input = [openPanel filename];
How can I edit my code to allow opening with the application-icon drag & drop?
Note: I edited the .plist file and added a line for "xml" but it change anything, got an error when my file is dropped on the icon.
Note 2: I linked the "File -> Open..." to the selectFile: wich refer to my code
Note 3: My app isn't a document-based application
Thanks for your help!
Miskia
First add the proper extensions to CFBundleDocumentTypes inside the .plist file.
Next implement the following delegates:
- application:openFile: (one file dropped)
- application:openFiles: (multiple files dropped)
Reference:
NSApplicationDelegate Protocol Reference
Response to comment:
Step by step example, hopefully it makes everything clear :)
Add to the .plist file:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>xml</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>application.icns</string>
<key>CFBundleTypeMIMETypes</key>
<array>
<string>text/xml</string>
</array>
<key>CFBundleTypeName</key>
<string>XML File</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSIsAppleDefaultForType</key>
<true/>
</dict>
</array>
Add to ...AppDelegate.h
- (BOOL)processFile:(NSString *)file;
- (IBAction)openFileManually:(id)sender;
Add to ...AppDelegate.m
- (IBAction)openFileManually:(id)sender;
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
NSArray *fileTypes = [NSArray arrayWithObjects:#"xml",nil];
NSInteger result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];
if(result == NSOKButton){
[self processFile:[openPanel filename]];
}
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
return [self processFile:filename];
}
- (BOOL)processFile:(NSString *)file
{
NSLog(#"The following file has been dropped or selected: %#",file);
// Process file here
return YES; // Return YES when file processed succesfull, else return NO.
}
for a quick&dirty solution:
Cocoa: Drag and Drop any file type
Tested under Xcode Version 11.4 (11E146)
catalina 10.15.4 (19E266)