ios - 从锁定屏幕处理本地操作时无法从 iOS 应用沙箱读取
<p><p>为了处理本地操作,我的应用需要读取一个文件来决定要做什么。当本地操作来自与横幅或警报交互的用户时,这可以正常工作。当本地操作来自与锁定屏幕上的通知交互的用户时,它也可以工作,只要应用当前处于后台(而不是完全关闭)。</p>
<p>但是,如果应用完全关闭,并且用户尝试与锁定屏幕上的通知进行交互,我会收到一条错误提示</p>
<pre><code>"The file ... couldn’t be opened because you don’t have permission to view it."
</code></pre>
<p>有谁知道可能出了什么问题,以及在这种情况下我如何才能访问以前保存的文件?</p>
<p>例如,下面的代码可以正常工作,除非由于锁定屏幕上的通知而调用了 handleActionWithIdentifier,并且应用程序已关闭。</p>
<pre class="lang-c prettyprint-override"><code>// Handle local actionable notifications
- (void)application:(UIApplication *) application handleActionWithIdentifier: (NSString *) identifier forLocalNotification: (UILocalNotification *) notification completionHandler: (void (^)()) completionHandler
{
//NSString *test = @"hello world";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ;
NSString *appFile = ;
//;
NSLog(@"*********************");
NSLog(@"trying to read test from handle action");
NSLog(@"trying to read test from this location: %@", appFile);
NSError *testerror;
NSStringEncoding testencoding;
NSString *testString = ;
if (testerror) {
NSLog(@"Failed to read storage file: %@", testerror);
} else if (testencoding != NSUTF8StringEncoding) {
NSLog(@"Incorect encoding of storage file.");
} else {
NSLog(testString);
}
NSLog(@"*********************");
}
</code></pre>
<p>发生错误时上述代码的完整输出如下:</p>
<pre><code>Aug 11 14:39:42 iPhone Tandem <Warning>: *********************
Aug 11 14:39:42 iPhone Tandem <Warning>: trying to read test from handle action
Aug 11 14:39:42 iPhone Tandem <Warning>: trying to read test from this location: /var/mobile/Containers/Data/Application/2C31417E-EF8E-4E0A-AC37-F4CB5BE9A4A5/Documents/test
Aug 11 14:39:42 iPhone Tandem <Warning>: Failed to read storage file: Error Domain=NSCocoaErrorDomain Code=257 "The file “test” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/2C31417E-EF8E-4E0A-AC37-F4CB5BE9A4A5/Documents/test, NSUnderlyingError=0x145577d30 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
Aug 11 14:39:42 iPhone Tandem <Warning>: *********************
</code></pre>
<p>如果我尝试从 <code>application: didFinishLaunching:</code> 读取文件也会出现同样的问题。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您应该检查文件保护属性。我的猜测是它设置为 <code>NSFileProtectionComplete</code> 或 <code>NSFileProtectionCompleteUnlessOpen</code> 这意味着在设备锁定时无法读取文件。您可以将属性设置为 <code>NSFileProtectionNone</code> 以在设备锁定时读取文件,但请注意它不会以加密状态存储。</p>
<p>见:<a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/#//apple_ref/doc/constant_group/File_Protection_Values" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/#//apple_ref/doc/constant_group/File_Protection_Values</a> </p></p>
<p style="font-size: 20px;">关于ios - 从锁定屏幕处理本地操作时无法从 iOS 应用沙箱读取,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38906776/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38906776/
</a>
</p>
页:
[1]