7

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.

Justin Mathews
  • 512
  • 4
  • 18
Red Mak
  • 1,176
  • 2
  • 25
  • 56

4 Answers4

14
  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"]];
        }
    
Michaël
  • 6,676
  • 3
  • 36
  • 55
3

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.

Rakesh patanga
  • 832
  • 9
  • 25
onnoweb
  • 3,038
  • 22
  • 29
  • thank you, i was thinking the same but see this http://stackoverflow.com/questions/12082184/install-mobileconfig-programmatically (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. – Red Mak Feb 12 '13 at 22:42
  • 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. – onnoweb Feb 13 '13 at 14:49
  • 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). – Red Mak Feb 13 '13 at 15:49
  • 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! – onnoweb Feb 13 '13 at 16:08
  • i'm sure it's possible because the application i add in the edit of my question do the same thing without a server. – Red Mak Feb 13 '13 at 16:18
  • Doesn't it come from iCloud in their case? – onnoweb Feb 13 '13 at 16:25
  • 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 Feb 13 '13 at 16:59
0

I think you can use data URI to encode and launch mobileconfig. (I don't have IOS device here, so I cannot test right now_

You can use http://dopiaza.org/tools/datauri/index.php to encode your profile (don't forget to add mime type: application/x-apple-aspen-config)

Then you can open:

[[UIApplication sharedApplication] openURL:dataURLGenerated];
Deniz Mert Edincik
  • 4,336
  • 22
  • 24
  • 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: http://caniuse.com/datauri) – Red Mak Feb 14 '13 at 22:41
0

quite had no luck either but I post this anyway if someone else can use this information. I tried opening the string via data: url which is supported by Mobile Safari, but not by openURL: – sadly.

NSString *urlHeader = @"data:application/x-apple-aspen-config;charset=utf-8,";
NSString *mobileConf = @"<?xmlversion=\"1.0\"encoding=\"UTF-8\"standalone=\"yes\"?>"
    "<!DOCTYPEplistPUBLIC\"-//Apple//DTDPLIST1.0//EN\"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
    "<plistversion=\"1.0\"><dict><key>PayloadUUID</key><string>A0670934-C558-42E1-9E80-9B8E079E9AB2</string><key>PayloadDisplayName</key><string>EnableTethering</string><key>PayloadDescription</key><string>EnablesTethering</string><key>PayloadOrganization</key><string>de.iphone-notes</string><key>PayloadVersion</key><integer>1</integer><key>PayloadIdentifier</key><string>de.iphone-notes.etisalat</string><key>PayloadType</key><string>Configuration</string><key>PayloadContent</key><array><dict><key>PayloadUUID</key><string>C1A41907-0CD9-4DC9-BAF1-A04A73B7E296</string><key>PayloadDisplayName</key><string>AdvancedSettings</string><key>PayloadDescription</key><string>ProvidescustomizationofcarrierAccessPointName.</string><key>PayloadOrganization</key><string>de.sendowski</string><key>PayloadVersion</key><integer>1</integer><key>PayloadIdentifier</key><string>de.iphone-notes.etisalat.apn</string><key>PayloadContent</key><array><dict><key>DefaultsDomainName</key><string>com.apple.managedCarrier</string><key>DefaultsData</key><dict><key>apns</key><array><dict><key>apn</key><string>Etisalat.ae</string><key>username</key><string></string><key>password</key><string></string><key>type-mask</key><integer>-2</integer></dict></array></dict></dict></array><key>PayloadType</key><string>com.apple.apn.managed</string></dict></array></dict></plist>";

mobileConf = [mobileConf stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *finalURL = [NSURL URLWithString:[urlHeader stringByAppendingString:mobileConf]];

BOOL canOpen = [[UIApplication sharedApplication] openURL:finalURL];
if (canOpen) NSLog(@"can open");
else NSLog(@"can't open");

For testing you can prepend http:// before data: then it will at least open in Safari and you can delete the prefix to try it. Maybe some javascript injection to remove the prefix will work; I don't know.

  • thank you, but this doesn't work i have this alert msg when safari is open : "safari cannot open the page because it is local file." what you mean with javascript injection? beacause 1) i want to sell my app in the appstore so no hack. 2) it have to be offline. – Red Mak Feb 16 '13 at 15:08
  • I don't even know if the `.mobileconfig` is valid, so maybe it's right that it's not working. I mean something like `http://javascript:window.location=data_string_here` which is obviously not working –  Feb 16 '13 at 16:10