Open .mobileconfig file saved in application in safari ios

11,360

Solution 1

  1. Authorize a background task

.h file :

UIBackgroundTaskIdentifier bgTask;

.m file : In applicationDidEnterBackground add a new background task :

bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];
  1. Add CocoaHTTPServer to your project

  2. Run the server and open the .mobileconfig file :

        RoutingHTTPServer *httpServer = [[RoutingHTTPServer alloc] init];
        [httpServer setType:@"_http._tcp."];
        [httpServer setPort:12345];
        [httpServer setDefaultHeader:@"Content-Type" value:@"application/x-apple-aspen-config"];
        [httpServer setDocumentRoot:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
    
        if([httpServer start:nil])
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://localhost:12345/myprofile.mobileconfig"]];
        }
    

Solution 2

The mobile config file is inside your app's sandbox. Safari doesn't have access to it. The return value of [UIApplication openURL] only indicates if there was an application that understands that url scheme. It looks to me as if you're sending that url to yourself, assuming that you added myAppURLScheme as a uri handler to your info.plist file.

Share:
11,360
Red Mak
Author by

Red Mak

iOS developer

Updated on June 18, 2022

Comments

  • Red Mak
    Red Mak almost 2 years

    I'm trying to open a mobile configuration file (mobileconfig) in safari to install it but nothing work. I use URL Scheme:

    NSURL *finalURL = [NSURL URLWithString:[NSString stringWithFormat:@"myAppURLScheme://%@",fileName]];
    BOOL canOpen = [[UIApplication sharedApplication] openURL:finalURL];
       if (canOpen) NSLog(@"can open");
       else NSLog(@"can't open");
    

    log --> can open

    and i try to set all the path(the file is in the Documents folder) to the file instead fileName, nothing. how can I do it. ?

    Edit1: this application do the same(open safari to install configuration)

    Edit2: I think that i have to search the way to send file(any) to safari, and safari will know what to do with it.

  • Red Mak
    Red Mak about 11 years
    thank you, i was thinking the same but see this stackoverflow.com/questions/12082184/… (not 100% same) so i try it because i have no idea how to do. but i think this will be the same thing to open any other type of file in safari from sandbox.
  • onnoweb
    onnoweb about 11 years
    In that scenario the mobileconfig file is coming from a server to Safari. That works, of course. The url handler is used to get back from Safari to your app after Safari and Settings have installed the profile.
  • Red Mak
    Red Mak about 11 years
    thank you, but my app create the configuration file and save it in the Documents folder(no server needed) so the main problem is how to send the file from the app sandbox to safari (like the 'open in' operation).
  • onnoweb
    onnoweb about 11 years
    I don't think that can be done. For an enterprise app I wrote we stumbled over the same and didn't find a way to do it. So if you do, I would be most interested!
  • Red Mak
    Red Mak about 11 years
    i'm sure it's possible because the application i add in the edit of my question do the same thing without a server.
  • onnoweb
    onnoweb about 11 years
    Doesn't it come from iCloud in their case?
  • Red Mak
    Red Mak about 11 years
    yes the app use icloud, but i turn off internet in the device,create a new configuration file,click into install button, and the app 'go' to safari then to settings to install it.
  • Red Mak
    Red Mak about 11 years
    thank you, but i see 2 problems: 1.how to generate a valide url with that data because URLWithString: return 'NO', not a valide url? 2.how can i generate that data? but i think this can be the solution (Data URIs are supported in ios 3.2 and higher: caniuse.com/datauri)
  • Vibhor Goyal
    Vibhor Goyal almost 11 years
    I couldn't find RoutingHTTPServer in CocoaHTTPServer. Is this something you created?