// // WXAppDelegate.m // Unity-iPhone // // Created by qyq on 2018/1/23. // #import #import "WXAppDelegate.h" @interface WXAppDelegate() @end static NSString* const AppId = @"wx11904574ece35867"; @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 AppId; } + (void) share:(NSDictionary*)dic { 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 (![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