Giter Site home page Giter Site logo

dragonlong206 / rn-zalo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from phithu/rn-zalo

0.0 1.0 0.0 21.76 MB

A React Native Module for Zalo SDK

Home Page: https://www.npmjs.com/package/rn-zalo

Java 44.03% JavaScript 14.76% Objective-C 29.37% Ruby 3.28% Python 8.57%

rn-zalo's Introduction

React Native Zalo

$ npm install rn-zalo --save

Zalo SDK Documents

Mostly automatic installation

$ react-native link rn-zalo

Limitation

  • Only working on real device

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesrn-zalo and add RNZalo.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNZalo.a to your project's Build PhasesLink Binary With Libraries
  4. Create a Podfile in ios folder (If you don't have Podfile file in ios folder)
  • cd ios && touch Podfile
  • Copy and paste content bellow to Podfile, then pod install
workspace '<PROJECT_NAME>'
project '<PROJECT_NAME>'
project '../node_modules/rn-zalo/ios/RNZalo'
target '<PROJECT_NAME>' do
    project '<PROJECT_NAME>'
    pod 'ZaloSDK'
end
target 'RNZalo' do
    project '../node_modules/rn-zalo/ios/RNZalo'
    pod 'ZaloSDK'
end
  1. Add URL Type Main target setting -> info -> URL types -> click +

identifier = “zalo”, URL Schemes = “zalo-<YOUR_APP_ID>”

  1. Open AppDelegate.m
...
#import <ZaloSDK/ZaloSDK.h>
- (BOOL)application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    [[ZaloSDK sharedInstance] initializeWithAppId:@"<YOUR_APP_ID>"];
    return YES;
}
  
- (BOOL)application:(UIApplication *)application 
    openURL:(NSURL *)url
    sourceApplication:(NSString *)sourceApplication
    annotation:(id)annotation {
 
    return [[ZDKApplicationDelegate sharedInstance] 
    application:application
    openURL:url sourceApplication:sourceApplication annotation:annotation];
}
  1. Clear and Run your project

Android

  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import rnzalo.RNZaloPackage; to the imports at the top of the file
  • Add import com.zing.zalo.zalosdk.oauth.ZaloSDKApplication; to the imports
  • Add new RNZaloPackage() to the list returned by the getPackages() method
  • Add ZaloSDKApplication.wrap(this) on "onCreate" function
  1. Open up android/app/src/main/java/[...]/MainActivity.java
... 
import android.content.Intent;
import com.zing.zalo.zalosdk.oauth.ZaloSDK;
import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {
   ...
    
   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       ZaloSDK.Instance.onActivityResult(this, requestCode, resultCode, data);
   }
}

  1. Append the following lines to android/settings.gradle:
    include ':rn-zalo'
    project(':rn-zalo').projectDir = new File(rootProject.projectDir, 	'../node_modules/rn-zalo/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
    ...
    implementation "com.zing.zalo.zalosdk:core:2.4.2501"
    implementation "com.zing.zalo.zalosdk:auth:2.4.2501"
    implementation project(':rn-zalo')
    
  3. Add appId to android/app/src/main/res/values/strings.xml
<resources>
    <string name="app_name">App Name</string>
    <string name="appID"><YOUR_APP_ID></string>
</res>
  1. Add code bellow to android/app/src/main/res/AndroidManifest.xml
 <application
        ...
        <meta-data
            android:name="com.zing.zalo.zalosdk.appID"
            android:value="@string/appID" />

        <activity
            android:name="com.zing.zalo.zalosdk.oauth.WebLoginActivity"
            android:configChanges="orientation|screenSize"
            android:screenOrientation="sensor"
            android:theme="@style/FixThemeForLoginWebview"
            android:windowSoftInputMode="stateHidden|stateAlwaysHidden"></activity>
    </application>

Usage

import {
  StyleSheet,
  View,
  TouchableOpacity,
  Image,
  Text,
} from 'react-native';
import React from 'react';
import RNZalo from 'rn-zalo';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: null,
    };
  }

  login = async () => {
    try {
      const data = await RNZalo.login();
      this.setState({ data });
    } catch (e) {
      console.log('e', e);
    }

  };

  logout = () => {
    RNZalo.logout();
  };

  renderUser() {
    if (!this.state.data) {
      return null;
    }
    const { birthday, gender, id, picture, name } = this.state.data.user;
    return (
      <View style={styles.userInfoContainer}>
        <Text style={{ fontSize: 18, fontWeight: 'bold' }}>Name: {name}</Text>
        <Text>Id: {id}</Text>
        <Text>Birthday: {birthday}</Text>
        <Text>Gender: {gender}</Text>
        <Image
          style={{ width: 100, height: 100, borderRadius: 50, marginTop: 20 }}
          source={{ uri: picture.data.url }}
        />
      </View>
    );
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={{ marginTop: 30 }}>
          <TouchableOpacity style={styles.buttonStyle} onPress={this.login}>
            <Text style={{ color: '#fff', fontSize: 18 }}>Login</Text>
          </TouchableOpacity>

          <TouchableOpacity style={styles.buttonStyle} onPress={this.logout}>
            <Text style={{ color: '#fff', fontSize: 18 }}>Logout</Text>
          </TouchableOpacity>
        </View>
        {this.renderUser()}
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
  userInfoContainer: {
    flexGrow: 1,
    flexShrink: 1,
    alignItems: 'center',
  },
  buttonStyle: {
    alignSelf: 'center',
    paddingVertical: 10,
    paddingHorizontal: 20,
    backgroundColor: 'blue',
    flexDirection: 'row',
    alignItems: 'center',
    borderRadius: 5,
    marginVertical: 10,
  },
});

rn-zalo's People

Contributors

phithu avatar phithuxyz avatar doko-demo-doa avatar

Watchers

James Cloos 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.