目标C/ Swift如何打开Base64 url?
目前,我正在尝试使用Base64 url请求safari。它不起作用。当我在Mac safari版本上执行这个特定的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中打开。有人知道为什么吗?
谢谢,DaTechnoGuru
附言:我同时使用Obj C和Swift。如果您有Obj C解决方案,请让我知道。
编辑:我只是尝试在safari模拟器中手动输入url "data:text/html;base64,PHA+Ym9iPC9wPg==“,它起作用了。但它仍然在编程上不起作用。似乎围绕着openURL函数有一个问题。有人知道这件事吗?
编辑:我认为这可能与localhost有关。但我不知道。
发布于 2015-07-01 00:02:41
要打开safari,URL需要以protocol ie开头。这里有一个苹果文档中支持的协议列表: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
https://stackoverflow.com/questions/31150302
复制相似问题