Archana SIngh Archana SIngh -4 years ago 111
Swift Question

how to pass value in swift project and access own created framework function in swift

i have created my own framework in swift below the code of my framework.
when i pass ApiID, ApiKey and ApiSecret value in framework it works fine but i want to pass values in my swift project but when i do that it doesn't work.

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("", {$0 + String(format: "%02X", $1)})
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")&regId=\(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()
}


And below is my project code i passed values in app delegate class and access in the below function.

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)
}

Answer Source

I got the Solution of above Que. its very Simple in Edit Schema > Archive > Post Action. Click on + icon and then click on New Run Time Script and select you target then just paste this code in that box

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download