0%

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调用友盟分享功能.