標籤:
這是這個主題的最後一篇,有疑問的可以留言,我盡量回複。
明確一點:Safari無法直接存取我們app的沙箱檔案。
這裡有一個解決的方法:app內部建立一個http的server,讓Safari來下載server裡面的描述檔案。(也可能有其他的方法)
在app內部建立server的方法有幾個 也有相應的第三方庫,我這裡用到的,列舉一下(我不會加跳轉的連結,你們可以去Github上下載)
使用的方法比較easy:我貼一下代碼
這是.h
#import <UIKit/UIKit.h> #import "RoutingHTTPServer.h" #import <Foundation/Foundation.h> UIBackgroundTaskIdentifier bgTask;//後台啟動並執行ID @interface AppDelegate : UIResponder <UIApplicationDelegate> { } @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic, readonly) RoutingHTTPServer *httpServer; @end
.m 我加了一句 強行改了server裡面的檔案格式 這麼做是確保Safari可以直接開啟
#import "AppDelegate.h"@interface AppDelegate ()- (void)startServer;@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self startServer]; return YES;}/** * to start a http server */- (void)startServer{ // Create server using our custom MyHTTPServer class _httpServer = [[RoutingHTTPServer alloc] init];
/* * * * * * * * * add By AK * * * * * * * * * * * * * *//* 設定檔案格式為 Apple.mobileconfig */ [_httpServer setDefaultHeader:@"Content-Type" value:@"application/x-apple-aspen-config"]; // Tell the server to broadcast its presence via Bonjour. // This allows browsers such as Safari to automatically discover our service. [_httpServer setType:@"_http._tcp."]; // Normally there‘s no need to run our server on any specific port. // Technologies like Bonjour allow clients to dynamically discover the server‘s port at runtime. // However, for easy testing you may want force a certain port so you can just hit the refresh button. [_httpServer setPort:8000];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; [_httpServer setDocumentRoot:documentsDirectory]; if (_httpServer.isRunning) [_httpServer stop]; NSError *error; if([_httpServer start:&error]) { NSLog(@"Started HTTP Server on port %hu", [_httpServer listeningPort]); } else { NSLog(@"Error starting HTTP Server: %@", error); // Probably should add an escape - but in practice never loops more than twice (bug filed on GitHub https://github.com/robbiehanson/CocoaHTTPServer/issues/88) [self startServer]; }}- (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. if(!bgTask) { bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{ dispatch_async(dispatch_get_main_queue(), ^{ [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); }]; }}- (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}
有兩點注意一下:
1、需要申請後台啟動並執行許可權,畢竟要瀏覽器來下載嘛
2、檔案存放的路徑和上一篇一致,也就是和你寫入的位置一樣
iOS app內部產生描述檔案(三)Safari開啟描述檔案