Difference between revisions of "ActionMenu"

From iPhone Development Wiki
Jump to: navigation, search
(fixing link to theos)
m (New How to use this library format.)
 
Line 9: Line 9:
 
Developers can integrate with it (by making a plugin) to add their own custom options to these menus.
 
Developers can integrate with it (by making a plugin) to add their own custom options to these menus.
  
== Creating an Action Menu Plugin ==
+
== How to use this library ==
 +
 
 +
Headers are available from [https://ghostbin.com/paste/xgh6e ActionMenu.h]. If using Theos, place the headers in <code>$THEOS/include/ActionMenu</code>.
  
=== Requirements ===
+
The project type should be a <tt>library</tt>.
  
* [[Theos]] installed
+
=== Include directive ===
* [https://ghostbin.com/paste/xgh6e ActionMenu.h]
 
* Knowledge of Objective-C
 
  
=== What is an Action Menu Plugin? ===
+
<source lang="objc">
 +
#import <ActionMenu/ActionMenu.h>
 +
</source>
  
We implement a category of the UIResponder class which will be loaded by Action Menu. Action Menu also provides us with easy methods to register our new UIMenuController item.
+
=== Makefile ===
  
=== The Makefile ===
+
Add to your Makefile:
  
The resulting file is a dynamic library (.dylib), so we will create a makefile for a library.
+
* <code>actionmenu</code> to the <code>XXX_LIBRARIES</code> variable.
 +
* <code>/Library/ActionMenu/Plugins</code> to the <code>XXX_INSTALL_PATH</code> variable.
 +
* <code>Foundation UIKit CoreGraphics</code> to the <code>XXX_FRAMEWORKS</code> variable.
  
<source lang=c>
+
=== Packaging ===
ARCHS = armv7 arm64
 
  
include theos/makefiles/common.mk
+
Add to your package's control file:
  
LIBRARY_NAME = AMExample
+
* <code>, mobilesubstrate (>= 0.9.5000), firmware (>= 3.0), preferenceloader (>= 2.2.2), cydia (>= 1.1.1), com.rpetrich.rocketbootstrap (>= 1.0.3) | firmware (<< 7.0)</code> to the <code>Depends</code> field.
AMExample_FILES = AMExample.m
 
AMExample_INSTALL_PATH = /Library/ActionMenu/Plugins
 
AMExample_FRAMEWORKS = Foundation UIKit CoreGraphics
 
  
include $(THEOS_MAKE_PATH)/library.mk
+
== Creating an Action Menu Plugin ==
</source>
 
  
We set the architecture to be armv7 and arm64 (64-bit). Then we include Theos' common.mk file, and after that we define the properties for our library project.
+
We implement a category of the UIResponder class which will be loaded by Action Menu. Action Menu also provides us with easy methods to register our new UIMenuController item.
  
 
=== AMExample.h ===
 
=== AMExample.h ===
Line 45: Line 44:
  
 
<source lang=objc>
 
<source lang=objc>
#import "ActionMenu.h" //import the Action Menu Header
 
 
 
@interface UIResponder (MyAwesomePlugin) //create our Category
 
@interface UIResponder (MyAwesomePlugin) //create our Category
 
- (BOOL)canPerformAction; //(we will check here if the selected text is longer than 0 characters)
 
- (BOOL)canPerformAction; //(we will check here if the selected text is longer than 0 characters)
Line 56: Line 53:
  
 
We are going to implement the methods and then our category in the .m implementation file.
 
We are going to implement the methods and then our category in the .m implementation file.
 
First we include our header file and write our @implementation section:
 
 
<source lang=objc>
 
#import "AMExample.h"
 
 
@implementation UIResponder (MyAwesomePlugin)
 
 
//our methods here
 
 
@end
 
</source>
 
  
 
We can use the NSObject's +(void)load method (which will be executed earlier than any other method) to initialize and create our UIMenuController item.
 
We can use the NSObject's +(void)load method (which will be executed earlier than any other method) to initialize and create our UIMenuController item.
Line 77: Line 62:
  
 
+ (void)load {
 
+ (void)load {
 +
/*
 +
This will call Action Menu's registerAction:title:canPerform method
 +
which is Action Menu's extension to UIMenuController
 +
*/
  
/*
+
[[UIMenuController sharedMenuController] registerAction:@selector(performMyAction)  
This will call Action Menu's registerAction:title:canPerform method
+
                                                  title:@"Example"  
which is Action Menu's extension to UIMenuController
+
                                            canPerform:@selector(canPerformAction)];
*/
 
 
 
[[UIMenuController sharedMenuController] registerAction:@selector(performMyAction)  
 
                                                  title:@"Example"  
 
                                                  canPerform:@selector(canPerformAction)];
 
 
 
 
}
 
}
  
Line 98: Line 81:
 
<source lang=objc>
 
<source lang=objc>
 
- (BOOL)canPerformAction {
 
- (BOOL)canPerformAction {
//returns TRUE if the selected text is longer than 0 characters
+
//returns TRUE if the selected text is longer than 0 characters
return [[self selectedTextualRepresentation] length] > 0;
+
return [[self selectedTextualRepresentation] length] > 0;
 
}
 
}
 
</source>
 
</source>
Line 106: Line 89:
  
 
<source lang=objc>
 
<source lang=objc>
 
 
- (void)performMyAction {
 
- (void)performMyAction {
 +
//get the selected text
 +
NSString *selection = [self selectedTextualRepresentation];
  
//get the selected text
+
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Selected Text"  
NSString *selection = [self selectedTextualRepresentation];
+
                                              message:selection delegate:nil  
 
+
                                    cancelButtonTitle:@"Okay" otherButtonTitles:nil];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Selected Text"  
+
[av show]; //shows the alert
                                                          message:selection delegate:nil  
+
[av release]; //releases the object to avoid memory leaks
                                                          cancelButtonTitle:@"Okay" otherButtonTitles:nil];
 
[av show]; //shows the alert
 
[av release]; //releases the object to avoid memory leaks
 
 
 
 
}
 
}
 
</source>
 
</source>
Line 126: Line 106:
  
 
== External links ==
 
== External links ==
+
 
 
* [http://rpetri.ch/cydia/actionmenu/ Package description]
 
* [http://rpetri.ch/cydia/actionmenu/ Package description]
 
* [https://ghostbin.com/paste/xgh6e ActionMenu.h]
 
* [https://ghostbin.com/paste/xgh6e ActionMenu.h]
+
 
 
{{Navbox Library}}
 
{{Navbox Library}}
+
 
 
[[Category:Directories in /Library]]
 
[[Category:Directories in /Library]]

Latest revision as of 22:52, 6 August 2016

ActionMenu
Cydia Package
Developer Ryan Petrich
Package ID actionmenu
Latest Version 1.2.14


ActionMenu is an extension that adds extra options to the menu that pops up when you select text or tap-and-hold an item (such as an image or a table cell).

Developers can integrate with it (by making a plugin) to add their own custom options to these menus.

How to use this library

Headers are available from ActionMenu.h. If using Theos, place the headers in $THEOS/include/ActionMenu.

The project type should be a library.

Include directive

#import <ActionMenu/ActionMenu.h>

Makefile

Add to your Makefile:

  • actionmenu to the XXX_LIBRARIES variable.
  • /Library/ActionMenu/Plugins to the XXX_INSTALL_PATH variable.
  • Foundation UIKit CoreGraphics to the XXX_FRAMEWORKS variable.

Packaging

Add to your package's control file:

  • , mobilesubstrate (>= 0.9.5000), firmware (>= 3.0), preferenceloader (>= 2.2.2), cydia (>= 1.1.1), com.rpetrich.rocketbootstrap (>= 1.0.3) | firmware (<< 7.0) to the Depends field.

Creating an Action Menu Plugin

We implement a category of the UIResponder class which will be loaded by Action Menu. Action Menu also provides us with easy methods to register our new UIMenuController item.

AMExample.h

We will first create our header file which will not have more than the category interface:

@interface UIResponder (MyAwesomePlugin) //create our Category
- (BOOL)canPerformAction; //(we will check here if the selected text is longer than 0 characters)
- (void)performMyAction; //this method will do the actual 'work' of our Plugin
@end

AMExample.m

We are going to implement the methods and then our category in the .m implementation file.

We can use the NSObject's +(void)load method (which will be executed earlier than any other method) to initialize and create our UIMenuController item.

#import "AMExample.h"

@implementation UIResponder (MyAwesomePlugin)

+ (void)load {
	/*
	This will call Action Menu's registerAction:title:canPerform method 
	which is Action Menu's extension to UIMenuController
	*/

	[[UIMenuController sharedMenuController] registerAction:@selector(performMyAction) 
	                                                  title:@"Example" 
	                                             canPerform:@selector(canPerformAction)];
}

@end

Now we have to implement the canPerformAction and performMyAction method. The canPerformAction checks for the length of the selected text and checks wether it is 0 or higher. It can only perform (return TRUE) if the selected text is longer than 0 characters.

Now we have to implement the canPerformAction and performMyAction method to complete this plugin:

- (BOOL)canPerformAction {
	//returns TRUE if the selected text is longer than 0 characters
	return [[self selectedTextualRepresentation] length] > 0;
}

And in our performMyAction method we show an UIAlertView:

- (void)performMyAction {
	//get the selected text
	NSString *selection = [self selectedTextualRepresentation];

	UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Selected Text" 
	                                              message:selection delegate:nil 
	                                    cancelButtonTitle:@"Okay" otherButtonTitles:nil];
	[av show]; //shows the alert
	[av release]; //releases the object to avoid memory leaks
}

To sum it up, +(void)load registers our Action Menu Item to be shown in the UIMenuController, - (BOOL)canPerformAction checks if there is any selected text, and - (void)performMyAction will run our code when the menu item is selected.

This is everything you need to know for your first Action Menu Plugin. All you need to do is change the actions that take place in the performMyAction method and you can customize your plugin.

External links