Apple Watch Extension如何访问NSDocumentDirectory中的文件?

时间:2015-05-29 20:59:31

标签: ios objective-c apple-watch

我试图访问包含NSDocumentDirectory中的数组的plist文件。这是您可以通过iTunes中的文件共享访问文件的目录。我试图使用此代码访问该文件

- (instancetype)init {
    self = [super init];
    if (self){
         paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"fav.out"];
        NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];
        if (!arrayFromFile)
             arrayFromFile = [[NSArray alloc] init];
        self.favArray = [[NSMutableArray alloc] initWithArray:arrayFromFile];
    }
}

1 个答案:

答案 0 :(得分:3)

您无法直接使用主应用程序的文件。您应该设置一个应用程序组并使用如下路径:

#!/bin/sh # # Startup script for mercurial server. # # @see http://jf.blogs.teximus.com/2011/01/running-mercurial-hg-serve-on-linux.html HG=/usr/bin/hg CONF=/etc/mercurial/hgweb.config # Path to PID file of running mercurial process. PID_FILE=/etc/mercurial/hg.pid state=$1 case "$state" in 'start') echo "Mecurial Server service starting." (su hguser -c "${HG} serve -d --webdir-conf ${CONF} -p 8000 --pid-file ${PID_FILE}") ;; 'stop') if [ -f "${PID_FILE}" ]; then PID=`cat "${PID_FILE}"` if [ "${PID}" -gt 1 ]; then kill -TERM ${PID} echo "Stopping the Mercurial service PID=${PID}." else echo Bad PID for Mercurial -- \"${PID}\" fi else echo No PID file recorded for mercurial fi ;; *) echo "$0 {start|stop}" exit 1 ;; esac