【问题标题】:Objective C/ Swift How to open a base64 url?Objective C/Swift 如何打开一个base64 url​​?
【发布时间】:2015-06-30 23:38:16
【问题描述】:

Objective C/Swift 如何打开 Base64 url​​?

目前我正在尝试使用 Base64 网址请求 safari。它不工作。当我在 Mac safari verison 上执行这个特定的 url 时,它可以工作。

import UIKit

class ViewController: UIViewController {

@IBOutlet var NameText: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func homeScreen(sender: AnyObject) {
    let name = self.NameText.text
    var html = "<p>" + name + "</p>"
    let plainData = (html as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    let base64String = plainData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
    let concat = "data:text/html;base64," + base64String
    print(concat)
    UIApplication.sharedApplication().openURL(NSURL(string: concat)!)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

目前,如果我输入名称“bob”,它会生成:data:text/html;base64,PHA+Ym9iPC9wPg==

基本上,我要做的是获取用户名并为其生成 HTML。但是这个特定的 URL 没有在移动 Safari 中打开。有谁知道为什么?

谢谢, 技术大师

P.S:我理解 Obj C 和 Swift。如果您有 Obj C 解决方案,请告诉我。

编辑:我只是尝试将 url "data:text/html;base64,PHA+Ym9iPC9wPg==" 手动输入到 safari 模拟器中并且它有效。但它仍然以编程方式不起作用。似乎 openURL 函数存在问题。有人知道吗?

编辑:我认为这可能与本地主机有关。但我不知道。

【问题讨论】:

    标签: ios objective-c swift base64 mobile-safari


    【解决方案1】:

    要打开 safari,URL 需要以协议 ie 开头。 http:// 这里有一个来自 Apple 文档的支持协议列表:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instm/UIApplication/openURL:,其中包括 http、https、tel、facetime 和 mailto 方案。

    我尝试了您的代码并在字符串前面添加了协议。它带我去野生动物园。

    let concat = "http://data:text/html;base64," + base64String
    

    【讨论】:

    • 谢谢,这把我带到了 safari,但它没有运行编码的 html 的内容。在不使用应用程序的情况下,url(没有 http 协议)可以正常工作。
    猜你喜欢
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多