简体   繁体   English

在 iOS 13 中将静态委托设置为 UIApplication (objective-c)

[英]Set static delegate to UIApplication in iOS 13 (objective-c)

I'm don't know objective-c very well and i am trying to define an app delegate to use it in other classes.我不太了解objective-c,我正在尝试定义一个应用程序委托以在其他类中使用它。

after searching and according to some examples i found that i have to create an static property to save delegate in that:经过搜索并根据一些示例,我发现我必须创建一个静态属性来保存委托:

.h:
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
+(AppDelegate *)sharedAppDelegate;


.m:
static AppDelegate *appDelegate = nil;
+(AppDelegate *)sharedAppDelegate{
    static dispatch_once_t pred;
    static AppDelegate *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[super alloc] init];
    });
    return shared;
}

and then set delegate to that static property (sharedAppDelegate):然后将委托设置为该静态属性 (sharedAppDelegate):

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[UIApplication sharedApplication] setDelegate:[AppDelegate sharedAppDelegate]];

i used this code on ios 11 and 12 and worked , But in ios 13 (xcode 11.3.1) when i set delegate app screen becomes black and xcode shows these errors:我在 ios 11 和 12 上使用了这段代码并且工作了,但是在 ios 13(xcode 11.3.1)中,当我设置委托应用程序屏幕变黑并且 xcode 显示这些错误时:

Thread 1: signal SIGABRT


libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

What's the problem?有什么问题?

finally i want to open ios popup page to share a text to other apps like telegram or whatsapp or ... i need that delegate to use that on other classes that i call share functions:最后,我想打开 ios 弹出页面以将文本共享给其他应用程序,例如电报或 whatsapp 或...我需要该委托才能在我称为共享函数的其他类上使用它:

void IOS::shareText(const QString &text) {

NSMutableArray *sharingItems = [NSMutableArray new];

    if (!text.isEmpty()) {
        [sharingItems addObject:text.toNSString()];
    }

    // get the main window rootViewController
    UIViewController *self = [[UIApplication sharedApplication].keyWindow rootViewController];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
    if ( [activityController respondsToSelector:@selector(popoverPresentationController)] ) { // iOS8
          activityController.popoverPresentationController.sourceView = self.view;
    }
    [self presentViewController:activityController animated:YES completion:nil];

}

i`m using mixed objective-c with c++ in qt我在qt中使用混合objective-c和c++

I'm don't know objective-c very well and i am trying to define an app delegate to use it in other classes.我不太了解objective-c,我正在尝试定义一个应用程序委托以在其他类中使用它。

after searching and according to some examples i found that i have to create an static property to save delegate in that:经过搜索并根据一些示例,我发现我必须创建一个静态属性来保存委托:

.h:
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
+(AppDelegate *)sharedAppDelegate;


.m:
static AppDelegate *appDelegate = nil;
+(AppDelegate *)sharedAppDelegate{
    static dispatch_once_t pred;
    static AppDelegate *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[super alloc] init];
    });
    return shared;
}

and then set delegate to that static property (sharedAppDelegate):然后将委托设置为该静态属性 (sharedAppDelegate):

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[UIApplication sharedApplication] setDelegate:[AppDelegate sharedAppDelegate]];

i used this code on ios 11 and 12 and worked , But in ios 13 (xcode 11.3.1) when i set delegate app screen becomes black and xcode shows these errors:我在 ios 11 和 12 上使用了这段代码并且工作了,但是在 ios 13(xcode 11.3.1)中,当我设置委托应用程序屏幕变黑并且 xcode 显示这些错误时:

Thread 1: signal SIGABRT


libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

What's the problem?有什么问题?

finally i want to open ios popup page to share a text to other apps like telegram or whatsapp or ... i need that delegate to use that on other classes that i call share functions:最后,我想打开 ios 弹出页面以将文本共享给其他应用程序,例如电报或 whatsapp 或...我需要该委托才能在我称为共享函数的其他类上使用它:

void IOS::shareText(const QString &text) {

NSMutableArray *sharingItems = [NSMutableArray new];

    if (!text.isEmpty()) {
        [sharingItems addObject:text.toNSString()];
    }

    // get the main window rootViewController
    UIViewController *self = [[UIApplication sharedApplication].keyWindow rootViewController];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
    if ( [activityController respondsToSelector:@selector(popoverPresentationController)] ) { // iOS8
          activityController.popoverPresentationController.sourceView = self.view;
    }
    [self presentViewController:activityController animated:YES completion:nil];

}

i`m using mixed objective-c with c++ in qt我在qt中使用混合objective-c和c++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM