2015-10-16 42 views
1

背景:的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

回答

5

如果您使用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中不可用。 –

2

如果您希望多個應用訪問相同的「沙箱」,您的選擇可能包括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)

0

對於Swift3

這將是─

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