// // WXAppDelegate.m // Unity-iPhone // // Created by qyq on 2018/1/23. // #import #import "WXAppDelegate.h" @interface WXAppDelegate() @end static NSString* gWXAppId = nil; static BOOL gWXRegistered = NO; #ifdef __cplusplus extern "C" { #endif void WX_SetAppId(const char *appid); void WX_Register(); #ifdef __cplusplus } #endif @implementation WXAppDelegate -(void) onResp:(BaseResp*)resp { if([resp isKindOfClass:[SendMessageToWXResp class]]) {//分享 if (resp.errCode == 0) { UnitySendMessage("Core","OnShareAction",""); } } else if([resp isKindOfClass:[SendAuthResp class]]) {//登录 [self getWeiXinCodeFinishedWithResp:resp]; } } - (void)getWeiXinCodeFinishedWithResp:(BaseResp *)resp { if (resp.errCode == 0) { SendAuthResp *aresp = (SendAuthResp *)resp; const char*code=[aresp.code UTF8String]; UnitySendMessage("Core", "WXGetToken", code); } } + (NSString*)getAppId { return gWXAppId; } + (void)setAppId:(NSString *)appid { if (gWXRegistered) { NSLog(@"[WX] already registered, ignore setAppId"); return; } gWXAppId = appid; } + (void)registerWeChat { if (gWXRegistered) { NSLog(@"[WX] register already done"); return; } if (gWXAppId == nil || gWXAppId.length == 0) { NSLog(@"[WX] AppId not set, cannot register"); return; } NSLog(@"[WX] register with appid = %@", gWXAppId); [WXApi registerApp:gWXAppId]; gWXRegistered = YES; } + (void) share:(NSDictionary*)dic { if (!gWXRegistered) { NSLog(@"[WX] share called before register"); return; } if (![WXAppDelegate checkApp]) return; NSDictionary* mediaObject = [dic objectForKey:@"mediaObject"]; NSString* info = [dic objectForKey:@"description"]; int scene = [[dic objectForKey:@"scene"] intValue]; NSString* title = [dic objectForKey:@"title"]; NSNumber* type = [mediaObject objectForKey:@"type"]; NSString* url = [mediaObject objectForKey:@"url"]; NSString* picUrl = @""; NSString* thumbUrl = @""; WXMediaMessage *message = [WXMediaMessage message]; if ([type isEqual:@0]) { message.title = title; message.description = info; WXWebpageObject *ext = [WXWebpageObject object]; [message setThumbImage:[UIImage imageNamed:@"AppIcon57x57"]]; ext.webpageUrl =url; message.mediaObject = ext; } else if([type isEqual:@1]) { NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); picUrl = [[[[filePaths objectAtIndex:0] stringByAppendingString:@"/"] stringByAppendingString:[mediaObject objectForKey:@"filename"]] stringByAppendingString:@".jpg"]; thumbUrl = [picUrl stringByReplacingOccurrencesOfString:@".jpg" withString:@"_thumb.jpg"]; UIImage* pic = [UIImage imageWithContentsOfFile:picUrl]; UIImage* thumb = [UIImage imageWithContentsOfFile:thumbUrl]; [message setThumbImage:thumb]; WXImageObject *imageObject = [WXImageObject object]; imageObject.imageData = UIImageJPEGRepresentation(pic, 1.0); message.mediaObject = imageObject; } else{ NSLog(@"invalid type"); return; } SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = scene; if(![WXApi sendReq:req]) NSLog(@"share picture failure"); } +(void)login { if (!gWXRegistered) { NSLog(@"[WX] login called before register"); return; } if (![WXAppDelegate checkApp]) return; //发送登录请求 SendAuthReq* req = [[SendAuthReq alloc] init]; req.scope = @"snsapi_userinfo"; req.state = @"51qp_test"; [WXApi sendReq:req]; } +(bool) checkApp { if(![WXApi isWXAppInstalled]){ NSString* str_url = [WXApi getWXAppInstallUrl]; NSURL* url = [NSURL URLWithString:str_url]; [[UIApplication sharedApplication] openURL:url]; return false; } if(![WXApi isWXAppSupportApi]){ NSLog(@"WX app isn't support api;"); return false; } return true; } @end void WX_SetAppId(const char *appid) { if (appid == NULL) { NSLog(@"[WX][C] SetAppId null"); return; } NSString *appIdStr = [NSString stringWithUTF8String:appid]; [WXAppDelegate setAppId:appIdStr]; } void WX_Register() { [WXAppDelegate registerWeChat]; }