|
| 1 | +// |
| 2 | +// NativeUtils.mm |
| 3 | +// |
| 4 | + |
| 5 | + |
| 6 | +#import <Foundation/Foundation.h> |
| 7 | +#import <MessageUI/MessageUI.h> |
| 8 | + |
| 9 | +//Hide error of undef function which is available in unity generated xCode project |
| 10 | +//#define FMDEBUG |
| 11 | +//#ifdef FMDEBUG |
| 12 | +//inline UIViewController* UnityGetGLViewController() |
| 13 | +//{ |
| 14 | +// return nil; |
| 15 | +//} |
| 16 | +//#endif |
| 17 | + |
| 18 | +@interface NativeUtils : NSObject<MFMailComposeViewControllerDelegate> |
| 19 | + |
| 20 | ++ (id)sharedInstance; |
| 21 | + |
| 22 | +-(void) shareText: (NSString*) body withURL: (NSString*) urlString withImage:(NSString*) imageDataString withSubject: (NSString*) subject; |
| 23 | +-(void) ShareWeb: (NSString*) body withURL: (NSString*) urlString withImage:(NSString*) imageDataString withSubject: (NSString*) subject; |
| 24 | +-(BOOL) isStringValideBase64:(NSString*)string; |
| 25 | +@end |
| 26 | + |
| 27 | + |
| 28 | +@implementation NativeUtils |
| 29 | + |
| 30 | +static NativeUtils * _sharedInstance; |
| 31 | + |
| 32 | ++ (id)sharedInstance { |
| 33 | + |
| 34 | + if (_sharedInstance == nil) { |
| 35 | + _sharedInstance = [[self alloc] init]; |
| 36 | + } |
| 37 | + |
| 38 | + return _sharedInstance; |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | ++(NSString*) charToNSString: (char*)text { |
| 43 | + return text ? [[NSString alloc] initWithUTF8String:text] : [[NSString alloc] initWithUTF8String:""]; |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +//分享图片 分享文字 |
| 48 | +-(void) SocialSharing: (NSString*) body withURL: (NSString*) urlString withImage:(NSString*) imageDataString withSubject: (NSString*) subject { |
| 49 | + |
| 50 | + NSMutableArray *sharingItems = [NSMutableArray new]; |
| 51 | + if (body && body.length > 0) { |
| 52 | + [sharingItems addObject:body]; |
| 53 | + } |
| 54 | + if (imageDataString && imageDataString.length > 0) { |
| 55 | + UIImage *image = NULL; |
| 56 | + if([self isStringValideBase64:imageDataString]){ |
| 57 | + NSData *imageData = [[NSData alloc] initWithBase64EncodedString:imageDataString options:0]; |
| 58 | + image = [[UIImage alloc] initWithData:imageData]; |
| 59 | + }else{ |
| 60 | + NSData *dataImage = [NSData dataWithContentsOfFile:imageDataString]; |
| 61 | + image = [[UIImage alloc] initWithData:dataImage]; |
| 62 | + } |
| 63 | + [sharingItems addObject:image]; |
| 64 | + } |
| 65 | + if (urlString && urlString.length > 0) { |
| 66 | + [sharingItems addObject:urlString]; |
| 67 | + } |
| 68 | + |
| 69 | + UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil]; |
| 70 | + activityViewController.popoverPresentationController.sourceView = UnityGetGLViewController().view; |
| 71 | + activityViewController.popoverPresentationController.sourceRect = CGRectMake(UnityGetGLViewController().view.frame.size.width/2, UnityGetGLViewController().view.frame.size.height/4, 0, 0); |
| 72 | + |
| 73 | + if(subject && subject.length > 0) |
| 74 | + { |
| 75 | + [activityViewController setValue:subject forKey:@"subject"]; |
| 76 | + } |
| 77 | + |
| 78 | + [UnityGetGLViewController() presentViewController:activityViewController animated:YES completion:nil]; |
| 79 | + |
| 80 | + |
| 81 | +} |
| 82 | +//所有参数都必须有分享网页链接 |
| 83 | +-(void) ShareWeb: (NSString*) body withURL: (NSString*) urlString withImage:(NSString*) imageDataString withSubject: (NSString*) subject { |
| 84 | + |
| 85 | + if(body==NULL||body.length<0||urlString==NULL||urlString.length<0||imageDataString==NULL||imageDataString.length<0||subject==NULL||subject.length<0){ |
| 86 | + return; |
| 87 | + } |
| 88 | + NSURL *urlToShare=[NSURL URLWithString:urlString]; |
| 89 | + UIImage *image = NULL; |
| 90 | + if([self isStringValideBase64:imageDataString]){ |
| 91 | + NSData *imageData = [[NSData alloc] initWithBase64EncodedString:imageDataString options:0]; |
| 92 | + image = [[UIImage alloc] initWithData:imageData]; |
| 93 | + } |
| 94 | + else{ |
| 95 | + NSData *dataImage = [NSData dataWithContentsOfFile:imageDataString]; |
| 96 | + image = [[UIImage alloc] initWithData:dataImage]; |
| 97 | + } |
| 98 | + NSArray *shareItems=@[body,urlToShare,image]; |
| 99 | + |
| 100 | + UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil]; |
| 101 | + activityViewController.popoverPresentationController.sourceView = UnityGetGLViewController().view; |
| 102 | + activityViewController.popoverPresentationController.sourceRect = CGRectMake(UnityGetGLViewController().view.frame.size.width/2, UnityGetGLViewController().view.frame.size.height/4, 0, 0); |
| 103 | + |
| 104 | + if(subject && subject.length > 0) |
| 105 | + { |
| 106 | + [activityViewController setValue:subject forKey:@"subject"]; |
| 107 | + } |
| 108 | + |
| 109 | + [UnityGetGLViewController() presentViewController:activityViewController animated:YES completion:nil]; |
| 110 | +} |
| 111 | + |
| 112 | +-(BOOL) isStringValideBase64:(NSString*)string{ |
| 113 | + |
| 114 | + NSString *regExPattern = @"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"; |
| 115 | + |
| 116 | + NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil]; |
| 117 | + NSUInteger regExMatches = [regEx numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])]; |
| 118 | + return regExMatches != 0; |
| 119 | +} |
| 120 | + |
| 121 | +@end |
| 122 | + |
| 123 | +extern "C" |
| 124 | +{ |
| 125 | + void SocialSharing(char* body, char* url, char* imageDataString, char* subject) { |
| 126 | + |
| 127 | + [[NativeUtils sharedInstance] SocialSharing:[NativeUtils charToNSString:body] withURL:[NativeUtils charToNSString:url] withImage:[NativeUtils charToNSString:imageDataString] withSubject:[NativeUtils charToNSString:subject]]; |
| 128 | + } |
| 129 | + void ShareWeb(char* body, char* url, char* imageDataString, char* subject) { |
| 130 | + |
| 131 | + [[NativeUtils sharedInstance] ShareWeb:[NativeUtils charToNSString:body] withURL:[NativeUtils charToNSString:url] withImage:[NativeUtils charToNSString:imageDataString] withSubject:[NativeUtils charToNSString:subject]]; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | + |
0 commit comments