2013-02-12 164 views
7

我想在safari中打開一個移動配置文件(mobileconfig)來安裝它,但沒有任何工作。 我使用URL方案:打開.mobileconfig文件保存在Safari中的應用程序ios

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

日誌 - >can open

,我嘗試設置所有的路徑(該文件是在Documents文件夾)的文件,而不是文件名,什麼都沒有。 我該怎麼做。 ?

EDIT1:此application做同樣的(打開Safari安裝配置)

EDIT2:我認爲我必須尋找到發送文件(任何)Safari瀏覽器的方式,Safari就會知道如何處理它。

回答

13
  1. 授權後臺任務

.h文件中:

UIBackgroundTaskIdentifier bgTask; 

.m文件: 在applicationDidEnterBackground添加一個新的後臺任務:

bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [application endBackgroundTask:self->bgTask]; 
      self->bgTask = UIBackgroundTaskInvalid; 
     }); 
    }]; 
  1. CocoaHTTPServer添加到您的項目

  2. 運行服務器並打開。mobileconfig文件:

    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"]]; 
        } 
    
+0

我在CocoaHTTPServer中找不到RoutingHTTPServer。這是你創造的東西嗎? – 2013-07-08 07:32:44

+0

https://github.com/mattstevens/RoutingHTTPServer – malinois 2013-07-08 09:26:26

+0

https://github.com/robbiehanson/CocoaHTTPServer – malinois 2013-07-08 09:27:08

3

移動配置文件位於您的應用的沙盒中。 Safari無法訪問它。 [UIApplication openURL]的返回值僅指示是否有應用程序瞭解url scheme。它看起來好像你是將自己的網址發送給你自己的,假設你添加了myAppURLScheme作爲uri handler到你的info.plist文件。

+0

謝謝你,我的想法一樣,但是看到這個 http://stackoverflow.com/questions/12082184/install-mobileconfig-programmatically (不是100%相同),所以我嘗試它,因爲我不知道該怎麼做。但我認爲這將是同樣的事情,從沙箱中打開safari中的其他類型的文件。 – 2013-02-12 22:42:07

+0

在這種情況下,mobileconfig文件來自服務器到Safari。這當然有效。在Safari和Settings安裝配置文件後,url處理程序用於從Safari返回到您的應用程序。 – onnoweb 2013-02-13 14:49:18

+0

謝謝,但我的應用程序創建配置文件並將其保存在Documents文件夾中(不需要服務器),所以主要問題是如何將文件從應用沙箱發送到Safari(如「打開」操作)。 – 2013-02-13 15:49:35

0

我想你可以使用數據URI來編碼和啓動mobileconfig。 (我沒有iOS設備在這裏,所以我無法測試右now_

您可以使用http://dopiaza.org/tools/datauri/index.php來編碼您的個人資料(不要忘了添加MIME類型:應用程序/ x-蘋果白楊-配置)

然後你可以打開:

[[UIApplication sharedApplication] openURL:dataURLGenerated]; 
+0

謝謝,但我看到2個問題: 1.如何用該數據生成valide url,因爲URLWithString:返回'NO',而不是valide url? 2.如何生成該數據? 但我認爲這可能是解決方案(數據URI在ios 3.2和更高版本中支持:http://caniuse.com/datauri) – 2013-02-14 22:41:22

0

相當沒有運氣要麼但我張貼此無論如何,如果別人可以使用這個信息我試圖打開通過data:網址是由移動Safari瀏覽器支持的字符串,但不是。 openURL: - 可悲

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"); 

對於測試,您可以在data:之前加上http://,然後它至少會在Safari中打開,您可以刪除前綴以嘗試它。也許一些JavaScript注入刪除前綴將工作;我不知道。

+0

謝謝,但這不起作用,我有這個警報味精當safari打開:「Safari瀏覽器無法打開頁面,因爲它是本地文件。」 你用JavaScript注入什麼意思?因爲1)我想在應用程序商店銷售我的應用程序,所以沒有破解。 2)它必須離線。 – 2013-02-16 15:08:49

+0

我甚至不知道'.mobileconfig'是否有效,所以也許它是正確的,它不工作。我的意思是'http:// javascript:window.location = data_string_here'這顯然不起作用 – 2013-02-16 16:10:20

相關問題