uni-app ios原生组件插件 跳转原生页面,有什么办法吗

uni-app ios原生组件插件 跳转原生页面,有什么办法吗

代码示例

// 获取当前显示的 UIViewController    
+ (UIViewController *)dc_findCurrentShowingViewController {    
    //获得当前活动窗口的根视图    
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;    
    UIViewController *currentShowingVC = [self findCurrentShowingViewControllerFrom:vc];    
    return currentShowingVC;    
}    
+ (UIViewController *)findCurrentShowingViewControllerFrom:(UIViewController *)vc    
{    
    // 递归方法 Recursive method    
    UIViewController *currentShowingVC;    
    if ([vc presentedViewController]) {    
        // 当前视图是被presented出来的    
        UIViewController *nextRootVC = [vc presentedViewController];    
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];    

    } else if ([vc isKindOfClass:[UITabBarController class]]) {    
        // 根视图为UITabBarController    
        UIViewController *nextRootVC = [(UITabBarController *)vc selectedViewController];    
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];    

    } else if ([vc isKindOfClass:[UINavigationController class]]){    
        // 根视图为UINavigationController    
        UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController];    
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];    

    } else {    
        // 根视图为非导航类    
        currentShowingVC = vc;    
    }    

    return currentShowingVC;    
}

目前使用这个跳转原生页面,效果不是很好,有什么好的方法吗


更多关于uni-app ios原生组件插件 跳转原生页面,有什么办法吗的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app ios原生组件插件 跳转原生页面,有什么办法吗的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app中实现iOS原生组件插件跳转原生页面,通常需要使用到uni-app提供的原生模块扩展功能,通过自定义原生插件来实现页面跳转。以下是一个基本的示例,展示如何在uni-app中通过iOS原生组件插件跳转到原生页面。

步骤一:创建iOS原生插件

首先,需要在iOS项目中创建一个原生插件。在Xcode中,添加一个Objective-C或Swift类,用于实现原生页面跳转逻辑。

// MyNativePlugin.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MyNativePlugin : NSObject

+ (void)openNativePage:(UIViewController *)currentViewController;

@end

// MyNativePlugin.m
#import "MyNativePlugin.h"
#import "NativeViewController.h" // 假设这是你要跳转到的原生页面

@implementation MyNativePlugin

+ (void)openNativePage:(UIViewController *)currentViewController {
    NativeViewController *vc = [[NativeViewController alloc] init];
    [currentViewController presentViewController:vc animated:YES completion:nil];
}

@end

步骤二:在uni-app中调用原生插件

在uni-app中,通过plus.bridge.exec方法调用iOS原生插件的方法。首先,确保在manifest.json中配置了原生插件。

// manifest.json 中的 "app-plus" -> "distribute" -> "plugins" 配置
{
    "plugins": {
        "MyNativePlugin": {
            "path": "path/to/your/ios/plugin", // 插件路径
            "www": {
                "default": "path/to/your/ios/plugin/MyNativePlugin.js" // 插件的JS接口文件
            }
        }
    }
}

MyNativePlugin.js中,可以暴露一个接口供uni-app调用:

// MyNativePlugin.js
export default {
    openNativePage: function (success, fail) {
        plus.bridge.exec('MyNativePlugin', 'openNativePage', [], success, fail);
    }
};

步骤三:在uni-app页面调用原生插件方法

最后,在uni-app的页面中调用这个原生插件方法:

// 在uni-app的某个页面中
const MyNativePlugin = require('path/to/your/ios/plugin/MyNativePlugin.js');

MyNativePlugin.openNativePage(function () {
    console.log('成功跳转到原生页面');
}, function (e) {
    console.error('跳转到原生页面失败', e);
});

以上代码展示了如何在uni-app中通过iOS原生组件插件跳转到原生页面的基本流程。注意,实际开发中可能需要根据具体需求调整代码,比如处理页面传参、生命周期管理等。

回到顶部