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

转载于:https://www.cnblogs.com/akforsure/p/4387514.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值