无法保存与扩展程序共享的网址

时间:2017-01-16 01:33:30

标签: ios objective-c singleton ios-extensions

我想使用扩展程序与主机应用程序共享网址。 urlShareManager是我用于在应用程序和扩展程序之间进行通信的嵌入式框架中的单例。当我在Viewcontroller中调用urlShareManager时,我可以添加url。当我添加带扩展名的网址时,它不起作用。我逐行遍历代码,似乎在从扩展中添加一个时保存了url。然而,我可以加载的唯一网址是使用主机应用程序viewcontroller保存的那些网址。我不确定我在这里失踪了什么。

urlShareManager

 + (id)UrlShareManager
    {
        static UrlShare *urlShareManager = nil;
        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{
            urlShareManager = [[self alloc] init];
    //        urlShareManager.urlArray = [[NSMutableArray alloc]init];
        });

        return urlShareManager;
    }

    - (id)init
    {
        if (self = [super init])
        {
            self.urlArray = [[NSMutableArray alloc]init];
            self.urlArray = [self loadURLS];
        }

        return self;

    }


    - (NSString *)getFileURL
    {
        NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.net.noizystudios.urlGroup"];
        NSURL *jsonURL = [NSURL URLWithString:[containerURL.path stringByAppendingString:@"/urlData.dat"]];
        NSString *filePath = [jsonURL absoluteString];
        NSLog(@"Filepath: %@", filePath );
        return filePath;
    }

    - (void)addURL:(NSString *)url
    {
        self.urlArray = [NSMutableArray new];
        NSLog(@"URL STRING: %@", url);
        Bookmark *urlBookmark = [[Bookmark alloc]init];
        urlBookmark.urlString = url;
        [self.urlArray addObject:urlBookmark];
        [self saveURLS];
    }

    - (NSMutableArray *)loadURLS
    {
        NSString *path = [self getFileURL];
        NSMutableArray *url = [[NSMutableArray alloc]init];
        url = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSLog(@"URL array inhoud: %@", url);
        return url;
    }

    - (void)saveURLS
    {

        NSString *path = [self getFileURL];
        BOOL isSaved = [NSKeyedArchiver archiveRootObject:self.urlArray toFile:path];
        NSLog (isSaved ? @"saved" : @"not saved");

    }



    @end

的viewController

- (void)viewDidLoad
{
   [super viewDidLoad];


   UrlShare  *urlManager = [UrlShare UrlShareManager];
   [urlManager addURL:@"http://www.reddit.com"];
   articleListArray = [urlManager loadURLS];
   [self.tableView reloadData];   
}

ActionRequestHandler

- (void)doneWithResults:(NSDictionary *)resultsForJavaScriptFinalize 
{
               ...

                  NSString *url = resultsForJavaScriptFinalize[@"URL"];
                  UrlShare *urlManager = [UrlShare UrlShareManager];
                  [urlManager addURL:url];


                 ... 
                // Don't hold on to this after we finished with it.
                self.extensionContext = nil;
}

1 个答案:

答案 0 :(得分:1)

您是否了解过沙盒和App Transport Security?还有URL书签数据吗?

基本上,出于安全原因,用户必须指定要打开的文件和URL,操作系统知道您的应用程序应该能够打开它们。如果您希望一个应用将此权限转移到另一个应用(并且扩展基本上是单独的应用),则必须传输书签数据,该数据结合了权限和URL。