的iOS:苹果观看保存在应用程序组中使用的图像

问题描述:

背景:的iOS:苹果观看保存在应用程序组中使用的图像

在我的iPhone应用程序,我检索图像,并将它们保存到文件目录,以便更快加载。我知道要在Apple Watch上使用这些图像,我必须与App Group分享这些图像。

因此,我创建了一个App Group,更新了我的配置文件,所有爵士乐。现在我的问题是,我不知道如何将图像保存到App Group并在WatchKit文件中读取该图像。

以下是我已经尝试了保存图像的应用程序组:

NSString *container = @"group.com.appName.watchdatasharing"; 

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container]; 

[defaults setValue:UIImagePNGRepresentation([FileManager readImageFromFileWithName:@"icon1_imgUrl"]) forKey:@"icon1_imgUrl"]; 
  • 注:我的文件管理类返回一个UIImage

在我WatchKit检索图像我使用这段代码:

NSString *container = @"group.com.fantrac.watchdatasharing"; 

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container]; 

NSData* imageData = [defaults valueForKey:@"icon1_imgUrl"]; 
UIImage* image = [UIImage imageWithData:imageData]; 

[tableRow.iconImage setImage:image]; 

Q问题:

当我在Apple Watch上测试时没有图像显示。为了在我的应用和Apple Watch之间保存/检索图像,我需要做些什么不同?

+0

您是否使用watchOS 1或2? watchOS 2无法访问共享的应用程序组。 – dan

+0

我正在使用watchOS 2 ....多数民众赞成在奇怪的,你能否建议另一种解决方案使用保存在手表的应用程序的文档目录中的图像? –

+0

您必须使用WatchConnectivity框架并在WCSession上使用'transferFile:metadata:'方法将文件从手机传输到手表,然后将该文件保存在手表自己的文档目录中。 – dan

如果您使用watchOS 2,则可以使用WatchConnectivity。我附上一个示例代码供您参考。

在iPhone上:

// Create a Watch Connectivity session 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.applicationDict = @{@"foo" : @"bar"}; 

    if ([WCSession isSupported]) { 
     WCSession *session = [WCSession defaultSession]; 
     session.delegate = self; 
     [session activateSession]; 
    } 
} 

// Transfer file to Apple Watch 
- (IBAction)fileTransferButtonTapped:(id)sender 
{ 
    // File Transfer 
    NSURL *url = 
    [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"]]; 
    WCSessionFileTransfer *fileTransfer = 
    [[WCSession defaultSession] transferFile:url 
            metadata:self.applicationDict]; 
} 

上观看:

// Receive file from iPhone 
- (void)session:(nonnull WCSession *)session didReceiveFile:(nonnull WCSessionFile *)file 
{ 
    // recieve file 
} 

参考。 http://www.kristinathai.com/watchos-2-how-to-communicate-between-devices-using-watch-connectivity/

+0

这很棒,watchOS 1是否可以与WatchConnectivity一起使用?我发现的所有内容仅与WatchOS 2讨论WatchConnectivity框架2 –

+0

WatchConnectivity在watchOS 1中不可用。 –

如果您希望多个应用访问相同的“沙箱”,您的选择可能包括AppGroups,回答原始问题。

使用的应用程序组允许多个应用程序访问共享的容器,并允许应用程序

here之间额外的进程间通信,你可以找到信息如何在配置应用程序组部分配置应用程序组。

一旦配置AppGroup和正确的权利添加到您的应用程序,你可以用下面的代码访问常用容器:

NSString * kSharedAppGroup = @"group.com.appName.watchdatasharing"; 

+ (NSURL*)commonStoreUrl { 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSURL *modelDestUrl = [[fileManager containerURLForSecurityApplicationGroupIdentifier:kSharedAppGroup] URLByAppendingPathComponent: kStoreName]; 
    return modelDestUrl; 
} 

用法:

NSURL *commonContainer = [Someclass commonStoreUrl]; 

这将是如果有用的话你的应用程序需要访问一个公共数据库,无论如何,如果你的方案只是从手表到iOS应用程序使用WCSession的微不足道的信息。 (仅在watchkit 2之后才可用。2)

对于Swift3

这将是─

let sharedFolderURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.your.app")! as URL