如何在 swift 项目中传递值并在 swift 中访问自己创建的框架函数
how to pass value in swift project and access own created framework function in swift
我在我的框架代码下面的 swift 中创建了我自己的框架。
当我在框架中传递 ApiID、ApiKey 和 ApiSecret 值时它工作正常但我想在我的 swift 项目中传递值但是当我这样做时它不起作用。
public class PushNotification: NSObject {
var ApiID:Int?
var ApiKey:String?
var ApiSecret:String?
public func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data,apiKey:String,apiSecret:String,apiId:Int)
{
// Convert Device Token into String
let deviceTokenString = deviceToken.reduce("", {[=12=] + String(format: "%02X", )})
print(deviceTokenString)
ApiKey = apiKey
ApiSecret = apiSecret
ApiID = apiId
let AppId = Bundle.main.bundleIdentifier!
// Get Unique Device ID
let oNSUUID: UUID = UIDevice.current.identifierForVendor!
let post: String = "appid=\(AppId)&deviceid=\(deviceTokenString)&ApiID=\(ApiID)&ApiKey=\(ApiKey)&ApiSecret=\(ApiSecret)&os=\("ios")®Id=\(oNSUUID.uuidString)"
let postData:Data = post.data(using: String.Encoding.ascii)!
let postLength:NSString = String(postData.count) as NSString
let urlstring = "https://www.abc.php"
let url = URL(string: urlstring)
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.httpBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let connection: NSURLConnection = NSURLConnection(request: request as URLRequest, delegate: self)!
connection.start()
}
下面是我的项目代码,我在应用程序委托 class 中传递了值,并在下面的函数中进行了访问。
var APIKEY = "00000"
var APISECRET = "000000"
var APIID = 40
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Device Token \(deviceToken)")
let appdetails = PushNotification.init()
appdetails.didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: deviceToken, apiKey: APIKEY , apiSecret: APISECRET, apiId: APIID)
}
我得到了上面Que的解决方案。它在 Edit Schema > Archive > Post Action 中非常简单。单击 + 图标,然后单击新建 运行 时间脚本和 select 您的目标,然后将此代码粘贴到该框中
set -e
BUILD_INTERMEDIATES="${OBJROOT}"
BUILD_PRODUCTS="${SYMROOT}/../../../../Products"
DEVICE_BIN="${BUILD_INTERMEDIATES}/UninstalledProducts/iphoneos/${TARGET_NAME}.framework"
SIMULATOR_BIN="${BUILD_PRODUCTS}/Debug-iphonesimulator/${TARGET_NAME}.framework"
ARCHIVE_PATH="${SRCROOT}/_Archive"
rm -rf "${ARCHIVE_PATH}"
mkdir "${ARCHIVE_PATH}"
if [ "${CONFIGURATION}" = "Release" ]; then
if [ -d "${DEVICE_BIN}" ]; then
DEVICE_PATH="${ARCHIVE_PATH}/Release"
mkdir "${DEVICE_PATH}"
cp -r "${DEVICE_BIN}" "${DEVICE_PATH}"
fi
if [ -d "${SIMULATOR_BIN}" ]; then
SIMULATOR_PATH="${ARCHIVE_PATH}/Debug"
mkdir "${SIMULATOR_PATH}"
cp -r "${SIMULATOR_BIN}" "${SIMULATOR_PATH}"
lipo -create "${DEVICE_BIN}/${TARGET_NAME}" "${SIMULATOR_BIN}/${TARGET_NAME}" -output "${SIMULATOR_PATH}/${TARGET_NAME}.framework/${TARGET_NAME}"
fi
fi
exit 0;
我在我的框架代码下面的 swift 中创建了我自己的框架。 当我在框架中传递 ApiID、ApiKey 和 ApiSecret 值时它工作正常但我想在我的 swift 项目中传递值但是当我这样做时它不起作用。
public class PushNotification: NSObject {
var ApiID:Int?
var ApiKey:String?
var ApiSecret:String?
public func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data,apiKey:String,apiSecret:String,apiId:Int)
{
// Convert Device Token into String
let deviceTokenString = deviceToken.reduce("", {[=12=] + String(format: "%02X", )})
print(deviceTokenString)
ApiKey = apiKey
ApiSecret = apiSecret
ApiID = apiId
let AppId = Bundle.main.bundleIdentifier!
// Get Unique Device ID
let oNSUUID: UUID = UIDevice.current.identifierForVendor!
let post: String = "appid=\(AppId)&deviceid=\(deviceTokenString)&ApiID=\(ApiID)&ApiKey=\(ApiKey)&ApiSecret=\(ApiSecret)&os=\("ios")®Id=\(oNSUUID.uuidString)"
let postData:Data = post.data(using: String.Encoding.ascii)!
let postLength:NSString = String(postData.count) as NSString
let urlstring = "https://www.abc.php"
let url = URL(string: urlstring)
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.httpBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let connection: NSURLConnection = NSURLConnection(request: request as URLRequest, delegate: self)!
connection.start()
}
下面是我的项目代码,我在应用程序委托 class 中传递了值,并在下面的函数中进行了访问。
var APIKEY = "00000"
var APISECRET = "000000"
var APIID = 40
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Device Token \(deviceToken)")
let appdetails = PushNotification.init()
appdetails.didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: deviceToken, apiKey: APIKEY , apiSecret: APISECRET, apiId: APIID)
}
我得到了上面Que的解决方案。它在 Edit Schema > Archive > Post Action 中非常简单。单击 + 图标,然后单击新建 运行 时间脚本和 select 您的目标,然后将此代码粘贴到该框中
set -e
BUILD_INTERMEDIATES="${OBJROOT}"
BUILD_PRODUCTS="${SYMROOT}/../../../../Products"
DEVICE_BIN="${BUILD_INTERMEDIATES}/UninstalledProducts/iphoneos/${TARGET_NAME}.framework"
SIMULATOR_BIN="${BUILD_PRODUCTS}/Debug-iphonesimulator/${TARGET_NAME}.framework"
ARCHIVE_PATH="${SRCROOT}/_Archive"
rm -rf "${ARCHIVE_PATH}"
mkdir "${ARCHIVE_PATH}"
if [ "${CONFIGURATION}" = "Release" ]; then
if [ -d "${DEVICE_BIN}" ]; then
DEVICE_PATH="${ARCHIVE_PATH}/Release"
mkdir "${DEVICE_PATH}"
cp -r "${DEVICE_BIN}" "${DEVICE_PATH}"
fi
if [ -d "${SIMULATOR_BIN}" ]; then
SIMULATOR_PATH="${ARCHIVE_PATH}/Debug"
mkdir "${SIMULATOR_PATH}"
cp -r "${SIMULATOR_BIN}" "${SIMULATOR_PATH}"
lipo -create "${DEVICE_BIN}/${TARGET_NAME}" "${SIMULATOR_BIN}/${TARGET_NAME}" -output "${SIMULATOR_PATH}/${TARGET_NAME}.framework/${TARGET_NAME}"
fi
fi
exit 0;