Giter Club home page Giter Club logo

react-native-weibo's Introduction

rn-weibo

React Native的新浪微博登录分享插件

本仓库fork自reactnativecn/react-native-weibo,但源仓库许久没维护了,故本人针对react-native和weiboSDK新版做了适配后npm发布了新包rn-weibo

版本要求

  • 适配react-native版本0.57.5
  • weiboSDK的版本分别为ios 3.2.3和安卓端4.3.1

如何安装

1.首先安装npm包

npm install rn-weibo --save

2.link

自动link方法

react-native link

手动link~(如果不能够自动link)

#####ios a.打开XCode's工程中, 右键点击Libraries文件夹 ➜ Add Files to <...> b.去node_modules ➜ react-native-weibo ➜ ios ➜ 选择 RCTWeiboAPI.xcodeproj c.在工程Build Phases ➜ Link Binary With Libraries中添加libRCTWeiboAPI.a

#####Android

// file: android/settings.gradle
...

include ':react-native-weibo'
project(':react-native-weibo').projectDir = new File(settingsDir, '../node_modules/react-native-weibo/android')
// file: android/app/build.gradle
...

dependencies {
    ...
    compile project(':react-native-weibo')
}

android/app/src/main/java/<你的包名>/MainApplication.java中添加如下两行:

...
import cn.reactnative.modules.weibo.WeiboPackage;  // 在public class MainApplication之前import

public class MainApplication extends Application implements ReactApplication {

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

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new WeiboPackage(), // 然后添加这一行
          new MainReactPackage()
      );
    }
  };

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

3.工程配置

ios配置

node_modules/react-native-weibo/ios/libWeiboSDK/WeiboSDK.bundle加入到工程中(必须,很重要,不然登录的时候会crash)

在工程target的Build Phases->Link Binary with Libraries中加入libRCTWeiboAPI.a、libsqlite3.tbd、libz.tbd、ImageIO.framework、SystemConfiguration.framework、Security.framework、CoreTelephony.framework、CoreText.framework

Info->URL Types 中增加QQ的scheme: Identifier 设置为sina, URL Schemes 设置为你注册的微博开发者账号中的APPID,需要加前缀wb,例如wb1915346979

在你工程的AppDelegate.m文件中添加如下代码:

#import "../Libraries/LinkingIOS/RCTLinkingManager.h"


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

iOS9的适配问题

由于iOS9的发布影响了微博SDK与应用的集成方式,为了确保好的应用体验,我们需要采取如下措施:

a.对传输安全的支持

在iOS9系统中,默认需要为每次网络传输建立SSL。解决这个问题:

  • 将NSAllowsArbitraryLoads属性设置为YES,并添加到你应用的plist中
  • NSAppTransportSecurity NSAllowsArbitraryLoads
b.对应用跳转的支持

如果你需要用到微博的相关功能,如登陆,分享等。并且需要实现跳转到微博的功能,在iOS9系统中就需要在你的app的plist中添加下列键值对。否则在canOpenURL函数执行时,就会返回NO。了解详情请至https://developer.apple.com/videos/wwdc/2015/?id=703

  • LSApplicationQueriesSchemes sinaweibohd sinaweibo weibosdk weibosdk2.5

Android

android/app/build.gradle里,defaultConfig栏目下添加如下代码:

manifestPlaceholders = [
    WB_APPID: "微博的APPID"		//在此修改微博APPID
]

如果react-native版本<0.18.0,确保你的MainActivity.java中有onActivityResult的实现:

private ReactInstanceManager mReactInstanceManager;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
}

如何使用

引入包

import * as WeiboAPI from 'react-native-weibo';

API

WeiboAPI.login(config)

// 登录参数 
config : {	
	scope: 权限设置, // 默认 'all'
	redirectURI: 重定向地址, // 默认 'https://api.weibo.com/oauth2/default.html'(必须和sina微博开放平台中应用高级设置中的redirectURI设置的一致,不然会登录失败)
}

返回一个Promise对象。成功时的回调为一个类似这样的对象:

{
	"accessToken": "2.005e3HMBzh7eFCca6a3854060GQFJf",
	"userID": "1098604232"
	"expirationDate": "1452884401084.538"	
	"refreshToken": "2.005e3HMBzh8eFC3db19a18bb00pvbp"
}

WeiboAPI.share(data)

分享到微博

// 分享文字
{	
	type: 'text', 
	text: 文字内容,
}
// 分享图片
{	
	type: 'image',
	text: 文字内容,	
	imageUrl: 图片地址	
}

react-native-weibo's People

Contributors

anijia avatar codeherecn avatar happyming9527 avatar lvbingru avatar songyouwei avatar sunnylqm avatar tdzl2003 avatar yun77op avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.