iOS app内部生成描述文件(三)Safari打开描述文件

  这是这个主题的最后一篇,有疑问的可以留言,我尽量回复。

  明确一点: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、文件存放的路径和上一篇一致,也就是和你写入的位置一样

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。