JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import "ShareViewController.h"
 
@interface ShareViewController ()
 
@end
 
@implementation ShareViewController
 
- (BOOL)isContentValid {
    // Do validation of contentText and/or NSExtensionContext attachments here
    return YES;
}
 
 
- (void)didSelectPost {
    NSString* groupName = [NSString stringWithFormat:@"group.%@",[[NSBundle mainBundle] bundleIdentifier]];
    NSLog(@"[SHARE EXTENSTION] using group name inside EXTENSION %@",groupName);
    // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
    BOOL support = TRUE;
    // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
    for (NSExtensionItem *item in self.extensionContext.inputItems) {
        for (NSItemProvider *provider in item.attachments) {
            NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:groupName];
            // TODO: Use [provider registeredTypeIdentifiersWithFileOptions:0]; to get all type identifiers of the provider instead of this if/else if structure
            support = TRUE;
            bool found = false;
            for (NSString *ti in SUPPORTED_EXTENTIONS) {
                if ([provider hasItemConformingToTypeIdentifier:ti]) {
                    found=true;
                    [self loadItem:provider typeIdentifier:ti defaults:defaults];
                    // Send only one item
                    return;
                }
            }
            if (!found){
                NSLog(@"Unkown itemprovider = %@", provider);
                support = false;
            }
        }
    }
    if (!support)
         [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}
 
- (NSArray *)configurationItems {
    // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
    return @[];
}
 
- (NSString *)cacheDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    BOOL isDir = NO;
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
        [[NSFileManager defaultManager] createDirectoryAtPath:cachePath
                                  withIntermediateDirectories:NO
                                                   attributes:nil
                                                        error:&error];
    }
    return cachePath;
}
 
-(void) nsDataWrite:(NSData *)data {
    NSString* groupName = [NSString stringWithFormat:@"group.%@",[[NSBundle mainBundle] bundleIdentifier]];
    NSError *error = nil;
    NSString *path  =[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupName] path];
    NSString *fullCacheFilePathPath = [NSString stringWithFormat:@"%@/%@",path,@"nsData"];
    [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:fullCacheFilePathPath] error:&error];
    if (![data writeToFile:fullCacheFilePathPath atomically:YES]) {
        NSLog(@"nsDataWrite error");
    }
}
 
- (void)loadItem:(NSItemProvider *)provider typeIdentifier:(NSString *)typeIdentifier defaults:(NSUserDefaults *)defaults  {
    [provider loadItemForTypeIdentifier:typeIdentifier options:nil completionHandler:^(id<NSSecureCoding>  _Nullable item, NSError * _Null_unspecified error) {
        if([(NSObject*)item isKindOfClass:[NSURL class]]) {
            NSURL *url = (NSURL *)item;
            NSData *nsData = [NSData dataWithContentsOfURL:url];
            
            if (nsData) {
                NSString *imgPath = url.path;
                NSString *filename = [imgPath lastPathComponent];
                if([imgPath containsString:@"var/mobile/Media/PhotoData"]) {
                    // We get the corresponding PHAsset identifier so we can display the image in the app without having to duplicate it.
                    NSDictionary *dict = @{@"url" : filename,
                                           @"message" : self.contentText};
                    [self nsDataWrite:nsData];
                    [defaults setObject:dict forKey:@"photoData"];
                } else if ([imgPath containsString:@"var/mobile/Library/Mobile Documents/com~apple~CloudDocs"] || [[url scheme] isEqualToString:@"file"]) {
                    // shared files from icloud drive
                    NSDictionary *dict = @{@"url" : filename,
                                           @"message" : self.contentText};
                    [self nsDataWrite:nsData];
                    [defaults setObject:dict forKey:@"icloudData"];
                } else {
                    NSDictionary *dict = @{@"url" : [url absoluteString],
                                           @"message" : self.contentText};
                    [defaults setObject:dict forKey:@"url"];
                }
            } else {
                //Others
                NSDictionary *dict = @{@"url" : [url absoluteString],
                                       @"message" : self.contentText};
                [defaults setObject:dict forKey:@"url"];
            }
 
            [self respondUrl:defaults];
        } else if ([(NSObject*)item isKindOfClass:[UIImage class]]) {
            UIImage *image = (UIImage*)item;
            NSDictionary *dict = @{@"url" : [NSString stringWithFormat:@"IMAGE_%f.PNG", [[NSDate date] timeIntervalSince1970]],
                                   @"message" : self.contentText};
            [self nsDataWrite:UIImagePNGRepresentation(image)];
            [defaults setObject:dict forKey:@"photoData"];
            
            [self respondUrl:defaults];
        } else {
            //share text
            NSLog(@"Unsupported provider = %@", provider);
            [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
        }
    }];
}
 
- (void)respondUrl:(NSUserDefaults *)defaults {
    UIResponder *responder = self;
    while (responder != nil) {
        if ([responder respondsToSelector:@selector(openURL:)]) {
            [responder performSelector:@selector(openURL:)
                            withObject:[NSURL URLWithString:@"message-linphone://" ]];
            [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
            break;
        }
        responder = [responder nextResponder];
    }
    [defaults synchronize];
}
 
@end