Commit 48809c93952b001aacb83768bb9e09b8b1fcd6c8
1 parent
7a035955e7
Exists in
master
and in
1 other branch
Share email
Showing 1 changed file with 4 additions and 0 deletions Inline Diff
LifeLog/LifeLog/Utilities.m
| 1 | // | 1 | // |
| 2 | // Utilities.m | 2 | // Utilities.m |
| 3 | // LifeLog | 3 | // LifeLog |
| 4 | // | 4 | // |
| 5 | // Created by Nguyen Van Phong on 7/29/17. | 5 | // Created by Nguyen Van Phong on 7/29/17. |
| 6 | // Copyright © 2017 PhongNV. All rights reserved. | 6 | // Copyright © 2017 PhongNV. All rights reserved. |
| 7 | // | 7 | // |
| 8 | 8 | ||
| 9 | #import <Social/Social.h> | 9 | #import <Social/Social.h> |
| 10 | #import <LineKit/Line.h> | 10 | #import <LineKit/Line.h> |
| 11 | 11 | ||
| 12 | #import "Utilities.h" | 12 | #import "Utilities.h" |
| 13 | 13 | ||
| 14 | @implementation Utilities | 14 | @implementation Utilities |
| 15 | + (NSString *)addCommaFromNumber:(NSInteger)number | 15 | + (NSString *)addCommaFromNumber:(NSInteger)number |
| 16 | { | 16 | { |
| 17 | NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init]; | 17 | NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init]; |
| 18 | [fmt setNumberStyle:NSNumberFormatterDecimalStyle]; | 18 | [fmt setNumberStyle:NSNumberFormatterDecimalStyle]; |
| 19 | [fmt setMaximumFractionDigits:0]; | 19 | [fmt setMaximumFractionDigits:0]; |
| 20 | NSString *result = [fmt stringFromNumber:@(number)]; | 20 | NSString *result = [fmt stringFromNumber:@(number)]; |
| 21 | return result; | 21 | return result; |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | + (UIColor *)convertHecToColor:(int) hex | 24 | + (UIColor *)convertHecToColor:(int) hex |
| 25 | { | 25 | { |
| 26 | return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 \ | 26 | return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 \ |
| 27 | green:((float)((hex & 0xFF00) >> 8))/255.0 \ | 27 | green:((float)((hex & 0xFF00) >> 8))/255.0 \ |
| 28 | blue:((float)(hex & 0xFF))/255.0 alpha:1.0]; | 28 | blue:((float)(hex & 0xFF))/255.0 alpha:1.0]; |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | + (void)showErrorMessage:(NSString *)message withViewController:(UIViewController *)vc | 31 | + (void)showErrorMessage:(NSString *)message withViewController:(UIViewController *)vc |
| 32 | { | 32 | { |
| 33 | if (message.length > 0) { | 33 | if (message.length > 0) { |
| 34 | UIAlertController * alert= [UIAlertController | 34 | UIAlertController * alert= [UIAlertController |
| 35 | alertControllerWithTitle:@"Error" | 35 | alertControllerWithTitle:@"Error" |
| 36 | message:message | 36 | message:message |
| 37 | preferredStyle:UIAlertControllerStyleAlert]; | 37 | preferredStyle:UIAlertControllerStyleAlert]; |
| 38 | 38 | ||
| 39 | UIAlertAction* ok = [UIAlertAction | 39 | UIAlertAction* ok = [UIAlertAction |
| 40 | actionWithTitle:@"OK" | 40 | actionWithTitle:@"OK" |
| 41 | style:UIAlertActionStyleDefault | 41 | style:UIAlertActionStyleDefault |
| 42 | handler:^(UIAlertAction * action) | 42 | handler:^(UIAlertAction * action) |
| 43 | { | 43 | { |
| 44 | [alert dismissViewControllerAnimated:YES completion:nil]; | 44 | [alert dismissViewControllerAnimated:YES completion:nil]; |
| 45 | 45 | ||
| 46 | }]; | 46 | }]; |
| 47 | 47 | ||
| 48 | [alert addAction:ok]; | 48 | [alert addAction:ok]; |
| 49 | 49 | ||
| 50 | [vc presentViewController:alert animated:YES completion:nil]; | 50 | [vc presentViewController:alert animated:YES completion:nil]; |
| 51 | } | 51 | } |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | + (void) shareFacebook : (NSString *) content withViewController:(UIViewController *)vc { | 54 | + (void) shareFacebook : (NSString *) content withViewController:(UIViewController *)vc { |
| 55 | if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) | 55 | if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) |
| 56 | { | 56 | { |
| 57 | SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; | 57 | SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; |
| 58 | 58 | ||
| 59 | [composeViewController setInitialText:content]; | 59 | [composeViewController setInitialText:content]; |
| 60 | [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) { | 60 | [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) { |
| 61 | 61 | ||
| 62 | switch (result) { | 62 | switch (result) { |
| 63 | case SLComposeViewControllerResultCancelled: | 63 | case SLComposeViewControllerResultCancelled: |
| 64 | NSLog(@"canceled"); | 64 | NSLog(@"canceled"); |
| 65 | break; | 65 | break; |
| 66 | case SLComposeViewControllerResultDone: | 66 | case SLComposeViewControllerResultDone: |
| 67 | NSLog(@"done"); | 67 | NSLog(@"done"); |
| 68 | break; | 68 | break; |
| 69 | default: | 69 | default: |
| 70 | break; | 70 | break; |
| 71 | } | 71 | } |
| 72 | }]; | 72 | }]; |
| 73 | [vc presentViewController:composeViewController animated:YES completion:nil]; | 73 | [vc presentViewController:composeViewController animated:YES completion:nil]; |
| 74 | } | 74 | } |
| 75 | else { | 75 | else { |
| 76 | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=FACEBOOK"]]; | 76 | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=FACEBOOK"]]; |
| 77 | } | 77 | } |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | + (void) shareTwitter : (NSString *) content withViewController:(UIViewController *)vc { | 80 | + (void) shareTwitter : (NSString *) content withViewController:(UIViewController *)vc { |
| 81 | if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) | 81 | if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) |
| 82 | { | 82 | { |
| 83 | SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; | 83 | SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; |
| 84 | 84 | ||
| 85 | [composeViewController setInitialText:content]; | 85 | [composeViewController setInitialText:content]; |
| 86 | [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) { | 86 | [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) { |
| 87 | 87 | ||
| 88 | switch (result) { | 88 | switch (result) { |
| 89 | case SLComposeViewControllerResultCancelled: | 89 | case SLComposeViewControllerResultCancelled: |
| 90 | NSLog(@"canceled"); | 90 | NSLog(@"canceled"); |
| 91 | break; | 91 | break; |
| 92 | case SLComposeViewControllerResultDone: | 92 | case SLComposeViewControllerResultDone: |
| 93 | NSLog(@"done"); | 93 | NSLog(@"done"); |
| 94 | break; | 94 | break; |
| 95 | default: | 95 | default: |
| 96 | break; | 96 | break; |
| 97 | } | 97 | } |
| 98 | }]; | 98 | }]; |
| 99 | [vc presentViewController:composeViewController animated:YES completion:nil]; | 99 | [vc presentViewController:composeViewController animated:YES completion:nil]; |
| 100 | } | 100 | } |
| 101 | else { | 101 | else { |
| 102 | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=TWITTER"]]; | 102 | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=TWITTER"]]; |
| 103 | } | 103 | } |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | + (void) shareLine : (NSString *) content withViewController:(UIViewController *)vc { | 106 | + (void) shareLine : (NSString *) content withViewController:(UIViewController *)vc { |
| 107 | if (![Line isLineInstalled]) { | 107 | if (![Line isLineInstalled]) { |
| 108 | [self showErrorMessage:@"Install Line app first" withViewController:vc]; | 108 | [self showErrorMessage:@"Install Line app first" withViewController:vc]; |
| 109 | } | 109 | } |
| 110 | else { | 110 | else { |
| 111 | [Line shareText:content]; | 111 | [Line shareText:content]; |
| 112 | } | 112 | } |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | + (void) shareEmail : (NSString *) content withViewController:(UIViewController *)vc { | 115 | + (void) shareEmail : (NSString *) content withViewController:(UIViewController *)vc { |
| 116 | NSString *urlEmail = @"mailto:?subject=Share from LifeLog&body="; | ||
| 117 | urlEmail = [urlEmail stringByAppendingString:content]; | ||
| 118 | urlEmail = [urlEmail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]; | ||
| 119 | [[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlEmail]]; | ||
| 116 | } | 120 | } |
| 117 | @end | 121 | @end |
| 118 | 122 |