DEV Community

lyc233333
lyc233333

Posted on

🌐 Crossing the boundaries and integrating the future: Thoughts on ArkWeb empowering new experiences in social applications

In today's world where social applications are extremely homogeneous, ArkWeb components are like opening up the "arbitrary door" between native and web.This article will use practical cases to analyze how to use ArkWeb to make social applications break through boundaries, realize seamless embedding and cross-platform sharing of web content, and create the next generation of social experience.

1. ArkWeb component: "any web door" for social applications

1. Panoramic view of core capabilities

Ability type Specific functions Examples of social application scenarios
Web page loading Local/online web page rendering Embed news, blogs, video websites
Interaction control JavaScript interface call Web page button triggers native sharing
Data Interoperability PostMessage Two-way Communication Web pages deliver sharing content to applications
Security protection Traceless mode, ad blocking Protect user privacy
Same-layer rendering Native components and web mixing Web content and native UI integration

2. Differences from traditional WebViews

Dimensions Traditional WebView ArkWeb Components
Rendering performance Independent process, easy lag Rendering in the same layer, fluency is improved by 50%
Native interaction Limited interface Full JS-native bidirectional communication
Security mechanism Basic protection Domain name whitelist + encrypted transmission
Extension capability Function fixation Support plug-in extension

2. ArkWeb practice in social applications: Three-step song for web embedding

1. Basic web page loading (news socialization case)

import { webview } from '@ohos.web.webview';

@Entry
@Component
struct SocialNewsPage {
  private controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column {
// Embed news page
      Web({
        src: 'https://news.harmonyos.com',
        controller: this.controller,
        onPageStarted: () => {
console.log('News page starts loading');
        },
        onPageFinished: () => {
console.log('News page loading is complete');
this.injectShareScript(); // Inject share script
        }
      })
      .width('100%')
      .height('100%')
    }
    .width('100%')
    .height('100%')
  }

// Inject sharing function script
  injectShareScript() {
    this.controller.executeJavaScript(`
// Add a share button to the web page
      const shareBtn = document.createElement('button');
      shareBtn.id = 'socialShareBtn';
shareBtn.textContent = 'Share to social circle';
      document.body.appendChild(shareBtn);

// Binding and sharing events
      document.getElementById('socialShareBtn').addEventListener('click', function() {
        const title = document.title;
        const url = window.location.href;
window.appShare.postMessage({ title, url }); // Call the application sharing interface
      });
    `);
  }
}
Enter fullscreen mode Exit fullscreen mode

2. "Two-way dialogue" between web pages and native

// Receive web messages on the application side
this.controller.on('message', (event) => {
  const message = event.data;
console.log(`Received the web message: ${JSON.stringify(message)}`);

// Process sharing content
  if (message.type === 'share') {
    this.shareToSocialPlatform(message.content);
  }
});

// Share to social platforms
shareToSocialPlatform(content: ShareContent) {
// Call the WeChat sharing API
  wechat.shareToWeChat({
    title: content.title,
    description: content.desc,
    imageUrl: content.imageUrl,
    success: () => {
this.showToast('Shared Successfully');
this.sendToWeb('Shared Successfully'); // Notify the webpage to share the results
    },
    fail: (err) => {
this.showToast('Sharing failed');
this.sendToWeb(`Share failed: ${err.message}`);
    }
  });
}

// The application sends a message to the web page
sendToWeb(message: string) {
  this.controller.postMessage(message);
}
Enter fullscreen mode Exit fullscreen mode

3. Cross-platform sharing: "Information Leap" from web pages to social circles

1. Social Platform API Integration Flowchart

Web content (news/video) β†’ ArkWeb component β†’ Application native sharing module β†’ Social platform API β†’ Moments/Weibo
        ↑                                 ↓  
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

2. Complete implementation of WeChat sharing

import { wechat } from '@ohos.arkui';

// Share components
@Entry
@Component
struct SocialShare {
  @State shareContent: ShareContent = {
    title: '',
    desc: '',
    imageUrl: '',
    url: ''
  };

  build() {
    Column {
// Share preview
      Image(this.shareContent.imageUrl)
        .width(200)
        .height(150)
        .objectFit(ImageFit.Contain)

      Text(this.shareContent.title)
        .fontSize(18)
        .margin({ top: 10 })

      Text(this.shareContent.desc)
        .fontSize(14)
        .margin({ top: 5 })
        .lineClamp(2)

// Share button
Button('Share to WeChat Moments')
        .width('90%')
        .height(50)
        .onClick(() => this.shareToWeChat())
    }
    .padding(20)
    .width('100%')
  }

  shareToWeChat() {
    wechat.shareToWeChat({
scene: wechat.ShareScene.TIMELINE, // Share to friends circle
      title: this.shareContent.title,
      description: this.shareContent.desc,
      thumbImageUrl: this.shareContent.imageUrl,
      targetUrl: this.shareContent.url,
      success: () => {
console.log('WeChat sharing successfully');
this.showToast('Shared Successfully');
      },
      fail: (error) => {
console.error(`WeChat sharing failed: ${error.message}`);
this.showToast('Sharing failed, please try again');
      }
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Deep integration: ArkWeb+ native "new social species"

1. Same-layer rendering: "seamless stitching" between web pages and native UI

@Entry
@Component
struct HybridSocialPage {
  private controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Stack() {
// The underlying web page (video live comment area)
      Web({
        src: 'https://live.harmonyos.com/12345',
        controller: this.controller
      })
      .width('100%')
      .height('100%')

// The upper layer native like button (suspended above the web page)
      Column {
        Button('πŸ‘')
          .width(60)
          .height(60)
          .backgroundColor('#FF0000')
          .borderRadius(30)
          .margin({ bottom: 50 })
      }
      .align(Alignment.BottomRight)
    }
    .width('100%')
    .height('100%')
  }
}
Enter fullscreen mode Exit fullscreen mode

2. Data interoperability: Building a "social-Web" data closed loop

// The application delivers user social data to the web page
const userData = {
  nickname: 'HarmonyUser',
  avatarUrl: 'https://avatar.harmony.com/123.png',
socialTags: ['Technology', 'Digital', 'Hongmeng']
};

this.controller.postMessage({
  type: 'userData',
  data: userData
});

// The web page receives user data and displays it
window.appShare.on('message', (data) => {
  if (data.type === 'userData') {
    document.getElementById('userNickname').textContent = data.data.nickname;
    document.getElementById('userAvatar').src = data.data.avatarUrl;
// Render user tags
    renderTags(data.data.socialTags);
  }
});
Enter fullscreen mode Exit fullscreen mode

5. Operational strategy: "value conversion" from traffic to stickiness

1. Personalized content recommendation system

Data dimensions Collection method Application scenarios
Web browsing behavior ArkWeb component burial Recommend similar web content
Sharing preferences Sharing API statistics Optimize sharing material types
Social interaction data Native social function records Personalized content aggregation

2. Social fission activity design

Web page content β†’ One-click sharing (get points) β†’ Point redemption (native social function) β†’ Invite friends (web page + native double reward)
Enter fullscreen mode Exit fullscreen mode

Case: Earn points by news sharing

  1. Embed news content on the web page and add the "Share Earn Points" button
  2. After sharing, the ArkWeb notification application will be added to the points system.
  3. Points can be redeemed for membership benefits in native social applications

6. Future Outlook: ArkWeb-driven "Social Metaverse"

1. Three frontier directions

🌐 Web Game Socialization

  • ArkWeb embeds HTML5 games to realize "talk while playing"
  • Case: Social apps are embedded in Landlord games, supporting friend battles + voice chat

πŸ“Ί Live broadcast interaction

  • ArkWeb loads live streams, native components provide gift and barrage functions
  • Technical points: live streaming and native UI rendering at the same level, interactive data synchronization in both directions

πŸ•ΆοΈ Metaverse social scene

  • ArkWeb loads 3D virtual scenes, native components control virtual avatars
  • Case: Social applications are embedded in virtual exhibitions, users log in to roam through their native account

2. Technology evolution roadmap

Current stage (2023): Web page embedding + basic sharing β†’ Next stage: same-layer rendering + data closed loop β†’ Future: Web native fusion + meta universe
Enter fullscreen mode Exit fullscreen mode

7. Practical combat pit avoidance guide

1. Three principles of performance optimization

  1. Lazy Loading: Lazy Loading of non-home page content
  2. Resource preload: Shared materials are cached in advance
  3. Memory Monitoring: Clean the ArkWeb cache regularly

2. Key points of safety protection

  • Domain Whitelist: Restrict embeddable web domain names
  • JS interface permission control: Only necessary native interfaces are opened
  • Data encryption: Cross-end transmission of data encryption processing

Conclusion

ArkWeb components are reshaping the boundaries of social applications, from a simple information sharing platform to a fusion space of "Web content + native experience".In the future, with the continuous evolution of ArkWeb's capabilities, social applications will become a super entrance to connect all digital services, truly realizing the experience vision of "crossing boundaries and integrating the future".

Top comments (0)