以编程方式在iPhone上安装配置文件

时间:2017-04-11 04:37:44

标签: objective-c ios10 configuration-files localserver

我实现了以下链接中提到的代码,以编程方式在iPhone中安装Profile。还安装了RoutingHTTPServer等本地服务器。

https://stackoverflow.com/a/21014275/3825016

以下是上述链接中的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    _httpServer = [[RoutingHTTPServer alloc] init];
    [_httpServer setPort:8000];                               // TODO: make sure this port isn't already in use

    _firstTime = TRUE;
    [_httpServer handleMethod:@"GET" withPath:@"/start" target:self selector:@selector(handleMobileconfigRootRequest:withResponse:)];
    [_httpServer handleMethod:@"GET" withPath:@"/load" target:self selector:@selector(handleMobileconfigLoadRequest:withResponse:)];

    NSMutableString* path = [NSMutableString stringWithString:[[NSBundle mainBundle] bundlePath]];
    [path appendString:@"/your.mobileconfig"];
    _mobileconfigData = [NSData dataWithContentsOfFile:path];

    [_httpServer start:NULL];

    return YES;
}

- (void)handleMobileconfigRootRequest:(RouteRequest *)request withResponse:(RouteResponse *)response {
    NSLog(@"handleMobileconfigRootRequest");
    [response respondWithString:@"<HTML><HEAD><title>Profile Install</title>\
     </HEAD><script> \
     function load() { window.location.href='http://localhost:8000/load/'; } \
     var int=self.setInterval(function(){load()},400); \
     </script><BODY></BODY></HTML>"];
}

- (void)handleMobileconfigLoadRequest:(RouteRequest *)request withResponse:(RouteResponse *)response {
    if( _firstTime ) {
        NSLog(@"handleMobileconfigLoadRequest, first time");
        _firstTime = FALSE;

        [response setHeader:@"Content-Type" value:@"application/x-apple-aspen-config"];
        [response respondWithData:_mobileconfigData];
    } else {
        NSLog(@"handleMobileconfigLoadRequest, NOT first time");
        [response setStatusCode:302]; // or 301
        [response setHeader:@"Location" value:@"yourapp://custom/scheme"];
    }
}

...这里是从app(即viewcontroller)调用此代码的代码:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://localhost:8000/start/"]];

基本上这里有一个本地服务器设置。我很接近要做的事情。我的问题是,当我启动应用程序时,首先打开safari中的空白页然后当我回到应用程序时,它会将我重定向到配置文件安装页面。知道为什么会这样吗?致命地困在这里。为什么最初在safari中打开空白页?

我想直接在个人资料安装页面上重定向。

1 个答案:

答案 0 :(得分:0)

在safari中打开一个空白页,因为你告诉它。这是在你的代码中:<BODY></BODY>,一个空白页面。如果它不应该是空白的,请将这些标记放在这些标记之间。

使用调试器。你有没有上过这条线?

NSLog(@"handleMobileconfigLoadRequest, NOT first time");

相关问题