phoneGap在iOS上的简单使用:自定义实现友盟分享插件

说明:本插件仅针对个人当前项目,参照支付宝支付插件类进行开发,不保证对任何项目都具有通用性,代码仅供参考.


找到项目中的config.xml文件,在相似位置插入如下代码:

 <feature name="UM_SharePlugin">
        <param name="ios-package" value="UM_SharePlugin"/>
    </feature>


具体插件类代码如下:

//
//  UM_SharePlugin.h
//  WDZJ
//
//  Created by Scarecrow on 15/4/23.
//
//
#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>
@interface UM_SharePlugin : CDVPlugin
- (void) print:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end

//
//  UM_SharePlugin.m
//  WDZJ
//
//  Created by Scarecrow on 15/4/23.
//
//

#import "UM_SharePlugin.h"
@implementation UM_SharePlugin
- (void) print:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{

    
    NSLog(@"UM_SharePlugin is called!!!");

    [[NSNotificationCenter defaultCenter] postNotificationName:@"UM_SharePluginCalled" object:options];
}



@end

 

由于cordova项目是基于一个webview的,自动生成的控制器为MainViewController.因为在插件类中无法使用actionSheet弹出分享底框,所有需要在MainViewController.m中接收UM_SharePluginCalled通知,具体代码如下:

在viewDidLoad方法中增加一行代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(umcalled:) name:@"UM_SharePluginCalled" object:nil];
并增加下面两个方法(测试数据来源与友盟官方demo):

- (void)umcalled:(NSNotification *)note
{
    NSString *shareText = @"友盟社会化组件可以让移动应用快速具备社会化分享、登录、评论、喜欢等功能,并提供实时、全面的社会化数据统计分析服务。 http://www.umeng.com/social";             //分享内嵌文字
    UIImage *shareImage = [UIImage imageNamed:@"UMS_social_demo"];          //分享内嵌图片
    
    //调用快速分享接口
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5211818556240bc9ee01db2f"
                                      shareText:shareText
                                     shareImage:shareImage
                                shareToSnsNames:@[UMShareToSina,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ]
                                       delegate:self];
}

- (void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    NSLog(@"didFinishGetUMSocialDataInViewController with response is %@",response);
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}

 

    之后可以在js文件要使用分享的地方调用:        

cordova.exec(function(){},function(){},"UM_SharePlugin","print",obj);
    即可实现cordova调用友盟分享功能.


    -----------------------

    转载请保留出处:http://blog.csdn.net/qq329735967





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值