7.4 编写tweak

本章的例子比较简单,所有的操作都可以在NotesDisplayController一个类中完成。

7.4.1 用Theos新建tweak工程“CharacountForNotes8”

新建CharacountForNotes8工程的命令如下:


snakeninnys-MacBook:Code snakeninny$ /opt/theos/bin/nic.pl
NIC 2.0 - New Instance Creator
------------------------------
  [1.] iphone/application
  [2.] iphone/cydget
  [3.] iphone/framework
  [4.] iphone/library
  [5.] iphone/notification_center_widget
  [6.] iphone/preference_bundle
  [7.] iphone/sbsettingstoggle
  [8.] iphone/tool
  [9.] iphone/tweak
  [10.] iphone/xpc_service
Choose a Template (required): 9
Project Name (required): CharacountForNotes8
Package Name [com.yourcompany.characountfornotes8]: com.naken.characountfornotes8
Author/Maintainer Name [snakeninny]: snakeninny
[iphone/tweak] MobileSubstrate Bundle filter [com.apple.springboard]: com.apple.mobilenotes          
[iphone/tweak] List of applications to terminate upon installation (space-separated, '-' for none) [SpringBoard]: MobileNotes 
Instantiating iphone/tweak in characountfornotes8/...
Done.

7.4.2 构造CharacountForNotes8.h

编辑后的CharacountForNotes8.h内容如下:


@interface NoteObject : NSObject
@property (readonly, nonatomic) NSString *contentAsPlainText;
@end
@interface NoteTextView : UIView
@property (copy, nonatomic) NSString *text;
@end
@interface NoteContentLayer : UIView
@property (retain, nonatomic) NoteTextView *textView;
@end
@interface NotesDisplayController : UIViewController
@property (retain, nonatomic) NoteContentLayer *contentLayer;
@property (retain, nonatomic) NoteObject *note;
@end

这个头文件的所有内容均摘自类对应的头文件,构造它的目的仅仅是通过编译,避免任何报错和警告。

7.4.3 编辑Tweak.xm

编辑后的Tweak.xm内容如下:


#import "CharacountForNotes8.h"
%hook NotesDisplayController
- (void)viewWillAppear:(BOOL)arg1 // Initialze title
{
      %orig;
      NSString *content = self.note.contentAsPlainText;
      NSString *contentLength = [NSString 
stringWithFormat:@"%lu", (unsigned long)[content length]];
      self.title = contentLength;
}
- (void)viewDidDisappear:(BOOL)arg1 // Reset title
{
      %orig;
      self.title = nil;
}
- (void)noteContentLayerContentDidChange:(NoteContentLayer 
*)arg1 updatedTitle:(BOOL)arg2 // Update title
{
      %orig;
      NSString *content = self.contentLayer.textView.text;
      NSString *contentLength = [NSString 
stringWithFormat:@"%lu", (unsigned long)[content length]];
      self.title = contentLength;
}
%end

7.4.4 编辑Makefile及control

编辑后的Makefile内容如下:


THEOS_DEVICE_IP = iOSIP
ARCHS = armv7 arm64
TARGET = iphone:latest:8.0
include theos/makefiles/common.mk
TWEAK_NAME = CharacountForNotes8
CharacountForNotes8_FILES = Tweak.xm
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
      install.exec "killall -9 MobileNotes"

编辑后的control内容如下:


Package: com.naken.characountfornotes8
Name: CharacountForNotes8
Depends: mobilesubstrate, firmware (>= 8.0)
Version: 1.0
Architecture: iphoneos-arm
Description: Add a character count to Notes
Maintainer: snakeninny
Author: snakeninny
Section: Tweaks
Homepage: http://bbs.iosre.com

7.4.5 测试

将写好的tweak编译打包安装到iOS后,打开Notes随便编辑一条note,查看标题的字数变化是否可做到完全实时,如图7-11至图7-17所示。

图7-11 Characount for Notes 8效果演示(1)

图7-12 Characount for Notes 8效果演示(2)

图7-13 Characount for Notes 8效果演示(3)

图7-14 Characount for Notes 8效果演示(4)

图7-15 Characount for Notes 8效果演示(5)

图7-16 Characount for Notes 8效果演示(6)

图7-17 Characount for Notes 8效果演示(7)

插件的表现与预期完全一致。