关于react native项目中使用[email protected]

关于react native项目中使用[email protected]

    • 插件官网
    • 安装依赖包(Android和iOS下载插件完成后记得更新依赖,)
    • Android中配置
      • 1.在项目文件夹下面创建文件夹wxapi(如上图)
      • 2.在文件MainApplication.java中如下配置
      • 3.在文件AndroidManifest.xml中添加如下配置
    • IOS配置
      • 1.在AppDelegate.h文件添加如下代码:
      • 2.在AppDelegate.mm文件添加如下代码:
      • 3.Info.plist文件
    • 在login.jsx页面中使用
    • 备注:文档官方文档更加详细

插件官网

https://www.npmjs.com/package/react-native-wechat-lib/v/3.0.4

安装依赖包(Android和iOS下载插件完成后记得更新依赖,)

npm i [email protected]

Android中配置

关于react native项目中使用react-native-wechat-lib@3.0.4_第1张图片

1.在项目文件夹下面创建文件夹wxapi(如上图)

wxapi/WXEntryActivity.java

package 包名.wxapi;

// react-native-wechat-lib support (
import android.app.Activity;
import android.os.Bundle;
import com.wechatlib.WeChatLibModule;

public class WXEntryActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WeChatLibModule.handleIntent(getIntent());
    finish();
  }
}
// )

2.在文件MainApplication.java中如下配置

package 包名;

import android.app.Application;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint;
import com.facebook.react.defaults.DefaultReactNativeHost;
import com.facebook.soloader.SoLoader;
import java.util.List;
// react-native-wechat-lib support (
import com.wechatlib.WeChatLibPackage;
// )

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost =
      new DefaultReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }

        @Override
        protected List getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
          // react-native-wechat-lib support (
          // Packages that cannot be autolinked yet can be added manually here, for example:
          packages.add(new WeChatLibPackage());
          // )
          return packages;
        }

        @Override
        protected String getJSMainModuleName() {
          return "index";
        }

        @Override
        protected boolean isNewArchEnabled() {
          return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
        }

        @Override
        protected Boolean isHermesEnabled() {
          return BuildConfig.IS_HERMES_ENABLED;
        }
      };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
      // If you opted-in for the New Architecture, we load the native entry point for this app.
      DefaultNewArchitectureEntryPoint.load();
    }
    ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  }
}

3.在文件AndroidManifest.xml中添加如下配置



    
    
    
    
    
    
    
    
    
    
    
    
    

    
      
        
            
            
        

        
            
            
            

            

        

      
      
      
      
    
    
    
      
    
    


IOS配置

关于react native项目中使用react-native-wechat-lib@3.0.4_第2张图片
到微信下载最新sdk,放在上图位置
在Xcode进行环境配置:如图
关于react native项目中使用react-native-wechat-lib@3.0.4_第3张图片
关于react native项目中使用react-native-wechat-lib@3.0.4_第4张图片
关于react native项目中使用react-native-wechat-lib@3.0.4_第5张图片
关于react native项目中使用react-native-wechat-lib@3.0.4_第6张图片

1.在AppDelegate.h文件添加如下代码:

#import 
#import 
#import "WXApi.h"

@interface AppDelegate : RCTAppDelegate

@end

2.在AppDelegate.mm文件添加如下代码:

#import "AppDelegate.h"

#import 
#import 

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.moduleName = @"项目名称";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.
  self.initialProps = @{};

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}
//ali
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [RCTLinkingManager application:application openURL:url
                      sourceApplication:sourceApplication annotation:annotation];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}
//weixin
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application
  continueUserActivity:(NSUserActivity *)userActivity
  restorationHandler:(void(^)(NSArray> * __nullable
  restorableObjects))restorationHandler {
  // 触发回调方法
  [RCTLinkingManager application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
  return [WXApi handleOpenUniversalLink:userActivity
  delegate:self];
}

@end

3.Info.plist文件

CFBundleURLTypes
	
		
			CFBundleTypeRole
			Editor
			CFBundleURLName
			alipay
			CFBundleURLSchemes
			
				alipayxxxxx
			
		
		
			CFBundleTypeRole
			Editor
			CFBundleURLName
			weixin
			CFBundleURLSchemes
			
				wxxxxxx
			
		
	
LSApplicationQueriesSchemes
	
		iosamap
		baidumap
		qqmap
		alipay
		weixin
		wechat
		weixinULAPI
		weixinURLParamsAPI
	

在login.jsx页面中使用

import * as WeChat from 'react-native-wechat-lib';

//微信授权登录
      WeChat.sendAuthRequest('snsapi_userinfo', 'wechat_sdk_demo')
        .then(response => {
          console.warn(response); // 处理授权登录后的结果
         
        })
        .catch(error => {
          console.warn(error); // 处理授权登录后的结果
        });

useEffect(() => {
    console.log('[登录]');
    WeChat.registerApp(
      'wxxxxx',
      'https://xxxxx.com/',
    );
    return () => {
      
    };
  }, []);

备注:文档官方文档更加详细

你可能感兴趣的:(react-native,React,react,native,微信,react.js)