自定讀取檔案格式,就可以在打開檔案的選項中看到「open in...」的選項,
並指定自己寫的app去開啓該檔案,
並指定自己寫的app去開啓該檔案,
除了一般已知的檔案格式外,
也可以自行定義副檔名,如此就可以用自己的app去開它,
(但是如果你取的副檔名跟別人一樣那就會出問題了...)
也可以自行定義副檔名,如此就可以用自己的app去開它,
(但是如果你取的副檔名跟別人一樣那就會出問題了...)
製作流程如下
- 在plist註冊自定義的副檔名
將plist以「Source code」模式打開,新增<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeDescription</key>
<string>Yellow Datalt;/string>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeIdentifier</key>
<string>yellow.abc</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>abc</string>
</dict>
</dict>
</array>
用一般模式看起來會是這樣
Description:描述(好像不能沒有)
TypeConformsTo:開啓類型(詳細參照)
UTTypeIdentifier:定義名稱
public.filename-extension:副檔名
2.在plist註冊可以支援的檔案類別
將plist以「Source code」模式打開,新增
一般模式看起來會是這樣
CFBundleTypeName:此設定的名稱
LSHandlerRank:對此種檔案類型的權限(Owner, Default, Alternate, None 參照)
LSItemContentTypes:檔案類型,這邊輸入剛剛設定的類型「yellow.abc」
3.在AppDelegate進行設定
在AppDelegate.h內新增UIApplicationDelegate
Description:描述(好像不能沒有)
TypeConformsTo:開啓類型(詳細參照)
UTTypeIdentifier:定義名稱
public.filename-extension:副檔名
2.在plist註冊可以支援的檔案類別
將plist以「Source code」模式打開,新增
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>ABC</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>yellow.abc</string>
</array>
</dict>
</array>
一般模式看起來會是這樣
CFBundleTypeName:此設定的名稱
LSHandlerRank:對此種檔案類型的權限(Owner, Default, Alternate, None 參照)
LSItemContentTypes:檔案類型,這邊輸入剛剛設定的類型「yellow.abc」
3.在AppDelegate進行設定
在AppDelegate.h內新增UIApplicationDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
}
在AppDelegate.m內新增監聽事件openURL
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// Make sure url indicates a file (as opposed to, e.g., http://)
if (url != nil && [url isFileURL]) {
// Tell our OfflineReaderView Controller to process the URL
[(ViewController *)self.window.rootViewController handleDocumentOpenURL:url];
}
// Indicate that we have successfully opened the URL
return YES;
}
4.在ViewController.m、ViewController.h進行設定
在其他app用「Open in...」的時候,
會將檔案復制到自己 app 的 Document 資料夾,
如果再度開啓,則會在檔案名稱後面加上-1 -2來作區隔,
所以要利用開啓檔案後驅動的handleDocumentOpenURL來將檔案移動到指定的地方
在ViewController.h 內設定公用函數 handleDocumentOpenURL
-(void)handleDocumentOpenURL:(NSURL *)url;
在ViewController.m 內設定公用函數 handleDocumentOpenURL 的內容
- (void)handleDocumentOpenURL:(NSURL *)DESurl {
//取得來源檔名
NSString *fileName = [DESurl lastPathComponent];
//設定移動目標路徑
NSURL *SRCUrl = [[[[DESurl URLByDeletingLastPathComponent] URLByDeletingLastPathComponent] URLByAppendingPathComponent:@"box"] URLByAppendingPathComponent:fileName];
//設定移動目標資料夾
NSURL *SRCFloder = [SRCUrl URLByDeletingLastPathComponent];
[txt setText:[NSString stringWithFormat:@"來源:%@" , [SRCUrl path]]];
BOOL isD;
//檢查副檔名
if ( [[DESurl pathExtension] isEqualToString:@"abc"]) {
NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:[SRCFloder path]]) {
//檢查資料夾是否存在,不存在則開新資料夾
if ([manager createDirectoryAtURL:SRCFloder withIntermediateDirectories:YES attributes:nil error:&error]){
NSLog(@"create************* success!");
} else {
NSLog(@"create************* fail!");
NSLog(@"error:%@" , error);
}
}
if([manager fileExistsAtPath:[SRCUrl path] isDirectory:&isD]){
//檢查檔案是否存在,存在則移除
if ([manager removeItemAtURL:SRCUrl error:&error]){
NSLog(@"remove***************** success!");
} else {
NSLog(@"remove***************** fail!");
NSLog(@"error:%@" , error);
}
}
//移動檔案
if ([manager moveItemAtURL:DESurl toURL:SRCUrl error:&error]) {
NSLog(@"move************* success!");
} else {
NSLog(@"move************* fail!");
NSLog(@"error:%@" , error);
}
// [self openTxtFileWithURL:SRCUrl];
}
}