respondsToSelector的相关使用

  • -(BOOL) isKindOfClass: classObj 用来判断是否是某个类或其子类的实例
  • -(BOOL) isMemberOfClass: classObj 用来判断是否是某个类的实例
  • -(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
  • +(BOOL) instancesRespondToSelector: selector 用来判断实例是否有以某个名字命名的方法. 和上面一个不同之处在于, 前面这个方法可以用在实例和类上,而此方法只能用在类上.
  • -(id) performSelector: selector

     

    SEL sel = @selector (start:) ; // 指定action  

    if ([obj respondsToSelector:sel]) 

    { //判断该对象是否有相应的方法  

    [obj performSelector:sel withObject:self]; //调用选择器方法  

    使用[[UIApplication sharedApplication] keyWindow]查找应用程序的主窗口对象


    respondsToSelector判断是否实现了某方法

    Tester.h

    复制代码
          
    #import < Foundation / Foundation.h >


    @interface Tester : NSObject {

    }

    - ( void ) test:(NSString * ) msg;

    - ( void ) notImp;

    @end
    复制代码

    Tester.m
    复制代码
          
    #import " Tester.h "

    @implementation Tester

    - ( void ) test:(NSString * ) msg
    {
    NSLog(
    @" %@ " , msg);
    }

    @end
    复制代码
    注意:没有实现notImp方法

    main.m

    复制代码
          
    #import < Foundation / Foundation.h >
    #import
    " Tester.h "

    int main ( int argc, const char * argv[])
    {

    NSAutoreleasePool
    * pool = [[NSAutoreleasePool alloc] init];

    id tester
    = [[Tester alloc] init]; // 注意,这里使用id

    SEL testSelector
    = @selector(test:);
    SEL notImpSelector
    = @selector(notImp:);

    if ([tester respondsToSelector:testSelector])
    {
    // tester.m中实现了test方法
    [tester test: @" invoke test method " ];
    }
    if ([tester respondsToSelector:notImpSelector])
    {
    // test.m中没有实现此主就去
    [tester notImp];
    }


    [pool drain];
    return 0 ;
    }
    复制代码
### 集成极光推送 JPush 的基本步骤 #### Android 端集成步骤 1. **添加依赖**:在 `pubspec.yaml` 文件中添加 Flutter 极光推送插件的依赖,例如: ```yaml dependencies: jpush_flutter: ^0.0.8 ``` 安装完成后,运行 `flutter pub get` 来获取插件。 2. **配置 AppKey 和 Channel**:在 AndroidManifest.xml 文件中通过 `manifestPlaceholders` 设置默认的 `JPUSH_CHANNEL` 和 `JPUSH_APPKEY`: ```xml <meta-data android:name="JPUSH_CHANNEL" android:value="${JPUSH_CHANNEL_VALUE}"/> <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY_VALUE}"/> ``` 此外,在代码中也可以动态设置 channel 和 appKey: ```dart JPush jpush = new JPush(); jpush.setup( appKey: "替换成你自己的 appKey", channel: "theChannel", production: false, debug: false, ); ``` 3. **初始化 SDK**:在应用启动时调用初始化方法以激活 JPush 服务: ```dart jpush.init(); ``` 4. **事件监听**:注册事件监听器来处理接收到的通知和自定义消息: ```dart jpush.addEventHandler( onReceiveNotification: (Map<String, dynamic> map) async { print("Received notification: $map"); }, onOpenNotification: (Map<String, dynamic> map) async { print("Opened notification: $map"); }, onReceiveMessage: (Map<String, dynamic> map) async { print("Received message: $map"); }, ); ``` 5. **申请权限(可选)**:对于 Android 6.0 及以上版本,需要动态申请通知权限。可以使用 `permission_handler` 插件进行管理。 6. **调试模式与生产环境切换**:根据当前的应用环境,将 `production` 参数设为 `true` 或 `false` 以切换生产与测试模式 [^1]。 #### iOS 端集成步骤 1. **手动导入 SDK**:解压下载的极光推送 SDK 包,并将 lib 文件夹中的文件(包含 JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a)通过 Xcode 添加到你的工程目录中 [^2]。 2. **添加必要的 Frameworks**:确保项目中已添加以下系统库: - UserNotifications.framework - CoreFoundation.framework - SystemConfiguration.framework - Security.framework - CFNetwork.framework - CoreTelephony.framework 3. **配置 Build Settings**:在 Xcode 中打开项目的 `Build Settings`,找到 `Other Linker Flags` 并添加 `-ObjC`。 4. **初始化 SDK**:在 AppDelegate.m 文件中引入头文件并初始化 SDK: ```objective-c #import "JPUSHService.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 初始化极光推送 [JPUSHService setupWithOption:launchOptions appKey:@"your_appkey" channel:@"theChannel" apsForProduction:NO]; return YES; } ``` 5. **实现通知回调**:在 AppDelegate 中实现相关方法以接收通知内容: ```objective-c - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [[JPUSHService defaultCenter] handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } ``` 6. **申请用户授权**:iOS 10 及以上版本需要主动请求用户授权通知权限: ```objective-c if ([[UNUserNotificationCenter currentNotificationCenter] respondsToSelector:@selector(requestAuthorizationWithOptions:completionHandler:)]) { [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; }); } }]; } ``` 7. **处理通知点击**:实现 `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:` 方法以响应通知点击事件。 #### 调试与发布 - 在开发阶段建议关闭生产模式 (`production: false`),以便更好地调试推送功能。 - 发布正式版本前,请务必开启生产模式并移除调试日志输出。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值