NSInvocation多个参数在iOS 13.0 beta中的Action Extension中不起作用

时间:2019-09-11 12:37:03

标签: objective-c ios13 nsinvocation

我将这段代码用于开放设置,并添加了我的App Action Extension。它可以在iOS 12.0中使用,但在iOS 13.0中会崩溃。

NSURL *shareURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//  SEL selector = NSSelectorFromString(@"openURL:options:completionHandler:");
//  SEL selector = @selector(openURL:);
SEL selector = @selector(openURL:options:completionHandler:);  

UIResponder* responder = self;
while ((responder = [responder nextResponder]) != nil) {
    if([responder respondsToSelector:selector] == true) {
        NSMethodSignature *methodSignature = [responder methodSignatureForSelector:selector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];

        NSDictionary<NSString *, id> *options = [NSDictionary dictionary];
        //   NSDictionary *options = @{UIApplicationOpenURLOptionUniversalLinksOnly: @NO};
        void (^completion)(BOOL success) = ^void(BOOL success) {
            NSLog(@"Completions block: %i", success);
        };

        [invocation setTarget: responder];
        [invocation setSelector: selector];
        [invocation setArgument: &shareURL atIndex: 2];
        [invocation setArgument: &options atIndex:3];
        [invocation setArgument: &completion atIndex: 4];

        [invocation retainArguments];

        //  [invocation invoke];
        //  [invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES];
        [invocation performSelector:@selector(invoke) withObject:nil afterDelay:1.0f];
        break;
    }
}

我崩溃了:

-[__NSDictionary0 universalLinksOnly]: unrecognized selector sent to instance 0x7fff805f8120
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionary0 universalLinksOnly]: unrecognized selector sent to instance 0x7fff805f8120'


  *** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23b98bde __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff503b5b20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23bb9704 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff23b9d7bc ___forwarding___ + 1436
    4   CoreFoundation                      0x00007fff23b9f6c8 _CF_forwarding_prep_0 + 120
    5   UIKitCore                           0x00007fff46711366 -[UIScene openURL:options:completionHandler:] + 124
    6   CoreFoundation                      0x00007fff23b9f95c __invoking___ + 140
    7   CoreFoundation                      0x00007fff23b9cd8f -[NSInvocation invoke] + 287
    8   Foundation                          0x00007fff2566e1d6 __NSFireDelayedPerform + 420
    9   CoreFoundation                      0x00007fff23afc1e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    10  CoreFoundation                      0x00007fff23afbed2 __CFRunLoopDoTimer + 1026
    11  CoreFoundation                      0x00007fff23afb52a __CFRunLoopDoTimers + 266
    12  CoreFoundation                      0x00007fff23af629e __CFRunLoopRun + 2238
    13  CoreFoundation                      0x00007fff23af56b6 CFRunLoopRunSpecific + 438
    14  GraphicsServices                    0x00007fff3815cbb0 GSEventRunModal + 65
    15  UIKitCore                           0x00007fff47162a67 UIApplicationMain + 1621
    16  App                                 0x0000000107ee8290 main + 112
    17  libdyld.dylib                       0x00007fff5123bcf5 start + 1
)

如果我们仅使用SEL selector = @selector(openURL:);,则它可以工作,但不能与SEL selector = @selector(openURL:options:completionHandler:);这样的多个参数一起使用

0 个答案:

没有答案