如何访问Private API 框架中的方法并将值传递给它?

How to access the method in the Private API framework and pass the value to it?

首先 - 我知道私人 frameworks/APIs 不会让我进入 AppStore,这仅适用于私人 use/research。

出于研究目的,我选择了 MFMessageComposer,我想禁用对从代码传递的任何输入的编辑。

我尝试着手解决这个问题,并按以下方式进行编码。我所做的是我采用了私有框架的路径并访问了一个名为 CKSMSComposeController 的特定 class,它具有上述方法。我提到了 ChatKit.framework https://github.com/nst/iOS-Runtime-Headers/blob/master/PrivateFrameworks/ChatKit.framework/CKSMSComposeController.h

class dump classes

我正在将 NSLog(@"Result %@", success ? @"YES" : @"NO"); 的日志作为 YES 获取,但即使在将 NO 传递给上面的选择器后我仍然无法禁用对收件人的编辑

有人能告诉我我传递参数的方式是否正确吗? 因为 -(void)setCanEditRecipients:(BOOL)arg1; ` 这是私有框架中的一个方法,它接受 bool 作为参数,我在上面的代码中传递了 NO

这只是为了私有框架的内部研究。我哪里做错了?请告诉

Class 方法以 + 开头,实例方法以 - 开头 Objective-C.

// Following is an instance method because it starts with `-`
- (void)setCanEditRecipients:(bool)arg1;

以上方法不适用于以下代码。

Class CKSMSComposeController = NSClassFromString(@"CKSMSComposeController");
SEL sel = NSSelectorFromString(@"setCanEditRecipients:");

// `CKSMSComposeController` is a class - NOT an instance
if ([CKSMSComposeController respondsToSelector:sel]) {
    // will not enter if's body
}

最重要的是 - 您不应该创建自己的实例并对其进行自定义。您应该对系统在屏幕上显示的实例进行自定义。

这是您可以尝试的方法 -

- (void) showMessageComposeViewController {
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController* messageController = [[MFMessageComposeViewController alloc] init];
        messageController.recipients = @[@"555-555-5555"];
        messageController.body = @"Example message";
        
        [self presentViewController:messageController animated:YES completion:^{
            
            // Allow enough time for the UI to be loaded fully
            dispatch_after(1, dispatch_get_main_queue(), ^{
                // Since `MFMessageComposeViewController` is a `UINavigationController`, we can access it's first view controller like this
                UIViewController* targetVC = messageController.viewControllers.firstObject;
                
                // Check if the instance is of correct class type
                if ([targetVC isKindOfClass:NSClassFromString(@"CKSMSComposeController")]) {
                    
                    SEL sel1 = NSSelectorFromString(@"setCanEditRecipients:");
                    if ([targetVC respondsToSelector:sel1]) {
                        // put breakpoint here to check whether this line is executed
                        [targetVC performSelector:sel1 withObject:@NO];
                    }
                    
                    SEL sel2 = NSSelectorFromString(@"setTextEntryContentsVisible:");
                    if ([targetVC respondsToSelector:sel2]) {
                        // put breakpoint here to check whether this line is executed
                        [targetVC performSelector:sel2 withObject:@NO];
                    }
                }
            });
        }];
    }
}