简体   繁体   English

在iOS App和WatchKit Extension之间共享Plist

[英]Share Plist between iOS App and WatchKit Extension

I have an app that saves and uses data from a plist file. 我有一个应用程序,可以保存和使用plist文件中的数据。 I'm working on a WatchKit extension that needs to access the same plist file to display data and save to the file. 我正在开发一个WatchKit扩展,需要访问相同的plist文件来显示数据并保存到文件中。 I know I need to be using app groups but I don't know how to share the plist between the iOS app and the WatchKit extension. 我知道我需要使用应用程序组,但我不知道如何在iOS应用程序和WatchKit扩展程序之间共享plist。

Here is how I'm saving to the plist in the iOS app currently. 这是我目前在iOS应用程序中保存到plist的方式。

NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"locations.plist"];
    BOOL fileExists = [fileManager fileExistsAtPath:docPath];
    NSError *error = nil;

    if (!fileExists) {
        NSString *strSourcePath = [[NSBundle mainBundle]pathForResource:@"locations" ofType:@"plist"];
        [fileManager copyItemAtPath:strSourcePath toPath:docPath error:&error];
    }

    NSString *path = docPath;
    NSMutableArray *plistArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
    NSDictionary *locationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.locationNameTextField.text, @"locationName", latString, @"latitude", longString, @"longitude", nil];
    [plistArray insertObject:locationDictionary atIndex:0];
    [plistArray writeToFile:docPath atomically:YES];

Once you've setup your app group (in both your primary iPhone app and the Watch Extension), you can get the path to the shared folder: 设置应用程序组后(在主iPhone应用程序和Watch Extension中),您可以获取共享文件夹的路径:

NSURL *groupContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"YourAppGroupSuiteName"];
NSString *groupContainerPath = [groupContainerURL path];

You can then use the groupContainerPath to build your docPath . 然后,您可以使用groupContainerPath构建docPath Otherwise, your code should work as-is. 否则,您的代码应该按原样运行。

I was able to successfully use plist data from my main existing iPhone app in my WatchKit app using Swift. 我能够使用Swift在我的WatchKit应用程序中成功使用来自我现有主要iPhone应用程序的plist数据。

There are two steps: 有两个步骤:

  1. Enable WatchKit App Extension Target Membership for each plist you want to use. 为您要使用的每个plist启用WatchKit App扩展目标成员资格。 Click on the plist, then: 点击plist,然后:

在此输入图像描述

  1. Here's the Swift code I used to read the plist, which has "id" and "name" fields. 这是我用来读取plist的Swift代码,它有“id”和“name”字段。

     func valueFromPlist(value: Int, file: String) -> String? { if let plistpath = NSBundle.mainBundle().pathForResource(file as String, ofType: "plist") { if let entries = NSArray(contentsOfFile: plistpath) as Array? { var entry = Dictionary<String, Int>() for entry in entries { if let id = entry.objectForKey("id") as? Int { if id == value { if let name = entry.objectForKey("name") as? String { return name } } } } } } return nil } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WatchKit扩展和iPhone应用程序之间共享钥匙串数据 - How to share keychain data between a WatchKit extension and an iPhone app 如何通过存储“回复”块在iOS应用和WatchKit扩展之间进行通信? - How to communicate between an iOS app and the WatchKit extension by storing the “reply” block? 在iOS App及其扩展程序之间共享情节提要 - Share Storyboard between iOS App and its Extension 在watchkit扩展和观看应用程序.plist文件中设置ATS密钥 - set ATS keys in watchkit extension & watch app .plist file 具有WatchKit扩展名的父级应用程序是否为iOS 8及更高版本? - Does parent app with WatchKit extension be iOS 8 and later? iOS使用Alamofire快速在应用程序和共享扩展之间共享Cookie - iOS swift using alamofire to share cookie between app and share extension 用于在共享扩展和iOS应用之间共享文件路径/文件的代码 - Code to share file path/file between a share extension and iOS app 通过WatchKit扩展检测iOS应用是否在前台 - Detect if iOS app is in foreground from WatchKit extension 在iOS8中的App和App Extension之间共享用户对象 - Share User Object between App and App Extension in iOS8 如何在 iOS 中的扩展和主机应用程序之间共享后删除数据 - How to remove data after share between extension and host app in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM